feat: success/error icon for exit segment

resolves #357
This commit is contained in:
Jan De Dobbeleer 2021-01-19 10:08:11 +01:00 committed by Jan De Dobbeleer
parent b651c9a065
commit 225b317e32
3 changed files with 61 additions and 40 deletions

View file

@ -21,6 +21,8 @@ const (
IgnoreFolders Property = "ignore_folders"
// DisplayVersion show the version number or not
DisplayVersion Property = "display_version"
// AlwaysEnabled decides whether or not to always display the info
AlwaysEnabled Property = "always_enabled"
)
type properties struct {

View file

@ -10,12 +10,14 @@ type exit struct {
const (
// DisplayExitCode shows or hides the error code
DisplayExitCode Property = "display_exit_code"
// AlwaysEnabled decides whether or not to always display the exitcode info
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"
// SuccessIcon displays when there's no error and AlwaysEnabled = true
SuccessIcon Property = "success_icon"
// ErrorIcon displays when there's an error
ErrorIcon Property = "error_icon"
)
func (e *exit) enabled() bool {
@ -43,7 +45,10 @@ func (e *exit) getFormattedText() string {
if e.env.lastErrorCode() != 0 && colorBackground {
e.props.background = e.props.getColor(ErrorColor, e.props.background)
}
return exitCode
if e.env.lastErrorCode() == 0 {
return e.props.getString(SuccessIcon, "")
}
return fmt.Sprintf("%s%s", e.props.getString(ErrorIcon, ""), exitCode)
}
func (e *exit) getMeaningFromExitCode() string {

View file

@ -7,49 +7,63 @@ import (
)
func TestExitWriterEnabled(t *testing.T) {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(102)
e := &exit{
env: env,
cases := []struct {
ExitCode int
Expected bool
}{
{ExitCode: 102, Expected: true},
{ExitCode: 0, Expected: false},
{ExitCode: -1, Expected: true},
}
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(tc.ExitCode)
e := &exit{
env: env,
}
assert.Equal(t, tc.Expected, e.enabled())
}
assert.True(t, e.enabled())
}
func TestExitWriterDisabled(t *testing.T) {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(0)
e := &exit{
env: env,
func TestExitWriterFormattedText(t *testing.T) {
cases := []struct {
ExitCode int
Expected string
SuccessIcon string
ErrorIcon string
DisplayExitCode bool
AlwaysNumeric bool
}{
{ExitCode: 129, Expected: "SIGHUP", DisplayExitCode: true},
{ExitCode: 5001, Expected: "5001", DisplayExitCode: true},
{ExitCode: 147, Expected: "SIGSTOP", DisplayExitCode: true},
{ExitCode: 147, Expected: "", DisplayExitCode: false},
{ExitCode: 147, Expected: "147", DisplayExitCode: true, AlwaysNumeric: true},
{ExitCode: 0, Expected: "wooopie", SuccessIcon: "wooopie"},
{ExitCode: 129, Expected: "err SIGHUP", ErrorIcon: "err ", DisplayExitCode: true},
{ExitCode: 129, Expected: "err", ErrorIcon: "err", DisplayExitCode: false},
}
assert.False(t, e.enabled())
}
func TestExitWriterStandardCode(t *testing.T) {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(129)
props := &properties{
foreground: "#111111",
background: "#ffffff",
for _, tc := range cases {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(tc.ExitCode)
props := &properties{
foreground: "#111111",
background: "#ffffff",
values: map[Property]interface{}{
SuccessIcon: tc.SuccessIcon,
ErrorIcon: tc.ErrorIcon,
DisplayExitCode: tc.DisplayExitCode,
AlwaysNumeric: tc.AlwaysNumeric,
},
}
e := &exit{
env: env,
props: props,
}
assert.Equal(t, tc.Expected, e.getFormattedText())
}
e := &exit{
env: env,
props: props,
}
assert.Equal(t, "SIGHUP", e.getFormattedText())
}
func TestExitWriterNonStandardCode(t *testing.T) {
env := new(MockedEnvironment)
env.On("lastErrorCode", nil).Return(5001)
props := &properties{
foreground: "#111111",
background: "#ffffff",
}
e := &exit{
env: env,
props: props,
}
assert.Equal(t, "5001", e.getFormattedText())
}
func TestGetMeaningFromExitCode(t *testing.T) {