From 5e6ade76d5827287aa44f7d046ee922c101425e1 Mon Sep 17 00:00:00 2001 From: Moritz Meier Date: Mon, 21 Dec 2020 20:16:33 +0100 Subject: [PATCH] feat: added display_mode property to language segment --- docs/docs/segment-golang.md | 4 ++++ docs/docs/segment-julia.md | 4 ++++ docs/docs/segment-node.md | 4 ++++ docs/docs/segment-python.md | 6 +++++- segment_language.go | 34 ++++++++++++++++++++++++++++++++++ segment_language_test.go | 20 +++++++++++++++++--- themes/schema.json | 7 +++++++ 7 files changed, 75 insertions(+), 4 deletions(-) diff --git a/docs/docs/segment-golang.md b/docs/docs/segment-golang.md index 0c265b44..46014d24 100644 --- a/docs/docs/segment-golang.md +++ b/docs/docs/segment-golang.md @@ -26,3 +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` +- 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) + - `never`: The segement is hidden diff --git a/docs/docs/segment-julia.md b/docs/docs/segment-julia.md index 7d0f8776..0258e6ef 100644 --- a/docs/docs/segment-julia.md +++ b/docs/docs/segment-julia.md @@ -26,3 +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` +- 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) + - `never`: The segement is hidden diff --git a/docs/docs/segment-node.md b/docs/docs/segment-node.md index 40e5d737..e63c5dcd 100644 --- a/docs/docs/segment-node.md +++ b/docs/docs/segment-node.md @@ -26,3 +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` +- 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) + - `never`: The segement is hidden diff --git a/docs/docs/segment-python.md b/docs/docs/segment-python.md index 7b5f67cf..aed45a1d 100644 --- a/docs/docs/segment-python.md +++ b/docs/docs/segment-python.md @@ -6,7 +6,7 @@ sidebar_label: Python ## What -Display the currently active python version and virtualenv when a folder contains `.py` files or `.ipynb` files. +Display the currently active python version and virtualenv. Supports conda, virtualenv and pyenv. ## Sample Configuration @@ -27,3 +27,7 @@ Supports conda, virtualenv and pyenv. ## Properties - display_virtual_env: `boolean` - show the name of the virtualenv or not - defaults to `true` +- 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) + - `never`: The segement is hidden diff --git a/segment_language.go b/segment_language.go index f746d2a8..79401e2c 100644 --- a/segment_language.go +++ b/segment_language.go @@ -10,6 +10,17 @@ type language struct { version string } +const ( + // DisplayModeProperty sets the display mode (always, when_in_context, never) + DisplayModeProperty Property = "display_mode" + // DisplayModeAlways displays the segement always + DisplayModeAlways string = "always" + // DisplayModeContext displays the segment when the current folder contains certain extensions + DisplayModeContext string = "context" + // DisplayModeNever hides the segment + DisplayModeNever string = "never" +) + func (l *language) string() string { if l.props.getBool(DisplayVersion, true) { return l.version @@ -18,6 +29,23 @@ func (l *language) string() string { } func (l *language) enabled() bool { + displayMode := l.props.getString(DisplayModeProperty, DisplayModeContext) + displayVersion := l.props.getBool(DisplayVersion, true) + hasVersion := l.getVersion() + + switch displayMode { + case DisplayModeAlways: + return (hasVersion || !displayVersion) + case DisplayModeNever: + return false + case DisplayModeContext: + fallthrough + default: + return l.isInContext() && (hasVersion || !displayVersion) + } +} + +func (l *language) isInContext() bool { for i, extension := range l.extensions { if l.env.hasFiles(extension) { break @@ -26,6 +54,11 @@ func (l *language) enabled() bool { return false } } + + return true +} + +func (l *language) getVersion() bool { var executable string for i, command := range l.commands { if l.env.hasCommand(command) { @@ -39,5 +72,6 @@ func (l *language) enabled() bool { versionInfo, _ := l.env.runCommand(executable, l.versionParam) values := findNamedRegexMatch(l.versionRegex, versionInfo) l.version = values["version"] + return true } diff --git a/segment_language_test.go b/segment_language_test.go index 33d145f5..9954844d 100644 --- a/segment_language_test.go +++ b/segment_language_test.go @@ -15,6 +15,7 @@ const ( type languageArgs struct { version string displayVersion bool + displayMode string extensions []string enabledExtensions []string commands []string @@ -43,7 +44,8 @@ func bootStrapLanguageTest(args *languageArgs) *language { } props := &properties{ values: map[Property]interface{}{ - DisplayVersion: args.displayVersion, + DisplayVersion: args.displayVersion, + DisplayModeProperty: args.displayMode, }, } l := &language{ @@ -57,7 +59,19 @@ func bootStrapLanguageTest(args *languageArgs) *language { return l } -func TestLanguageFilesFoundButNoCommand(t *testing.T) { +func TestLanguageFilesFoundButNoCommandAndVersion(t *testing.T) { + args := &languageArgs{ + commands: []string{"unicorn"}, + versionParam: "--version", + extensions: []string{uni}, + enabledExtensions: []string{uni}, + displayVersion: true, + } + lang := bootStrapLanguageTest(args) + assert.False(t, lang.enabled(), "unicorn is not available") +} + +func TestLanguageFilesFoundButNoCommandAndNoVersion(t *testing.T) { args := &languageArgs{ commands: []string{"unicorn"}, versionParam: "--version", @@ -65,7 +79,7 @@ func TestLanguageFilesFoundButNoCommand(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) { diff --git a/themes/schema.json b/themes/schema.json index 10e562a0..6af7f6ef 100644 --- a/themes/schema.json +++ b/themes/schema.json @@ -912,6 +912,13 @@ "title": "Display Virtual Env", "description": "Show the name of the virtualenv or not", "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" } } }