Skip to content

Commit

Permalink
feat: missing command text + json schema update
Browse files Browse the repository at this point in the history
doc updated
  • Loading branch information
lnu committed Dec 30, 2020
1 parent a987351 commit 4b51add
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 23 deletions.
1 change: 1 addition & 0 deletions docs/docs/segment-dotnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions docs/docs/segment-golang.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/docs/segment-julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/docs/segment-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions docs/docs/segment-python.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/segment_dotnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 10 additions & 6 deletions src/segment_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
59 changes: 48 additions & 11 deletions src/segment_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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>.*)",
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>.*)",
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")
}
46 changes: 41 additions & 5 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -357,6 +370,12 @@
},
"display_version": {
"$ref": "#/definitions/display_version"
},
"display_mode": {
"$ref": "#/definitions/display_mode"
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
}
}
}
Expand Down Expand Up @@ -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"
}
}
}
Expand All @@ -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"
}
}
}
Expand Down Expand Up @@ -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"
}
}
}
Expand Down Expand Up @@ -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"
}
}
}
Expand Down

0 comments on commit 4b51add

Please sign in to comment.