forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_language.go
205 lines (185 loc) · 5.21 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"errors"
"fmt"
"strings"
)
type loadContext func()
type inContext func() bool
type matchesVersionFile func() bool
type version struct {
full string
major string
minor string
patch string
}
type cmd struct {
executable string
args []string
regex string
version *version
}
func (c *cmd) parse(versionInfo string) error {
values := findNamedRegexMatch(c.regex, versionInfo)
if len(values) == 0 {
return errors.New("cannot parse version string")
}
c.version = &version{}
c.version.full = values["version"]
c.version.major = values["major"]
c.version.minor = values["minor"]
c.version.patch = values["patch"]
return nil
}
func (c *cmd) buildVersionURL(template string) string {
if template == "" {
return c.version.full
}
truncatingSprintf := func(str string, args ...interface{}) (string, error) {
n := strings.Count(str, "%s")
if n > len(args) {
return "", errors.New("Too many parameters")
}
if n == 0 {
return fmt.Sprintf(str, args...), nil
}
return fmt.Sprintf(str, args[:n]...), nil
}
version, err := truncatingSprintf(template, c.version.full, c.version.major, c.version.minor, c.version.patch)
if err != nil {
return c.version.full
}
return version
}
type language struct {
props *properties
env environmentInfo
extensions []string
commands []*cmd
versionURLTemplate string
activeCommand *cmd
exitCode int
loadContext loadContext
inContext inContext
matchesVersionFile matchesVersionFile
}
const (
// DisplayMode sets the display mode (always, when_in_context, never)
DisplayMode Property = "display_mode"
// DisplayModeAlways displays the segment always
DisplayModeAlways string = "always"
// DisplayModeFiles displays the segment when the current folder contains certain extensions
DisplayModeFiles string = "files"
// DisplayModeEnvironment displays the segment when the environment has a language's context
DisplayModeEnvironment string = "environment"
// DisplayModeContext displays the segment when the environment or files is active
DisplayModeContext string = "context"
// MissingCommandText sets the text to display when the command is not present in the system
MissingCommandText Property = "missing_command_text"
// VersionMismatchColor displays empty string by default
VersionMismatchColor Property = "version_mismatch_color"
// EnableVersionMismatch displays empty string by default
EnableVersionMismatch Property = "enable_version_mismatch"
// HomeEnabled displays the segment in the HOME folder or not
HomeEnabled Property = "home_enabled"
)
func (l *language) string() string {
if !l.props.getBool(DisplayVersion, true) {
return ""
}
err := l.setVersion()
displayError := l.props.getBool(DisplayError, true)
if err != nil && displayError {
return err.Error()
}
if err != nil {
return ""
}
if l.props.getBool(EnableHyperlink, false) {
return l.activeCommand.buildVersionURL(l.versionURLTemplate)
}
if l.props.getBool(EnableVersionMismatch, false) {
l.setVersionFileMismatch()
}
return l.activeCommand.version.full
}
func (l *language) enabled() bool {
inHomeDir := func() bool {
return l.env.getcwd() == l.env.homeDir()
}
homeEnabled := l.props.getBool(HomeEnabled, false)
if inHomeDir() && !homeEnabled {
return false
}
displayMode := l.props.getString(DisplayMode, DisplayModeFiles)
l.loadLanguageContext()
switch displayMode {
case DisplayModeAlways:
return true
case DisplayModeEnvironment:
return l.inLanguageContext()
case DisplayModeFiles:
return l.hasLanguageFiles()
case DisplayModeContext:
fallthrough
default:
return l.hasLanguageFiles() || l.inLanguageContext()
}
}
// 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
}
if i == len(l.extensions)-1 {
return false
}
}
return true
}
// setVersion parses the version string returned by the command
func (l *language) setVersion() error {
for _, command := range l.commands {
if !l.env.hasCommand(command.executable) {
continue
}
version, err := l.env.runCommand(command.executable, command.args...)
if exitErr, ok := err.(*commandError); ok {
l.exitCode = exitErr.exitCode
return fmt.Errorf("err executing %s with %s", command.executable, command.args)
}
if version == "" {
continue
}
err = command.parse(version)
if err != nil {
return fmt.Errorf("err parsing info from %s with %s", command.executable, version)
}
l.activeCommand = command
return nil
}
return errors.New(l.props.getString(MissingCommandText, ""))
}
func (l *language) loadLanguageContext() {
if l.loadContext == nil {
return
}
l.loadContext()
}
func (l *language) inLanguageContext() bool {
if l.inContext == nil {
return false
}
return l.inContext()
}
func (l *language) setVersionFileMismatch() {
if l.matchesVersionFile == nil || l.matchesVersionFile() {
return
}
if l.props.getBool(ColorBackground, false) {
l.props.background = l.props.getColor(VersionMismatchColor, l.props.background)
return
}
l.props.foreground = l.props.getColor(VersionMismatchColor, l.props.foreground)
}