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
- 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
- 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
- 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
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

View file

@ -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
}

View file

@ -15,6 +15,7 @@ const (
type languageArgs struct {
version string
displayVersion bool
displayMode string
extensions []string
enabledExtensions []string
commands []string
@ -44,6 +45,7 @@ func bootStrapLanguageTest(args *languageArgs) *language {
props := &properties{
values: map[Property]interface{}{
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) {

View file

@ -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"
}
}
}