From 9457be39906e6188f09bc89d57d62b2901faa4b6 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 14 Feb 2021 12:45:06 +0100 Subject: [PATCH] refactor: hide erorr for languages resolves #400 --- docs/docs/segment-dotnet.md | 1 + docs/docs/segment-golang.md | 1 + docs/docs/segment-julia.md | 1 + docs/docs/segment-node.md | 1 + docs/docs/segment-python.md | 1 + docs/docs/segment-ruby.md | 1 + src/segment_language.go | 6 +++++- src/segment_language_test.go | 18 ++++++++++++++++++ 8 files changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/docs/segment-dotnet.md b/docs/docs/segment-dotnet.md index ea6d0389..0293edc4 100644 --- a/docs/docs/segment-dotnet.md +++ b/docs/docs/segment-dotnet.md @@ -27,6 +27,7 @@ Display the currently active .NET SDK version. - display_version: `boolean` - display the active version or not; useful if all you need is an icon indicating `dotnet` is present - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` - missing_command_text: `string` - text to display when the command is missing - defaults to empty - display_mode: `string` - determines when the segment is displayed - `always`: the segment is always displayed diff --git a/docs/docs/segment-golang.md b/docs/docs/segment-golang.md index 13e21d0a..b934c81f 100644 --- a/docs/docs/segment-golang.md +++ b/docs/docs/segment-golang.md @@ -26,6 +26,7 @@ Display the currently active golang version. ## Properties - display_version: `boolean` - display the golang version - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` - missing_command_text: `string` - text to display when the command is missing - defaults to empty - display_mode: `string` - determines when the segment is displayed - `always`: the segment is always displayed diff --git a/docs/docs/segment-julia.md b/docs/docs/segment-julia.md index f6b5fb8d..52c219ea 100644 --- a/docs/docs/segment-julia.md +++ b/docs/docs/segment-julia.md @@ -26,6 +26,7 @@ Display the currently active julia version. ## Properties - display_version: `boolean` - display the julia version - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` - missing_command_text: `string` - text to display when the command is missing - defaults to empty - display_mode: `string` - determines when the segment is displayed - `always`: the segment is always displayed diff --git a/docs/docs/segment-node.md b/docs/docs/segment-node.md index 0ca46320..27645a90 100644 --- a/docs/docs/segment-node.md +++ b/docs/docs/segment-node.md @@ -26,6 +26,7 @@ Display the currently active node version. ## Properties - display_version: `boolean` - display the node version - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` - missing_command_text: `string` - text to display when the command is missing - defaults to empty - display_mode: `string` - determines when the segment is displayed - `always`: The segment is always displayed diff --git a/docs/docs/segment-python.md b/docs/docs/segment-python.md index 63c2ebb8..8864edbe 100644 --- a/docs/docs/segment-python.md +++ b/docs/docs/segment-python.md @@ -30,6 +30,7 @@ Supports conda, virtualenv and pyenv. - display_default_env: `boolean` - show the name of the virtualenv when it's default (`system`, `base`) or not - defaults to `true` - display_version: `boolean` - display the python version - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` - missing_command_text: `string` - text to display when the command is missing - defaults to empty - display_mode: `string` - determines when the segment is displayed - `always`: the segment is always displayed diff --git a/docs/docs/segment-ruby.md b/docs/docs/segment-ruby.md index 32a32de8..39ab86f3 100644 --- a/docs/docs/segment-ruby.md +++ b/docs/docs/segment-ruby.md @@ -26,6 +26,7 @@ Display the currently active ruby version. ## Properties - display_version: `boolean` - display the ruby version - defaults to `true` +- display_error: `boolean` - show the error context when failing to retrieve the version information - defaults to `true` - missing_command_text: `string` - text to display when the command is missing - defaults to empty - display_mode: `string` - determines when the segment is displayed - `always`: the segment is always displayed diff --git a/src/segment_language.go b/src/segment_language.go index 017d7e7f..0aab485e 100644 --- a/src/segment_language.go +++ b/src/segment_language.go @@ -98,9 +98,13 @@ func (l *language) string() string { } err := l.setVersion() - if err != nil { + displayError := l.props.getBool(DisplayError, true) + if err != nil && displayError { return err.Error() } + if err != nil { + return "" + } if l.props.getBool(EnableHyperlink, false) { return l.activeCommand.buildVersionURL(l.versionURLTemplate) diff --git a/src/segment_language_test.go b/src/segment_language_test.go index 7e571f90..b9fecb0b 100644 --- a/src/segment_language_test.go +++ b/src/segment_language_test.go @@ -261,6 +261,24 @@ func TestLanguageEnabledMissingCommandCustomText(t *testing.T) { assert.Equal(t, expected, lang.string(), "unicorn is available and uni and corn files are found") } +func TestLanguageEnabledMissingCommandCustomTextHideError(t *testing.T) { + props := map[Property]interface{}{ + MissingCommandText: "missing", + DisplayError: false, + } + args := &languageArgs{ + commands: []*cmd{}, + extensions: []string{uni, corn}, + enabledExtensions: []string{uni, corn}, + enabledCommands: []string{"unicorn"}, + version: universion, + properties: props, + } + lang := bootStrapLanguageTest(args) + assert.True(t, lang.enabled()) + assert.Equal(t, "", lang.string()) +} + func TestLanguageEnabledCommandExitCode(t *testing.T) { expected := 200 args := &languageArgs{