Skip to content

Commit

Permalink
refactor(language): remap context to files
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jan 15, 2021
1 parent 31c77af commit 890f9cd
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/docs/segment-golang.md
Expand Up @@ -29,4 +29,4 @@ Display the currently active golang version when a folder contains `.go` files.
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- 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)
- `files`: The segment is only displayed when `*.go` or `go.mod` files are present (default)
2 changes: 1 addition & 1 deletion docs/docs/segment-julia.md
Expand Up @@ -29,4 +29,4 @@ Display the currently active julia version when a folder contains `.jl` files.
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- 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)
- `files`: The segment is only displayed when `*.jl` files are present (default)
2 changes: 1 addition & 1 deletion docs/docs/segment-node.md
Expand Up @@ -29,4 +29,4 @@ Display the currently active node version when a folder contains `.js` or `.ts`
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- 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)
- `files`: The segment is only displayed when `*.js`, `*.ts` or package.json files are present (default)
2 changes: 1 addition & 1 deletion docs/docs/segment-python.md
Expand Up @@ -31,4 +31,4 @@ Supports conda, virtualenv and pyenv.
- missing_command_text: `string` - text to display when the command is missing - defaults to empty
- 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)
- `files`: The segment is only displayed when `*.py` or `*.ipynb` files are present (default)
18 changes: 9 additions & 9 deletions src/segment_language.go
Expand Up @@ -15,12 +15,12 @@ type language struct {
}

const (
// DisplayModeProperty sets the display mode (always, when_in_context, never)
DisplayModeProperty Property = "display_mode"
// DisplayMode sets the display mode (always, when_in_context, never)
DisplayMode Property = "display_mode"
// DisplayModeAlways displays the segment always
DisplayModeAlways string = "always"
// DisplayModeContext displays the segment when the current folder contains certain extensions
DisplayModeContext string = "context"
// DisplayModeFiles displays the segment when the current folder contains certain extensions
DisplayModeFiles string = "files"
// MissingCommandTextProperty sets the text to display when the command is not present in the system
MissingCommandTextProperty Property = "missing_command_text"
// MissingCommandText displays empty string by default
Expand All @@ -41,21 +41,21 @@ func (l *language) string() string {
}

func (l *language) enabled() bool {
displayMode := l.props.getString(DisplayModeProperty, DisplayModeContext)
displayMode := l.props.getString(DisplayMode, DisplayModeFiles)
displayVersion := l.props.getBool(DisplayVersion, true)

switch displayMode {
case DisplayModeAlways:
return (!displayVersion || l.hasCommand())
case DisplayModeContext:
case DisplayModeFiles:
fallthrough
default:
return l.isInContext() && (!displayVersion || l.hasCommand())
return l.hasLanguageFiles() && (!displayVersion || l.hasCommand())
}
}

// isInContext will return true at least one file matching the extensions is found
func (l *language) isInContext() bool {
// hasLanguageFiles will return true at least one file matching the extensions is found
func (l *language) hasLanguageFiles() bool {
for i, extension := range l.extensions {
if l.env.hasFiles(extension) {
break
Expand Down
4 changes: 2 additions & 2 deletions src/segment_language_test.go
Expand Up @@ -45,8 +45,8 @@ func bootStrapLanguageTest(args *languageArgs) *language {
}
props := &properties{
values: map[Property]interface{}{
DisplayVersion: args.displayVersion,
DisplayModeProperty: args.displayMode,
DisplayVersion: args.displayVersion,
DisplayMode: args.displayMode,
},
}
if args.missingCommandText != "" {
Expand Down
2 changes: 1 addition & 1 deletion themes/schema.json
Expand Up @@ -21,7 +21,7 @@
"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"],
"enum": ["always", "files"],
"default": "context"
},
"missing_command_text": {
Expand Down

0 comments on commit 890f9cd

Please sign in to comment.