feat: added display_mode property to language segment

This commit is contained in:
Moritz Meier 2020-12-21 20:16:33 +01:00 committed by Jan De Dobbeleer
parent a1b2b8d67b
commit 5e6ade76d5
7 changed files with 75 additions and 4 deletions

View file

@ -26,3 +26,7 @@ Display the currently active golang version when a folder contains `.go` files.
## Properties ## Properties
- display_version: `boolean` - display the golang version - defaults to `true` - 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

View file

@ -26,3 +26,7 @@ Display the currently active julia version when a folder contains `.jl` files.
## Properties ## Properties
- display_version: `boolean` - display the julia version - defaults to `true` - 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

View file

@ -26,3 +26,7 @@ Display the currently active node version when a folder contains `.js` or `.ts`
## Properties ## Properties
- display_version: `boolean` - display the node version - defaults to `true` - 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

View file

@ -6,7 +6,7 @@ sidebar_label: Python
## What ## 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. Supports conda, virtualenv and pyenv.
## Sample Configuration ## Sample Configuration
@ -27,3 +27,7 @@ Supports conda, virtualenv and pyenv.
## Properties ## Properties
- display_virtual_env: `boolean` - show the name of the virtualenv or not - defaults to `true` - 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

View file

@ -10,6 +10,17 @@ type language struct {
version string 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 { func (l *language) string() string {
if l.props.getBool(DisplayVersion, true) { if l.props.getBool(DisplayVersion, true) {
return l.version return l.version
@ -18,6 +29,23 @@ func (l *language) string() string {
} }
func (l *language) enabled() bool { 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 { for i, extension := range l.extensions {
if l.env.hasFiles(extension) { if l.env.hasFiles(extension) {
break break
@ -26,6 +54,11 @@ func (l *language) enabled() bool {
return false return false
} }
} }
return true
}
func (l *language) getVersion() bool {
var executable string var executable string
for i, command := range l.commands { for i, command := range l.commands {
if l.env.hasCommand(command) { if l.env.hasCommand(command) {
@ -39,5 +72,6 @@ func (l *language) enabled() bool {
versionInfo, _ := l.env.runCommand(executable, l.versionParam) versionInfo, _ := l.env.runCommand(executable, l.versionParam)
values := findNamedRegexMatch(l.versionRegex, versionInfo) values := findNamedRegexMatch(l.versionRegex, versionInfo)
l.version = values["version"] l.version = values["version"]
return true return true
} }

View file

@ -15,6 +15,7 @@ const (
type languageArgs struct { type languageArgs struct {
version string version string
displayVersion bool displayVersion bool
displayMode string
extensions []string extensions []string
enabledExtensions []string enabledExtensions []string
commands []string commands []string
@ -44,6 +45,7 @@ func bootStrapLanguageTest(args *languageArgs) *language {
props := &properties{ props := &properties{
values: map[Property]interface{}{ values: map[Property]interface{}{
DisplayVersion: args.displayVersion, DisplayVersion: args.displayVersion,
DisplayModeProperty: args.displayMode,
}, },
} }
l := &language{ l := &language{
@ -57,7 +59,19 @@ func bootStrapLanguageTest(args *languageArgs) *language {
return l 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{ args := &languageArgs{
commands: []string{"unicorn"}, commands: []string{"unicorn"},
versionParam: "--version", versionParam: "--version",
@ -65,7 +79,7 @@ func TestLanguageFilesFoundButNoCommand(t *testing.T) {
enabledExtensions: []string{uni}, enabledExtensions: []string{uni},
} }
lang := bootStrapLanguageTest(args) 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) { func TestLanguageDisabledNoFiles(t *testing.T) {

View file

@ -912,6 +912,13 @@
"title": "Display Virtual Env", "title": "Display Virtual Env",
"description": "Show the name of the virtualenv or not", "description": "Show the name of the virtualenv or not",
"default": true "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"
} }
} }
} }