oh-my-posh/segment.go

149 lines
4.4 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
2020-09-17 07:51:29 -07:00
import "errors"
2019-03-13 04:14:30 -07:00
//Segment represent a single segment and it's configuration
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"`
props *properties
2020-09-27 00:37:50 -07:00
writer SegmentWriter
stringValue string
active bool
2019-03-13 04:14:30 -07:00
}
//SegmentWriter is the interface used to define what and if to write to the prompt
type SegmentWriter interface {
enabled() bool
string() string
init(props *properties, env environmentInfo)
}
//SegmentStyle the syle of segment, for more information, see the constants
type SegmentStyle string
//SegmentType the type of segment, for more information, see the constants
type SegmentType string
const (
//Session represents the user info segment
Session SegmentType = "session"
//Path represents the current path segment
Path SegmentType = "path"
//Git represents the git status and information
Git SegmentType = "git"
//Exit writes the last exit code
Exit SegmentType = "exit"
//Python writes the virtual env name
Python SegmentType = "python"
2019-03-13 04:14:30 -07:00
//Root writes root symbol
Root SegmentType = "root"
//Time writes the current timestamp
Time SegmentType = "time"
//Text writes a text
Text SegmentType = "text"
//Cmd writes the output of a shell command
Cmd SegmentType = "command"
//Battery writes the battery percentage
Battery SegmentType = "battery"
//Spotify writes the Spotify status for Mac
Spotify SegmentType = "spotify"
2020-09-15 04:44:53 -07:00
//ShellInfo writes which shell we're currently in
ShellInfo SegmentType = "shell"
2020-10-01 11:57:02 -07:00
//Node writes which node version is currently active
Node SegmentType = "node"
2020-10-09 03:51:22 -07:00
//Os write os specific icon
2020-10-07 12:01:03 -07:00
Os SegmentType = "os"
2020-10-09 10:22:32 -07:00
//EnvVar writes the content of an environment variable
EnvVar SegmentType = "envvar"
//Az writes the Azure subscription info we're currently in
Az SegmentType = "az"
//Kubectl writes the Kubernetes context we're currently in
Kubectl SegmentType = "kubectl"
//Dotnet writes which dotnet version is currently active
Dotnet SegmentType = "dotnet"
2020-10-16 07:25:38 -07:00
//Terraform writes the terraform workspace we're currently in
Terraform SegmentType = "terraform"
2020-10-22 04:47:42 -07:00
//Golang writes which go version is currently active
Golang SegmentType = "go"
2019-03-13 04:14:30 -07:00
//Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
Plain SegmentStyle = "plain"
//Diamond writes the prompt shaped with a leading and trailing symbol
Diamond SegmentStyle = "diamond"
)
func (segment *Segment) string() string {
return segment.writer.string()
}
func (segment *Segment) enabled() bool {
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
}
func (segment *Segment) hasValue(property Property, match string) bool {
if value, ok := segment.Properties[property]; ok {
list := parseStringArray(value)
for _, element := range list {
if element == match {
return true
}
}
return false
}
return false
}
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
2019-03-13 04:14:30 -07:00
functions := map[SegmentType]SegmentWriter{
2020-09-15 04:44:53 -07:00
Session: &session{},
Path: &path{},
Git: &git{},
Exit: &exit{},
Python: &python{},
2020-09-15 04:44:53 -07:00
Root: &root{},
Text: &text{},
Time: &tempus{},
Cmd: &command{},
Battery: &batt{},
Spotify: &spotify{},
ShellInfo: &shell{},
2020-10-01 11:57:02 -07:00
Node: &node{},
2020-10-07 12:01:03 -07:00
Os: &osInfo{},
2020-10-09 10:22:32 -07:00
EnvVar: &envvar{},
Az: &az{},
Kubectl: &kubectl{},
Dotnet: &dotnet{},
2020-10-16 07:25:38 -07:00
Terraform: &terraform{},
2020-10-22 04:47:42 -07:00
Golang: &golang{},
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
segment.props = props
return nil
2019-03-13 04:14:30 -07:00
}
return errors.New("Unable to map writer")
2019-03-13 04:14:30 -07:00
}