feat: 'exit' segment: option to always use numeric codes

This commit is contained in:
Shaun Sutterfield 2020-11-27 07:51:13 -06:00 committed by Jan De Dobbeleer
parent 6f1a7b9d39
commit bfb21739d4
3 changed files with 21 additions and 0 deletions

View file

@ -34,5 +34,6 @@ Displays the last exit code or that the last command failed based on the configu
- always_enabled: `boolean` - always show the status - defaults to `false`
- color_background: `boolean` - color the background or foreground when an error occurs - defaults to `false`
- error_color: `string` [color][colors] - color to use when an error occured
- always_numeric: `boolean` - always display exit code as a number - defaults to `false`
[colors]: /docs/configure#colors

View file

@ -14,6 +14,8 @@ const (
AlwaysEnabled Property = "always_enabled"
// ErrorColor specify a different foreground color for the error text when using always_show = true
ErrorColor Property = "error_color"
// AlwaysNumeric shows error codes as numbers
AlwaysNumeric Property = "always_numeric"
)
func (e *exit) enabled() bool {
@ -48,6 +50,9 @@ func (e *exit) getMeaningFromExitCode() string {
if !e.props.getBool(DisplayExitCode, true) {
return ""
}
if e.props.getBool(AlwaysNumeric, false) {
return fmt.Sprintf("%d", e.env.lastErrorCode())
}
switch e.env.lastErrorCode() {
case 1:
return "ERROR"

View file

@ -91,3 +91,18 @@ func TestGetMeaningFromExitCode(t *testing.T) {
assert.Equal(t, want, e.getMeaningFromExitCode())
}
}
func TestAlwaysNumericExitCode(t *testing.T) {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(1)
props := &properties{
values: map[Property]interface{}{
AlwaysNumeric: true,
},
}
e := &exit{
env: env,
props: props,
}
assert.Equal(t, "1", e.getMeaningFromExitCode())
}