oh-my-posh/src/engine/segment.go

337 lines
11 KiB
Go
Raw Normal View History

2022-01-26 23:38:46 -08:00
package engine
2019-03-13 04:14:30 -07:00
import (
"errors"
"fmt"
"oh-my-posh/color"
"oh-my-posh/environment"
"oh-my-posh/properties"
2022-01-26 06:54:36 -08:00
"oh-my-posh/segments"
"oh-my-posh/template"
"runtime/debug"
"strings"
"time"
)
2020-09-17 07:51:29 -07:00
// Segment represent a single segment and it's configuration
2019-03-13 04:14:30 -07:00
type Segment struct {
Type SegmentType `json:"type,omitempty"`
Tips []string `json:"tips,omitempty"`
Style SegmentStyle `json:"style,omitempty"`
PowerlineSymbol string `json:"powerline_symbol,omitempty"`
InvertPowerline bool `json:"invert_powerline,omitempty"`
Foreground string `json:"foreground,omitempty"`
ForegroundTemplates color.Templates `json:"foreground_templates,omitempty"`
Background string `json:"background,omitempty"`
BackgroundTemplates color.Templates `json:"background_templates,omitempty"`
LeadingDiamond string `json:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty"`
Properties properties.Map `json:"properties,omitempty"`
writer SegmentWriter
text string
enabled bool
env environment.Environment
backgroundCache string
foregroundCache string
2019-03-13 04:14:30 -07:00
}
// SegmentTiming holds the timing context for a segment
type SegmentTiming struct {
2022-02-02 03:16:39 -08:00
name string
nameLength int
active bool
text string
duration time.Duration
}
// 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
Template() string
Init(props properties.Properties, env environment.Environment)
2019-03-13 04:14:30 -07:00
}
// SegmentStyle the style of segment, for more information, see the constants
2019-03-13 04:14:30 -07:00
type SegmentStyle string
// SegmentType the type of segment, for more information, see the constants
2019-03-13 04:14:30 -07:00
type SegmentType string
const (
2022-01-26 05:10:18 -08:00
// 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"
// PLASTIC represents the plastic scm status and information
PLASTIC SegmentType = "plastic"
// EXIT writes the last exit code
EXIT SegmentType = "exit"
// PYTHON writes the virtual env name
PYTHON SegmentType = "python"
// 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"
// SHELL writes which shell we're currently in
SHELL SegmentType = "shell"
// NODE writes which node version is currently active
NODE SegmentType = "node"
// OS write os specific icon
OS SegmentType = "os"
// 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"
// TERRAFORM writes the terraform workspace we're currently in
TERRAFORM SegmentType = "terraform"
// GOLANG writes which go version is currently active
GOLANG SegmentType = "go"
// JULIA writes which julia version is currently active
JULIA SegmentType = "julia"
// Powerline writes it Powerline style
2019-03-13 04:14:30 -07:00
Powerline SegmentStyle = "powerline"
// Plain writes it without ornaments
2019-03-13 04:14:30 -07:00
Plain SegmentStyle = "plain"
// Diamond writes the prompt shaped with a leading and trailing symbol
2019-03-13 04:14:30 -07:00
Diamond SegmentStyle = "diamond"
// YTM writes YouTube Music information and status
YTM SegmentType = "ytm"
2022-01-26 05:10:18 -08:00
// EXECUTIONTIME writes the execution time of the last run command
EXECUTIONTIME SegmentType = "executiontime"
// RUBY writes which ruby version is currently active
RUBY SegmentType = "ruby"
// AWS writes the active aws context
AWS SegmentType = "aws"
// JAVA writes the active java version
JAVA SegmentType = "java"
// POSHGIT writes the posh git prompt
POSHGIT SegmentType = "poshgit"
// AZFUNC writes current AZ func version
AZFUNC SegmentType = "azfunc"
// CRYSTAL writes the active crystal version
CRYSTAL SegmentType = "crystal"
// DART writes the active dart version
DART SegmentType = "dart"
// NBGV writes the nbgv version information
NBGV SegmentType = "nbgv"
// RUST writes the cargo version information if cargo.toml is present
RUST SegmentType = "rust"
// OWM writes the weather coming from openweatherdata
OWM SegmentType = "owm"
2022-01-26 05:10:18 -08:00
// SYSTEMINFO writes system information (memory, cpu, load)
SYSTEMINFO SegmentType = "sysinfo"
// ANGULAR writes which angular cli version us currently active
ANGULAR SegmentType = "angular"
2021-10-27 01:52:56 -07:00
// PHP writes which php version is currently active
PHP SegmentType = "php"
2022-01-26 05:10:18 -08:00
// NIGHTSCOUT is an open source diabetes system
NIGHTSCOUT SegmentType = "nightscout"
// STRAVA is a sports activity tracker
STRAVA SegmentType = "strava"
// WAKATIME writes tracked time spend in dev editors
WAKATIME SegmentType = "wakatime"
// WIFI writes details about the current WIFI connection
WIFI SegmentType = "wifi"
// WINREG queries the Windows registry.
WINREG SegmentType = "winreg"
2021-12-14 23:49:32 -08:00
// Brewfather segment
2022-01-26 05:10:18 -08:00
BREWFATHER SegmentType = "brewfather"
// IPIFY segment
IPIFY SegmentType = "ipify"
2022-02-08 00:11:00 -08:00
// HASKELL segment
HASKELL SegmentType = "haskell"
2022-02-13 23:41:33 -08:00
// UI5 Tooling segment
UI5TOOLING SegmentType = "ui5tooling"
2022-02-16 08:23:40 -08:00
// Cloud Foundry segment
CF SegmentType = "cf"
2022-02-19 05:45:21 -08:00
// Cloud Foundry logged in target
CFTARGET SegmentType = "cftarget"
2022-03-01 01:01:58 -08:00
// KOTLIN writes the active kotlin version
KOTLIN SegmentType = "kotlin"
2022-03-02 17:38:09 -08:00
// SWIFT writes the active swift version
SWIFT SegmentType = "swift"
2022-03-06 13:52:12 -08:00
// cds (SAP CAP) version
CDS SegmentType = "cds"
2022-03-10 09:41:48 -08:00
// npm version
NPM SegmentType = "npm"
2022-03-11 06:28:25 -08:00
// Project version
PROJECT SegmentType = "project"
2022-03-19 01:06:06 -07:00
// R version
R SegmentType = "r"
2019-03-13 04:14:30 -07:00
)
func (segment *Segment) shouldIncludeFolder() bool {
if segment.env == nil {
return true
}
cwdIncluded := segment.cwdIncluded()
cwdExcluded := segment.cwdExcluded()
return cwdIncluded && !cwdExcluded
2021-02-27 20:05:51 -08:00
}
func (segment *Segment) cwdIncluded() bool {
value, ok := segment.Properties[properties.IncludeFolders]
2021-02-27 20:05:51 -08:00
if !ok {
// IncludeFolders isn't specified, everything is included
return true
}
list := properties.ParseStringArray(value)
2021-02-27 20:05:51 -08:00
if len(list) == 0 {
// IncludeFolders is an empty array, everything is included
return true
}
return environment.DirMatchesOneOf(segment.env, segment.env.Pwd(), list)
2021-02-27 20:05:51 -08:00
}
func (segment *Segment) cwdExcluded() bool {
value, ok := segment.Properties[properties.ExcludeFolders]
2021-02-27 20:05:51 -08:00
if !ok {
value = segment.Properties[properties.IgnoreFolders]
2021-02-27 20:05:51 -08:00
}
list := properties.ParseStringArray(value)
return environment.DirMatchesOneOf(segment.env, segment.env.Pwd(), list)
2020-10-02 07:58:25 -07:00
}
2021-06-05 07:14:44 -07:00
func (segment *Segment) shouldInvokeWithTip(tip string) bool {
for _, t := range segment.Tips {
if t == tip {
return true
}
}
return false
}
func (segment *Segment) foreground() string {
if len(segment.foregroundCache) == 0 {
segment.foregroundCache = segment.ForegroundTemplates.Resolve(segment.writer, segment.env, segment.Foreground)
}
return segment.foregroundCache
}
func (segment *Segment) background() string {
if len(segment.backgroundCache) == 0 {
segment.backgroundCache = segment.BackgroundTemplates.Resolve(segment.writer, segment.env, segment.Background)
}
return segment.backgroundCache
}
func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error {
segment.env = env
2019-03-13 04:14:30 -07:00
functions := map[SegmentType]SegmentWriter{
2022-03-11 06:28:25 -08:00
PROJECT: &segments.Project{},
2022-03-10 09:41:48 -08:00
NPM: &segments.Npm{},
2022-01-26 06:54:36 -08:00
OWM: &segments.Owm{},
SESSION: &segments.Session{},
PATH: &segments.Path{},
GIT: &segments.Git{},
PLASTIC: &segments.Plastic{},
EXIT: &segments.Exit{},
PYTHON: &segments.Python{},
ROOT: &segments.Root{},
TEXT: &segments.Text{},
TIME: &segments.Time{},
CMD: &segments.Cmd{},
BATTERY: &segments.Battery{},
SPOTIFY: &segments.Spotify{},
SHELL: &segments.Shell{},
NODE: &segments.Node{},
OS: &segments.Os{},
AZ: &segments.Az{},
KUBECTL: &segments.Kubectl{},
DOTNET: &segments.Dotnet{},
TERRAFORM: &segments.Terraform{},
GOLANG: &segments.Golang{},
JULIA: &segments.Julia{},
YTM: &segments.Ytm{},
EXECUTIONTIME: &segments.Executiontime{},
RUBY: &segments.Ruby{},
AWS: &segments.Aws{},
JAVA: &segments.Java{},
POSHGIT: &segments.PoshGit{},
AZFUNC: &segments.AzFunc{},
CRYSTAL: &segments.Crystal{},
DART: &segments.Dart{},
NBGV: &segments.Nbgv{},
RUST: &segments.Rust{},
SYSTEMINFO: &segments.SystemInfo{},
ANGULAR: &segments.Angular{},
PHP: &segments.Php{},
NIGHTSCOUT: &segments.Nightscout{},
STRAVA: &segments.Strava{},
WAKATIME: &segments.Wakatime{},
WIFI: &segments.Wifi{},
WINREG: &segments.WindowsRegistry{},
BREWFATHER: &segments.Brewfather{},
IPIFY: &segments.IPify{},
2022-02-08 00:11:00 -08:00
HASKELL: &segments.Haskell{},
2022-02-13 23:41:33 -08:00
UI5TOOLING: &segments.UI5Tooling{},
2022-02-16 08:23:40 -08:00
CF: &segments.Cf{},
2022-02-19 05:45:21 -08:00
CFTARGET: &segments.CfTarget{},
2022-03-01 01:01:58 -08:00
KOTLIN: &segments.Kotlin{},
2022-03-02 17:38:09 -08:00
SWIFT: &segments.Swift{},
2022-03-06 13:52:12 -08:00
CDS: &segments.Cds{},
2022-03-19 01:06:06 -07:00
R: &segments.R{},
2019-03-13 04:14:30 -07:00
}
if segment.Properties == nil {
segment.Properties = make(properties.Map)
}
2019-03-13 04:14:30 -07:00
if writer, ok := functions[segment.Type]; ok {
writer.Init(segment.Properties, env)
2019-03-13 04:14:30 -07:00
segment.writer = writer
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
}
2022-02-02 03:16:39 -08:00
func (segment *Segment) string() string {
segmentTemplate := segment.Properties.GetString(properties.SegmentTemplate, segment.writer.Template())
tmpl := &template.Text{
Template: segmentTemplate,
Context: segment.writer,
Env: segment.env,
}
text, err := tmpl.Render()
if err != nil {
return err.Error()
}
return text
}
func (segment *Segment) renderText(env environment.Environment) {
defer func() {
err := recover()
if err == nil {
return
}
// display a message explaining omp failed(with the err)
message := fmt.Sprintf("\noh-my-posh fatal error rendering %s segment:%s\n\n%s\n", segment.Type, err, debug.Stack())
fmt.Println(message)
2022-02-02 03:16:39 -08:00
segment.text = "error"
segment.enabled = true
}()
err := segment.mapSegmentWithWriter(env)
if err != nil || !segment.shouldIncludeFolder() {
return
}
2022-02-02 03:16:39 -08:00
if segment.writer.Enabled() {
segment.text = segment.string()
segment.enabled = len(strings.ReplaceAll(segment.text, " ", "")) > 0
}
}