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

440 lines
14 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"
2022-08-17 09:12:18 -07:00
"runtime/debug"
"strings"
"time"
2022-11-09 11:27:54 -08:00
"oh-my-posh/platform"
"oh-my-posh/properties"
2022-01-26 06:54:36 -08:00
"oh-my-posh/segments"
"oh-my-posh/shell"
2022-01-26 06:54:36 -08:00
"oh-my-posh/template"
c "golang.org/x/text/cases"
"golang.org/x/text/language"
)
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 template.List `json:"foreground_templates,omitempty"`
Background string `json:"background,omitempty"`
BackgroundTemplates template.List `json:"background_templates,omitempty"`
LeadingDiamond string `json:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty"`
Template string `json:"template,omitempty"`
Templates template.List `json:"templates,omitempty"`
TemplatesLogic template.Logic `json:"templates_logic,omitempty"`
Properties properties.Map `json:"properties,omitempty"`
Interactive bool `json:"interactive,omitempty"`
Alias string `json:"alias,omitempty"`
MaxWidth int `json:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty"`
env platform.Environment
writer SegmentWriter
Enabled bool `json:"-"`
text string
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
2022-11-09 11:27:54 -08:00
Init(props properties.Properties, env platform.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 (
// Plain writes it without ornaments
Plain SegmentStyle = "plain"
2022-04-11 03:40:20 -07:00
// Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
// Accordion writes it Powerline style but collapses the segment when disabled instead of hiding
Accordion SegmentStyle = "accordion"
// Diamond writes the prompt shaped with a leading and trailing symbol
Diamond SegmentStyle = "diamond"
// ANGULAR writes which angular cli version us currently active
ANGULAR SegmentType = "angular"
2022-01-26 05:10:18 -08:00
// AWS writes the active aws context
AWS SegmentType = "aws"
// AZ writes the Azure subscription info we're currently in
AZ SegmentType = "az"
2022-01-26 05:10:18 -08:00
// AZFUNC writes current AZ func version
AZFUNC SegmentType = "azfunc"
// BATTERY writes the battery percentage
BATTERY SegmentType = "battery"
// Brewfather segment
BREWFATHER SegmentType = "brewfather"
// cds (SAP CAP) version
CDS SegmentType = "cds"
// Cloud Foundry segment
CF SegmentType = "cf"
// Cloud Foundry logged in target
CFTARGET SegmentType = "cftarget"
2022-07-15 01:17:37 -07:00
// CMAKE writes the active cmake version
CMAKE SegmentType = "cmake"
// CMD writes the output of a shell command
CMD SegmentType = "command"
// CONNECTION writes a connection's information
CONNECTION SegmentType = "connection"
2022-01-26 05:10:18 -08:00
// CRYSTAL writes the active crystal version
CRYSTAL SegmentType = "crystal"
// DART writes the active dart version
DART SegmentType = "dart"
2022-09-18 00:01:52 -07:00
// DENO writes the active deno version
DENO SegmentType = "deno"
// DOTNET writes which dotnet version is currently active
DOTNET SegmentType = "dotnet"
// EXECUTIONTIME writes the execution time of the last run command
EXECUTIONTIME SegmentType = "executiontime"
// EXIT writes the last exit code
EXIT SegmentType = "exit"
2022-05-24 01:13:01 -07:00
// FLUTTER writes the flutter version
FLUTTER SegmentType = "flutter"
2022-06-05 07:30:39 -07:00
// FOSSIL writes the fossil status
FOSSIL SegmentType = "fossil"
2022-08-17 09:12:18 -07:00
// GCP writes the active GCP context
GCP SegmentType = "gcp"
// GIT represents the git status and information
GIT SegmentType = "git"
2022-08-26 11:58:48 -07:00
// GITVERSION represents the gitversion information
GITVERSION SegmentType = "gitversion"
// GOLANG writes which go version is currently active
GOLANG SegmentType = "go"
// HASKELL segment
HASKELL SegmentType = "haskell"
// IPIFY segment
IPIFY SegmentType = "ipify"
// ITERM inserts the Shell Integration prompt mark on iTerm zsh/bash/fish
ITERM SegmentType = "iterm"
// JAVA writes the active java version
JAVA SegmentType = "java"
// JULIA writes which julia version is currently active
JULIA SegmentType = "julia"
// KOTLIN writes the active kotlin version
KOTLIN SegmentType = "kotlin"
// KUBECTL writes the Kubernetes context we're currently in
KUBECTL SegmentType = "kubectl"
2022-07-18 21:38:38 -07:00
// LUA writes the active lua version
LUA SegmentType = "lua"
2022-01-26 05:10:18 -08:00
// NBGV writes the nbgv version information
NBGV SegmentType = "nbgv"
// NIGHTSCOUT is an open source diabetes system
NIGHTSCOUT SegmentType = "nightscout"
// NODE writes which node version is currently active
NODE SegmentType = "node"
// npm version
NPM SegmentType = "npm"
2022-05-26 05:51:26 -07:00
// NX writes which Nx version us currently active
NX SegmentType = "nx"
// OS write os specific icon
OS SegmentType = "os"
// OWM writes the weather coming from openweatherdata
OWM SegmentType = "owm"
// PATH represents the current path segment
PATH SegmentType = "path"
2022-07-08 03:21:08 -07:00
// PERL writes which perl version is currently active
PERL SegmentType = "perl"
2021-10-27 01:52:56 -07:00
// PHP writes which php version is currently active
PHP SegmentType = "php"
// PLASTIC represents the plastic scm status and information
PLASTIC SegmentType = "plastic"
// Project version
PROJECT SegmentType = "project"
// PYTHON writes the virtual env name
PYTHON SegmentType = "python"
// R version
R SegmentType = "r"
// ROOT writes root symbol
ROOT SegmentType = "root"
// RUBY writes which ruby version is currently active
RUBY SegmentType = "ruby"
// RUST writes the cargo version information if cargo.toml is present
RUST SegmentType = "rust"
// SESSION represents the user info segment
SESSION SegmentType = "session"
// SHELL writes which shell we're currently in
SHELL SegmentType = "shell"
// SPOTIFY writes the SPOTIFY status for Mac
SPOTIFY SegmentType = "spotify"
2022-01-26 05:10:18 -08:00
// STRAVA is a sports activity tracker
STRAVA SegmentType = "strava"
2022-03-30 15:33:15 -07:00
// Subversion segment
SVN SegmentType = "svn"
// SWIFT writes the active swift version
SWIFT SegmentType = "swift"
// SYSTEMINFO writes system information (memory, cpu, load)
SYSTEMINFO SegmentType = "sysinfo"
// TERRAFORM writes the terraform workspace we're currently in
TERRAFORM SegmentType = "terraform"
// TEXT writes a text
TEXT SegmentType = "text"
// TIME writes the current timestamp
TIME SegmentType = "time"
// UI5 Tooling segment
UI5TOOLING SegmentType = "ui5tooling"
2022-01-26 05:10:18 -08:00
// WAKATIME writes tracked time spend in dev editors
WAKATIME SegmentType = "wakatime"
// WINREG queries the Windows registry.
WINREG SegmentType = "winreg"
2022-07-17 12:11:23 -07:00
// WITHINGS queries the Withings API.
WITHINGS SegmentType = "withings"
2022-11-05 05:22:05 -07:00
// XMAKE write the xmake version if xmake.lua is present
XMAKE SegmentType = "xmake"
// YTM writes YouTube Music information and status
YTM SegmentType = "ytm"
2019-03-13 04:14:30 -07:00
)
2022-12-26 10:18:35 -08:00
// Segments contains all available prompt segment writers.
// Consumers of the library can also add their own segment writer.
var Segments = map[SegmentType]SegmentWriter{
ANGULAR: &segments.Angular{},
AWS: &segments.Aws{},
AZ: &segments.Az{},
AZFUNC: &segments.AzFunc{},
BATTERY: &segments.Battery{},
BREWFATHER: &segments.Brewfather{},
CDS: &segments.Cds{},
CF: &segments.Cf{},
CFTARGET: &segments.CfTarget{},
CMD: &segments.Cmd{},
CONNECTION: &segments.Connection{},
CRYSTAL: &segments.Crystal{},
CMAKE: &segments.Cmake{},
DART: &segments.Dart{},
DENO: &segments.Deno{},
DOTNET: &segments.Dotnet{},
EXECUTIONTIME: &segments.Executiontime{},
EXIT: &segments.Exit{},
FLUTTER: &segments.Flutter{},
FOSSIL: &segments.Fossil{},
GCP: &segments.Gcp{},
GIT: &segments.Git{},
GITVERSION: &segments.GitVersion{},
GOLANG: &segments.Golang{},
HASKELL: &segments.Haskell{},
IPIFY: &segments.IPify{},
ITERM: &segments.ITerm{},
JAVA: &segments.Java{},
JULIA: &segments.Julia{},
KOTLIN: &segments.Kotlin{},
KUBECTL: &segments.Kubectl{},
LUA: &segments.Lua{},
NBGV: &segments.Nbgv{},
NIGHTSCOUT: &segments.Nightscout{},
NODE: &segments.Node{},
NPM: &segments.Npm{},
NX: &segments.Nx{},
OS: &segments.Os{},
OWM: &segments.Owm{},
PATH: &segments.Path{},
PERL: &segments.Perl{},
PHP: &segments.Php{},
PLASTIC: &segments.Plastic{},
PROJECT: &segments.Project{},
PYTHON: &segments.Python{},
R: &segments.R{},
ROOT: &segments.Root{},
RUBY: &segments.Ruby{},
RUST: &segments.Rust{},
SESSION: &segments.Session{},
SHELL: &segments.Shell{},
SPOTIFY: &segments.Spotify{},
STRAVA: &segments.Strava{},
SVN: &segments.Svn{},
SWIFT: &segments.Swift{},
SYSTEMINFO: &segments.SystemInfo{},
TERRAFORM: &segments.Terraform{},
TEXT: &segments.Text{},
TIME: &segments.Time{},
UI5TOOLING: &segments.UI5Tooling{},
WAKATIME: &segments.Wakatime{},
WINREG: &segments.WindowsRegistry{},
WITHINGS: &segments.Withings{},
XMAKE: &segments.XMake{},
YTM: &segments.Ytm{},
}
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
}
2022-04-11 03:40:20 -07:00
func (segment *Segment) isPowerline() bool {
return segment.Style == Powerline || segment.Style == Accordion
}
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 segment.env.DirMatchesOneOf(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 segment.env.DirMatchesOneOf(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.FirstMatch(segment.writer, segment.env, segment.Foreground)
}
return segment.foregroundCache
}
func (segment *Segment) background() string {
if len(segment.backgroundCache) == 0 {
segment.backgroundCache = segment.BackgroundTemplates.FirstMatch(segment.writer, segment.env, segment.Background)
}
return segment.backgroundCache
}
2022-11-09 11:27:54 -08:00
func (segment *Segment) mapSegmentWithWriter(env platform.Environment) error {
segment.env = env
2022-12-26 10:18:35 -08:00
if segment.Properties == nil {
segment.Properties = make(properties.Map)
}
2022-12-26 10:18:35 -08:00
if writer, ok := Segments[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
}
2022-12-26 10:18:35 -08: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 {
var templatesResult string
if !segment.Templates.Empty() {
templatesResult = segment.Templates.Resolve(segment.writer, segment.env, "", segment.TemplatesLogic)
if len(segment.Template) == 0 {
return templatesResult
}
}
2022-03-27 01:12:47 -07:00
if len(segment.Template) == 0 {
segment.Template = segment.writer.Template()
}
2022-02-02 03:16:39 -08:00
tmpl := &template.Text{
Template: segment.Template,
Context: segment.writer,
Env: segment.env,
TemplatesResult: templatesResult,
2022-02-02 03:16:39 -08:00
}
text, err := tmpl.Render()
if err != nil {
return err.Error()
}
return text
}
2022-11-09 11:27:54 -08:00
func (segment *Segment) SetEnabled(env platform.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)
segment.Enabled = true
}()
err := segment.mapSegmentWithWriter(env)
if err != nil || !segment.shouldIncludeFolder() {
return
}
// validate toggles
if toggles, OK := segment.env.Cache().Get(platform.TOGGLECACHE); OK && len(toggles) > 0 {
list := strings.Split(toggles, ",")
for _, toggle := range list {
if SegmentType(toggle) == segment.Type {
return
}
}
}
if shouldHideForWidth(segment.env, segment.MinWidth, segment.MaxWidth) {
return
}
2022-02-02 03:16:39 -08:00
if segment.writer.Enabled() {
segment.Enabled = true
name := segment.Alias
if len(name) == 0 {
name = c.Title(language.English).String(string(segment.Type))
}
env.TemplateCache().AddSegmentData(name, segment.writer)
}
}
func (segment *Segment) SetText() {
if !segment.Enabled {
return
}
segment.text = segment.string()
2022-05-14 12:18:03 -07:00
segment.Enabled = len(strings.ReplaceAll(segment.text, " ", "")) > 0
if segment.Interactive {
return
}
// we have to do this to prevent bash/zsh from misidentifying escape sequences
2022-05-14 11:50:20 -07:00
switch segment.env.Shell() {
case shell.BASH:
segment.text = strings.NewReplacer("`", "\\`", `\`, `\\`).Replace(segment.text)
2022-05-14 11:50:20 -07:00
case shell.ZSH:
segment.text = strings.NewReplacer("`", "\\`", `%`, `%%`).Replace(segment.text)
}
}