2020-11-14 11:04:04 -08:00
|
|
|
package main
|
|
|
|
|
2021-01-10 03:14:43 -08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-01-12 11:38:13 -08:00
|
|
|
type loadContext func()
|
|
|
|
|
|
|
|
type inContext func() bool
|
|
|
|
|
2021-01-10 03:14:43 -08:00
|
|
|
type version struct {
|
|
|
|
full string
|
|
|
|
major string
|
|
|
|
minor string
|
|
|
|
patch string
|
|
|
|
regex string
|
|
|
|
urlTemplate string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *version) parse(versionInfo string) error {
|
|
|
|
values := findNamedRegexMatch(v.regex, versionInfo)
|
|
|
|
if len(values) == 0 {
|
|
|
|
return errors.New("cannot parse version string")
|
|
|
|
}
|
|
|
|
|
|
|
|
v.full = values["version"]
|
|
|
|
v.major = values["major"]
|
|
|
|
v.minor = values["minor"]
|
|
|
|
v.patch = values["patch"]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
2021-01-10 03:14:43 -08:00
|
|
|
version *version
|
2020-12-27 23:33:58 -08:00
|
|
|
exitCode int
|
2021-01-12 11:38:13 -08:00
|
|
|
loadContext loadContext
|
|
|
|
inContext inContext
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:16:33 -08:00
|
|
|
const (
|
2021-01-12 11:37:14 -08:00
|
|
|
// DisplayMode sets the display mode (always, when_in_context, never)
|
|
|
|
DisplayMode Property = "display_mode"
|
2020-12-31 23:00:08 -08:00
|
|
|
// DisplayModeAlways displays the segment always
|
2020-12-21 11:16:33 -08:00
|
|
|
DisplayModeAlways string = "always"
|
2021-01-12 11:37:14 -08:00
|
|
|
// DisplayModeFiles displays the segment when the current folder contains certain extensions
|
|
|
|
DisplayModeFiles string = "files"
|
2021-01-12 11:38:13 -08:00
|
|
|
// DisplayModeEnvironment displays the segment when the environment has a language's context
|
|
|
|
DisplayModeEnvironment string = "environment"
|
2021-01-14 23:08:56 -08:00
|
|
|
// DisplayModeContext displays the segment when the environment or files is active
|
|
|
|
DisplayModeContext string = "context"
|
2021-01-05 03:28:33 -08:00
|
|
|
// MissingCommandTextProperty sets the text to display when the command is not present in the system
|
2020-12-27 23:33:58 -08:00
|
|
|
MissingCommandTextProperty Property = "missing_command_text"
|
2021-01-05 03:28:33 -08:00
|
|
|
// MissingCommandText displays empty string by default
|
2020-12-27 23:33:58 -08:00
|
|
|
MissingCommandText string = ""
|
2020-12-21 11:16:33 -08:00
|
|
|
)
|
|
|
|
|
2020-11-14 11:04:04 -08:00
|
|
|
func (l *language) string() string {
|
2021-01-14 23:08:56 -08:00
|
|
|
if !l.props.getBool(DisplayVersion, true) {
|
|
|
|
return ""
|
|
|
|
}
|
2020-12-27 23:33:58 -08:00
|
|
|
if !l.hasCommand() {
|
|
|
|
return l.props.getString(MissingCommandTextProperty, MissingCommandText)
|
|
|
|
}
|
2021-01-10 03:14:43 -08:00
|
|
|
|
|
|
|
err := l.setVersion()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// build release notes hyperlink
|
|
|
|
if l.props.getBool(EnableHyperlink, false) && l.version.urlTemplate != "" {
|
|
|
|
version, err := TruncatingSprintf(l.version.urlTemplate, l.version.full, l.version.major, l.version.minor, l.version.patch)
|
|
|
|
if err != nil {
|
|
|
|
return l.version.full
|
|
|
|
}
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
return l.version.full
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *language) enabled() bool {
|
2021-01-12 11:38:13 -08:00
|
|
|
l.loadLanguageContext()
|
2021-01-14 23:08:56 -08:00
|
|
|
displayMode := l.props.getString(DisplayMode, DisplayModeFiles)
|
2020-12-21 11:16:33 -08:00
|
|
|
switch displayMode {
|
|
|
|
case DisplayModeAlways:
|
2021-01-14 23:08:56 -08:00
|
|
|
return true
|
2021-01-12 11:38:13 -08:00
|
|
|
case DisplayModeEnvironment:
|
|
|
|
return l.inLanguageContext()
|
2021-01-12 11:37:14 -08:00
|
|
|
case DisplayModeFiles:
|
2021-01-14 23:08:56 -08:00
|
|
|
return l.hasLanguageFiles()
|
|
|
|
case DisplayModeContext:
|
2020-12-21 11:16:33 -08:00
|
|
|
fallthrough
|
|
|
|
default:
|
2021-01-14 23:08:56 -08:00
|
|
|
return l.hasLanguageFiles() || l.inLanguageContext()
|
2020-12-21 11:16:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:37:14 -08:00
|
|
|
// hasLanguageFiles will return true at least one file matching the extensions is found
|
|
|
|
func (l *language) hasLanguageFiles() 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
|
|
|
|
}
|
|
|
|
|
2021-01-10 03:14:43 -08:00
|
|
|
// setVersion parses the version string returned by the command
|
|
|
|
func (l *language) setVersion() error {
|
2020-12-27 23:33:58 -08:00
|
|
|
versionInfo, err := l.env.runCommand(l.executable, l.versionParam)
|
2021-01-14 23:08:56 -08:00
|
|
|
if exitErr, ok := err.(*commandError); ok {
|
|
|
|
l.exitCode = exitErr.exitCode
|
2021-01-10 03:14:43 -08:00
|
|
|
return errors.New("error executing command")
|
2020-12-27 23:33:58 -08:00
|
|
|
}
|
2021-01-10 03:14:43 -08:00
|
|
|
err = l.version.parse(versionInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2020-12-27 23:33:58 -08:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-01-05 04:05:37 -08:00
|
|
|
if l.env.hasCommand(command) {
|
|
|
|
l.executable = command
|
2020-11-14 11:04:04 -08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == len(l.commands)-1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-01-12 11:38:13 -08:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2021-01-10 03:14:43 -08:00
|
|
|
|
|
|
|
func TruncatingSprintf(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
|
|
|
|
}
|