forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsegment_language.go
77 lines (68 loc) · 1.75 KB
/
segment_language.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
type language struct {
props *properties
env environmentInfo
extensions []string
commands []string
versionParam string
versionRegex 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 {
if l.props.getBool(DisplayVersion, true) {
return l.version
}
return ""
}
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
}
if i == len(l.extensions)-1 {
return false
}
}
return true
}
func (l *language) getVersion() bool {
var executable string
for i, command := range l.commands {
if l.env.hasCommand(command) {
executable = command
break
}
if i == len(l.commands)-1 {
return false
}
}
versionInfo, _ := l.env.runCommand(executable, l.versionParam)
values := findNamedRegexMatch(l.versionRegex, versionInfo)
l.version = values["version"]
return true
}