2020-11-14 11:04:04 -08:00
|
|
|
package main
|
|
|
|
|
2020-12-27 23:33:58 -08:00
|
|
|
import "errors"
|
|
|
|
|
2020-11-14 11:04:04 -08:00
|
|
|
type language struct {
|
|
|
|
props *properties
|
|
|
|
env environmentInfo
|
|
|
|
extensions []string
|
|
|
|
commands []string
|
2020-12-27 23:33:58 -08:00
|
|
|
executable string
|
2020-11-14 11:04:04 -08:00
|
|
|
versionParam string
|
|
|
|
versionRegex string
|
|
|
|
version string
|
2020-12-27 23:33:58 -08:00
|
|
|
exitCode int
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:16:33 -08:00
|
|
|
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"
|
2020-12-27 23:33:58 -08:00
|
|
|
// 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 = ""
|
2020-12-21 11:16:33 -08:00
|
|
|
)
|
|
|
|
|
2020-11-14 11:04:04 -08:00
|
|
|
func (l *language) string() string {
|
2020-12-27 23:33:58 -08:00
|
|
|
// 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() {
|
2020-11-14 11:04:04 -08:00
|
|
|
return l.version
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *language) enabled() bool {
|
2020-12-21 11:16:33 -08:00
|
|
|
displayMode := l.props.getString(DisplayModeProperty, DisplayModeContext)
|
|
|
|
displayVersion := l.props.getBool(DisplayVersion, true)
|
|
|
|
|
|
|
|
switch displayMode {
|
|
|
|
case DisplayModeAlways:
|
2020-12-27 23:33:58 -08:00
|
|
|
return (!displayVersion || l.hasCommand())
|
2020-12-21 11:16:33 -08:00
|
|
|
case DisplayModeContext:
|
|
|
|
fallthrough
|
|
|
|
default:
|
2020-12-27 23:33:58 -08:00
|
|
|
return l.isInContext() && (!displayVersion || l.hasCommand())
|
2020-12-21 11:16:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:33:58 -08:00
|
|
|
// isInContext will return true at least one file matching the extensions is found
|
2020-12-21 11:16:33 -08:00
|
|
|
func (l *language) isInContext() bool {
|
2020-11-14 11:04:04 -08:00
|
|
|
for i, extension := range l.extensions {
|
|
|
|
if l.env.hasFiles(extension) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == len(l.extensions)-1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 11:16:33 -08:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-31 11:07:59 -08:00
|
|
|
// getVersion returns the version and exit code returned by the executable
|
2020-12-21 11:16:33 -08:00
|
|
|
func (l *language) getVersion() bool {
|
2020-12-27 23:33:58 -08:00
|
|
|
versionInfo, err := l.env.runCommand(l.executable, l.versionParam)
|
|
|
|
var exerr *commandError
|
|
|
|
if err == nil {
|
|
|
|
values := findNamedRegexMatch(l.versionRegex, versionInfo)
|
|
|
|
l.exitCode = 0
|
|
|
|
l.version = values["version"]
|
|
|
|
} else {
|
|
|
|
errors.As(err, &exerr)
|
|
|
|
l.exitCode = exerr.exitCode
|
|
|
|
l.version = ""
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-31 11:07:59 -08:00
|
|
|
// hasCommand checks if one of the commands exists and sets it as executable
|
2020-12-27 23:33:58 -08:00
|
|
|
func (l *language) hasCommand() bool {
|
2020-11-14 11:04:04 -08:00
|
|
|
for i, command := range l.commands {
|
2020-12-27 23:33:58 -08:00
|
|
|
commandPath, commandExists := l.env.hasCommand(command)
|
|
|
|
if commandExists {
|
|
|
|
l.executable = commandPath
|
2020-11-14 11:04:04 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == len(l.commands)-1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|