From 4b51addf7f88eabc5baf5a3f02fa406c14fb27cb Mon Sep 17 00:00:00 2001 From: lnu Date: Wed, 30 Dec 2020 09:34:46 +0100 Subject: [PATCH] feat: missing command text + json schema update doc updated --- 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 | 2 ++ src/segment_dotnet_test.go | 2 +- src/segment_language.go | 16 ++++++---- src/segment_language_test.go | 59 +++++++++++++++++++++++++++++------- themes/schema.json | 46 +++++++++++++++++++++++++--- 9 files changed, 106 insertions(+), 23 deletions(-) diff --git a/docs/docs/segment-dotnet.md b/docs/docs/segment-dotnet.md index 2ce5920f9ebb..8b9d8a658b4c 100644 --- a/docs/docs/segment-dotnet.md +++ b/docs/docs/segment-dotnet.md @@ -27,5 +27,6 @@ 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` +- missing_command_text: `string` - text to display when the command is missing - default to `` - unsupported_version_icon: `string` - text/icon that is displayed when the active .NET SDK version (e.g., one specified by `global.json`) is not installed/supported - defaults to `\u2327` (X in a rectangle box) diff --git a/docs/docs/segment-golang.md b/docs/docs/segment-golang.md index 46014d247f61..e9b36fff5567 100644 --- a/docs/docs/segment-golang.md +++ b/docs/docs/segment-golang.md @@ -26,6 +26,7 @@ Display the currently active golang version when a folder contains `.go` files. ## Properties - display_version: `boolean` - display the golang version - defaults to `true` +- missing_command_text: `string` - text to display when the command is missing - default to `` - display_mode: `string` - determines when the segment is displayed - `always`: The segment is always displayed - `context`: The segment is only displayed when *.go or go.mod files are present (default) diff --git a/docs/docs/segment-julia.md b/docs/docs/segment-julia.md index 0258e6efa859..6aeeb5856e97 100644 --- a/docs/docs/segment-julia.md +++ b/docs/docs/segment-julia.md @@ -26,6 +26,7 @@ Display the currently active julia version when a folder contains `.jl` files. ## Properties - display_version: `boolean` - display the julia version - defaults to `true` +- missing_command_text: `string` - text to display when the command is missing - default to `` - display_mode: `string` - determines when the segment is displayed - `always`: The segment is always displayed - `context`: The segment is only displayed when *.jl files are present (default) diff --git a/docs/docs/segment-node.md b/docs/docs/segment-node.md index e63c5dcdc40f..853aeeafd01d 100644 --- a/docs/docs/segment-node.md +++ b/docs/docs/segment-node.md @@ -26,6 +26,7 @@ Display the currently active node version when a folder contains `.js` or `.ts` ## Properties - display_version: `boolean` - display the node version - defaults to `true` +- missing_command_text: `string` - text to display when the command is missing - default to `` - display_mode: `string` - determines when the segment is displayed - `always`: The segment is always displayed - `context`: The segment is only displayed when *.js, *.ts or package.json files are present (default) diff --git a/docs/docs/segment-python.md b/docs/docs/segment-python.md index aed45a1d82d4..899e668232cb 100644 --- a/docs/docs/segment-python.md +++ b/docs/docs/segment-python.md @@ -27,6 +27,8 @@ Supports conda, virtualenv and pyenv. ## Properties - display_virtual_env: `boolean` - show the name of the virtualenv or not - defaults to `true` +- display_version: `boolean` - display the python version - defaults to `true` +- missing_command_text: `string` - text to display when the command is missing - default to `` - display_mode: `string` - determines when the segment is displayed - `always`: The segment is always displayed - `context`: The segment is only displayed when *.py or *.ipynb files are present (default) diff --git a/src/segment_dotnet_test.go b/src/segment_dotnet_test.go index 3d5ff478d8a8..f8f7f88dfffb 100644 --- a/src/segment_dotnet_test.go +++ b/src/segment_dotnet_test.go @@ -42,7 +42,7 @@ func TestEnabledDotnetNotFound(t *testing.T) { enabled: false, } dotnet := bootStrapDotnetTest(args) - assert.False(t, dotnet.enabled()) + assert.True(t, dotnet.enabled()) } func TestDotnetVersionNotDisplayed(t *testing.T) { diff --git a/src/segment_language.go b/src/segment_language.go index 6e0db33e4eb0..db9e02e686b0 100644 --- a/src/segment_language.go +++ b/src/segment_language.go @@ -23,22 +23,26 @@ const ( DisplayModeContext string = "context" // DisplayModeNever hides the segment DisplayModeNever string = "never" + // MissingCommandProperty sets the text to display when the command is not present in the system + MissingCommandTextProperty Property = "missing_command_text" + // MissingCommand displays empty string by default + MissingCommandText string = "" ) func (l *language) string() string { + // check if one of the defined commands exists in the system + if !l.hasCommand() { + return l.props.getString(MissingCommandTextProperty, MissingCommandText) + } + // call getVersion if displayVersion set in config - if l.props.getBool(DisplayVersion, true) && l.getVersion() { + if l.props.getBool(DisplayVersion, true) && l.hasCommand() && l.getVersion() { return l.version } return "" } func (l *language) enabled() bool { - // check if one of the defined commands exists in the system - if !l.hasCommand() { - return false - } - displayMode := l.props.getString(DisplayModeProperty, DisplayModeContext) displayVersion := l.props.getBool(DisplayVersion, true) hasCommand := l.hasCommand() diff --git a/src/segment_language_test.go b/src/segment_language_test.go index d0b517b1b722..5362a7a13738 100644 --- a/src/segment_language_test.go +++ b/src/segment_language_test.go @@ -13,15 +13,16 @@ const ( ) type languageArgs struct { - version string - displayVersion bool - displayMode string - extensions []string - enabledExtensions []string - commands []string - enabledCommands []string - versionParam string - versionRegex string + version string + displayVersion bool + displayMode string + extensions []string + enabledExtensions []string + commands []string + enabledCommands []string + versionParam string + versionRegex string + missingCommandText string } func (l *languageArgs) hasvalue(value string, list []string) bool { @@ -48,6 +49,9 @@ func bootStrapLanguageTest(args *languageArgs) *language { DisplayModeProperty: args.displayMode, }, } + if args.missingCommandText != "" { + props.values[MissingCommandTextProperty] = args.missingCommandText + } l := &language{ props: props, env: env, @@ -68,7 +72,7 @@ func TestLanguageFilesFoundButNoCommandAndVersionAndDisplayVersion(t *testing.T) displayVersion: true, } lang := bootStrapLanguageTest(args) - assert.False(t, lang.enabled(), "unicorn is not available") + assert.True(t, lang.enabled(), "unicorn is not available") } func TestLanguageFilesFoundButNoCommandAndVersionAndDontDisplayVersion(t *testing.T) { @@ -91,7 +95,7 @@ func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) { enabledExtensions: []string{uni}, } lang := bootStrapLanguageTest(args) - assert.False(t, lang.enabled(), "unicorn is not available") + assert.True(t, lang.enabled(), "unicorn is not available") } func TestLanguageDisabledNoFiles(t *testing.T) { @@ -184,3 +188,36 @@ func TestLanguageEnabledNoVersion(t *testing.T) { assert.True(t, lang.enabled()) assert.Equal(t, "", lang.string(), "unicorn is available and uni and corn files are found") } + +func TestLanguageEnabledMissingCommand(t *testing.T) { + args := &languageArgs{ + versionParam: "--version", + commands: []string{""}, + enabledCommands: []string{"unicorn"}, + extensions: []string{uni, corn}, + versionRegex: "(?P.*)", + version: universion, + enabledExtensions: []string{uni, corn}, + displayVersion: false, + } + lang := bootStrapLanguageTest(args) + assert.True(t, lang.enabled()) + assert.Equal(t, "", lang.string(), "unicorn is available and uni and corn files are found") +} + +func TestLanguageEnabledMissingCommandCustomText(t *testing.T) { + args := &languageArgs{ + versionParam: "--version", + commands: []string{""}, + enabledCommands: []string{"unicorn"}, + extensions: []string{uni, corn}, + versionRegex: "(?P.*)", + version: universion, + enabledExtensions: []string{uni, corn}, + displayVersion: false, + missingCommandText: "missing", + } + lang := bootStrapLanguageTest(args) + assert.True(t, lang.enabled()) + assert.Equal(t, args.missingCommandText, lang.string(), "unicorn is available and uni and corn files are found") +} diff --git a/themes/schema.json b/themes/schema.json index 785a3cab01a9..42755f4c3e39 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -17,6 +17,19 @@ "description": "Show or hide the version number", "default": true }, + "display_mode": { + "type": "string", + "title": "Display Mode", + "description": "Determines whether the segment is displayed always or only if a file matching the extensions are present in the current folder", + "enum": ["always", "context", "never"], + "default": "context" + }, + "missing_command_text": { + "type": "string", + "title": "Missing command text", + "description": "The string to display when the command is not available", + "default": "" + }, "block": { "type": "object", "description": "https://ohmyposh.dev/docs/configure#block", @@ -357,6 +370,12 @@ }, "display_version": { "$ref": "#/definitions/display_version" + }, + "display_mode": { + "$ref": "#/definitions/display_mode" + }, + "missing_command_text": { + "$ref": "#/definitions/missing_command_text" } } } @@ -613,6 +632,12 @@ "properties": { "display_version": { "$ref": "#/definitions/display_version" + }, + "display_mode": { + "$ref": "#/definitions/display_mode" + }, + "missing_command_text": { + "$ref": "#/definitions/missing_command_text" } } } @@ -633,6 +658,12 @@ "properties": { "display_version": { "$ref": "#/definitions/display_version" + }, + "display_mode": { + "$ref": "#/definitions/display_mode" + }, + "missing_command_text": { + "$ref": "#/definitions/missing_command_text" } } } @@ -664,6 +695,12 @@ "properties": { "display_version": { "$ref": "#/definitions/display_version" + }, + "display_mode": { + "$ref": "#/definitions/display_mode" + }, + "missing_command_text": { + "$ref": "#/definitions/missing_command_text" } } } @@ -914,11 +951,10 @@ "default": true }, "display_mode": { - "type": "string", - "title": "Display Mode", - "description": "Determines whether the segment is displayed always or only if *.py or *.ipynb file are present in the current folder", - "enum": ["always", "context", "never"], - "default": "context" + "$ref": "#/definitions/display_mode" + }, + "missing_command_text": { + "$ref": "#/definitions/missing_command_text" } } }