2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
2020-11-14 23:47:00 -08:00
|
|
|
import (
|
|
|
|
"errors"
|
2020-12-06 10:34:42 -08:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2020-11-14 23:47:00 -08:00
|
|
|
"time"
|
|
|
|
)
|
2020-09-17 07:51:29 -07:00
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// Segment represent a single segment and it's configuration
|
2019-03-13 04:14:30 -07:00
|
|
|
type Segment struct {
|
2020-09-27 00:37:50 -07:00
|
|
|
Type SegmentType `json:"type"`
|
|
|
|
Style SegmentStyle `json:"style"`
|
|
|
|
PowerlineSymbol string `json:"powerline_symbol"`
|
|
|
|
InvertPowerline bool `json:"invert_powerline"`
|
|
|
|
Foreground string `json:"foreground"`
|
|
|
|
Background string `json:"background"`
|
|
|
|
LeadingDiamond string `json:"leading_diamond"`
|
|
|
|
TrailingDiamond string `json:"trailing_diamond"`
|
|
|
|
Properties map[Property]interface{} `json:"properties"`
|
2020-10-12 23:57:46 -07:00
|
|
|
props *properties
|
2020-09-27 00:37:50 -07:00
|
|
|
writer SegmentWriter
|
2020-10-12 23:57:46 -07:00
|
|
|
stringValue string
|
|
|
|
active bool
|
2020-11-14 23:47:00 -08:00
|
|
|
timing time.Duration
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// SegmentWriter is the interface used to define what and if to write to the prompt
|
2019-03-13 04:14:30 -07:00
|
|
|
type SegmentWriter interface {
|
|
|
|
enabled() bool
|
|
|
|
string() string
|
|
|
|
init(props *properties, env environmentInfo)
|
|
|
|
}
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// SegmentStyle the syle of segment, for more information, see the constants
|
2019-03-13 04:14:30 -07:00
|
|
|
type SegmentStyle string
|
|
|
|
|
2020-11-12 00:43:32 -08:00
|
|
|
// SegmentType the type of segment, for more information, see the constants
|
2019-03-13 04:14:30 -07:00
|
|
|
type SegmentType string
|
|
|
|
|
|
|
|
const (
|
2020-11-12 00:43:32 -08:00
|
|
|
// Session represents the user info segment
|
2019-03-13 04:14:30 -07:00
|
|
|
Session SegmentType = "session"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Path represents the current path segment
|
2019-03-13 04:14:30 -07:00
|
|
|
Path SegmentType = "path"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Git represents the git status and information
|
2019-03-13 04:14:30 -07:00
|
|
|
Git SegmentType = "git"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Exit writes the last exit code
|
2019-03-13 04:14:30 -07:00
|
|
|
Exit SegmentType = "exit"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Python writes the virtual env name
|
2020-10-02 02:55:27 -07:00
|
|
|
Python SegmentType = "python"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Root writes root symbol
|
2019-03-13 04:14:30 -07:00
|
|
|
Root SegmentType = "root"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Time writes the current timestamp
|
2019-03-13 04:14:30 -07:00
|
|
|
Time SegmentType = "time"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Text writes a text
|
2019-03-13 04:14:30 -07:00
|
|
|
Text SegmentType = "text"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Cmd writes the output of a shell command
|
2019-03-13 04:14:30 -07:00
|
|
|
Cmd SegmentType = "command"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Battery writes the battery percentage
|
2019-03-13 04:14:30 -07:00
|
|
|
Battery SegmentType = "battery"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Spotify writes the Spotify status for Mac
|
2019-03-13 04:14:30 -07:00
|
|
|
Spotify SegmentType = "spotify"
|
2020-11-12 00:43:32 -08:00
|
|
|
// ShellInfo writes which shell we're currently in
|
2020-09-15 04:44:53 -07:00
|
|
|
ShellInfo SegmentType = "shell"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Node writes which node version is currently active
|
2020-10-01 11:57:02 -07:00
|
|
|
Node SegmentType = "node"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Os write os specific icon
|
2020-10-07 12:01:03 -07:00
|
|
|
Os SegmentType = "os"
|
2020-11-12 00:43:32 -08:00
|
|
|
// EnvVar writes the content of an environment variable
|
2020-10-09 10:22:32 -07:00
|
|
|
EnvVar SegmentType = "envvar"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Az writes the Azure subscription info we're currently in
|
2020-10-14 16:56:25 -07:00
|
|
|
Az SegmentType = "az"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Kubectl writes the Kubernetes context we're currently in
|
2020-10-15 10:48:41 -07:00
|
|
|
Kubectl SegmentType = "kubectl"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Dotnet writes which dotnet version is currently active
|
2020-10-16 08:43:02 -07:00
|
|
|
Dotnet SegmentType = "dotnet"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Terraform writes the terraform workspace we're currently in
|
2020-10-16 07:25:38 -07:00
|
|
|
Terraform SegmentType = "terraform"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Golang writes which go version is currently active
|
2020-10-22 04:47:42 -07:00
|
|
|
Golang SegmentType = "go"
|
2020-11-14 11:04:04 -08:00
|
|
|
// Julia writes which julia version is currently active
|
|
|
|
Julia SegmentType = "julia"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Powerline writes it Powerline style
|
2019-03-13 04:14:30 -07:00
|
|
|
Powerline SegmentStyle = "powerline"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Plain writes it without ornaments
|
2019-03-13 04:14:30 -07:00
|
|
|
Plain SegmentStyle = "plain"
|
2020-11-12 00:43:32 -08:00
|
|
|
// Diamond writes the prompt shaped with a leading and trailing symbol
|
2019-03-13 04:14:30 -07:00
|
|
|
Diamond SegmentStyle = "diamond"
|
2020-11-19 19:12:20 -08:00
|
|
|
// YTM writes YouTube Music information and status
|
|
|
|
YTM SegmentType = "ytm"
|
2020-12-06 13:03:40 -08:00
|
|
|
// ExecutionTime writes the execution time of the last run command
|
|
|
|
ExecutionTime SegmentType = "executiontime"
|
2019-03-13 04:14:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (segment *Segment) string() string {
|
|
|
|
return segment.writer.string()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (segment *Segment) enabled() bool {
|
2020-10-12 23:57:46 -07:00
|
|
|
segment.active = segment.writer.enabled()
|
|
|
|
return segment.active
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2020-10-02 07:58:25 -07:00
|
|
|
func (segment *Segment) getValue(property Property, defaultValue string) string {
|
|
|
|
if value, ok := segment.Properties[property]; ok {
|
|
|
|
return parseString(value, defaultValue)
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2020-12-06 10:34:42 -08:00
|
|
|
func (segment *Segment) shouldIgnoreFolder(cwd string) bool {
|
|
|
|
if value, ok := segment.Properties[IgnoreFolders]; ok {
|
2020-10-02 07:58:25 -07:00
|
|
|
list := parseStringArray(value)
|
|
|
|
for _, element := range list {
|
2020-12-06 10:34:42 -08:00
|
|
|
pattern := fmt.Sprintf("^%s$", element)
|
|
|
|
matched, err := regexp.MatchString(pattern, cwd)
|
|
|
|
if err == nil && matched {
|
2020-10-02 07:58:25 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-10-12 23:57:46 -07:00
|
|
|
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
|
2019-03-13 04:14:30 -07:00
|
|
|
functions := map[SegmentType]SegmentWriter{
|
2020-12-06 13:03:40 -08:00
|
|
|
Session: &session{},
|
|
|
|
Path: &path{},
|
|
|
|
Git: &git{},
|
|
|
|
Exit: &exit{},
|
|
|
|
Python: &python{},
|
|
|
|
Root: &root{},
|
|
|
|
Text: &text{},
|
|
|
|
Time: &tempus{},
|
|
|
|
Cmd: &command{},
|
|
|
|
Battery: &batt{},
|
|
|
|
Spotify: &spotify{},
|
|
|
|
ShellInfo: &shell{},
|
|
|
|
Node: &node{},
|
|
|
|
Os: &osInfo{},
|
|
|
|
EnvVar: &envvar{},
|
|
|
|
Az: &az{},
|
|
|
|
Kubectl: &kubectl{},
|
|
|
|
Dotnet: &dotnet{},
|
|
|
|
Terraform: &terraform{},
|
|
|
|
Golang: &golang{},
|
|
|
|
Julia: &julia{},
|
|
|
|
YTM: &ytm{},
|
|
|
|
ExecutionTime: &executiontime{},
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
if writer, ok := functions[segment.Type]; ok {
|
|
|
|
props := &properties{
|
|
|
|
values: segment.Properties,
|
|
|
|
foreground: segment.Foreground,
|
|
|
|
background: segment.Background,
|
|
|
|
}
|
|
|
|
writer.init(props, env)
|
|
|
|
segment.writer = writer
|
2020-10-12 23:57:46 -07:00
|
|
|
segment.props = props
|
|
|
|
return nil
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2020-11-12 00:43:32 -08:00
|
|
|
return errors.New("unable to map writer")
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
2020-11-14 23:47:00 -08:00
|
|
|
|
|
|
|
func (segment *Segment) setStringValue(env environmentInfo, cwd string, debug bool) {
|
|
|
|
err := segment.mapSegmentWithWriter(env)
|
2020-12-06 10:34:42 -08:00
|
|
|
if err != nil || segment.shouldIgnoreFolder(cwd) {
|
2020-11-14 23:47:00 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// add timing only in debug
|
|
|
|
if debug {
|
|
|
|
start := time.Now()
|
|
|
|
defer (func() {
|
|
|
|
// force segment rendering to display the time it took
|
|
|
|
// to check if the segment is enabled or not
|
|
|
|
// depending on the segement, calling enabled()
|
|
|
|
// can be time consuming
|
|
|
|
segment.active = true
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
segment.timing = elapsed
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
if segment.enabled() {
|
|
|
|
segment.stringValue = segment.string()
|
|
|
|
}
|
|
|
|
}
|