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"
|
2021-11-11 23:40:08 -08:00
|
|
|
"runtime/debug"
|
2021-04-18 10:16:06 -07:00
|
|
|
"time"
|
2020-11-14 23:47:00 -08:00
|
|
|
)
|
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 {
|
2021-11-26 01:37:33 -08:00
|
|
|
Type SegmentType `config:"type"`
|
|
|
|
Tips []string `config:"tips"`
|
|
|
|
Style SegmentStyle `config:"style"`
|
|
|
|
PowerlineSymbol string `config:"powerline_symbol"`
|
|
|
|
InvertPowerline bool `config:"invert_powerline"`
|
|
|
|
Foreground string `config:"foreground"`
|
|
|
|
ForegroundTemplates []string `config:"foreground_templates"`
|
|
|
|
Background string `config:"background"`
|
|
|
|
BackgroundTemplates []string `config:"background_templates"`
|
|
|
|
LeadingDiamond string `config:"leading_diamond"`
|
|
|
|
TrailingDiamond string `config:"trailing_diamond"`
|
|
|
|
Properties properties `config:"properties"`
|
2021-02-13 07:27:31 -08:00
|
|
|
writer SegmentWriter
|
|
|
|
stringValue string
|
|
|
|
active bool
|
2021-05-26 12:12:58 -07:00
|
|
|
env environmentInfo
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
2021-04-18 10:16:06 -07:00
|
|
|
// SegmentTiming holds the timing context for a segment
|
|
|
|
type SegmentTiming struct {
|
|
|
|
name string
|
|
|
|
nameLength int
|
|
|
|
enabled bool
|
|
|
|
stringValue string
|
|
|
|
enabledDuration time.Duration
|
|
|
|
stringDuration time.Duration
|
|
|
|
}
|
|
|
|
|
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
|
2021-11-26 01:37:33 -08:00
|
|
|
init(props properties, env environmentInfo)
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
|
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"
|
2020-10-25 08:32:14 -07:00
|
|
|
// Ruby writes which ruby version is currently active
|
|
|
|
Ruby SegmentType = "ruby"
|
2021-02-07 01:55:09 -08:00
|
|
|
// Aws writes the active aws context
|
|
|
|
Aws SegmentType = "aws"
|
2021-03-17 00:16:19 -07:00
|
|
|
// Java writes the active java version
|
|
|
|
Java SegmentType = "java"
|
2021-03-27 09:04:09 -07:00
|
|
|
// PoshGit writes the posh git prompt
|
|
|
|
PoshGit SegmentType = "poshgit"
|
2021-04-02 10:36:50 -07:00
|
|
|
// AZFunc writes current AZ func version
|
|
|
|
AZFunc SegmentType = "azfunc"
|
2021-05-14 04:39:49 -07:00
|
|
|
// Crystal writes the active crystal version
|
|
|
|
Crystal SegmentType = "crystal"
|
2021-05-14 12:26:26 -07:00
|
|
|
// Dart writes the active dart version
|
|
|
|
Dart SegmentType = "dart"
|
2021-06-04 10:27:39 -07:00
|
|
|
// Nbgv writes the nbgv version information
|
|
|
|
Nbgv SegmentType = "nbgv"
|
2021-07-04 13:53:10 -07:00
|
|
|
// Rust writes the cargo version information if cargo.toml is present
|
|
|
|
Rust SegmentType = "rust"
|
2021-08-17 23:21:55 -07:00
|
|
|
// OWM writes the weather coming from openweatherdata
|
|
|
|
OWM SegmentType = "owm"
|
2021-11-13 14:46:06 -08:00
|
|
|
// SysInfo writes system information (memory, cpu, load)
|
|
|
|
SysInfo SegmentType = "sysinfo"
|
2021-10-13 00:05:29 -07:00
|
|
|
// 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"
|
2021-11-23 01:34:35 -08:00
|
|
|
// Nightscout is an open source diabetes system
|
|
|
|
Nightscout SegmentType = "nightscout"
|
2021-11-22 16:32:05 -08:00
|
|
|
// WiFi writes details about the current WiFi connection
|
|
|
|
WiFi SegmentType = "wifi"
|
2021-11-24 04:47:30 -08:00
|
|
|
// WinReg queries the Windows registry.
|
|
|
|
WinReg SegmentType = "winreg"
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-20 08:01:19 -07:00
|
|
|
func (segment *Segment) shouldIncludeFolder() bool {
|
|
|
|
cwdIncluded := segment.cwdIncluded()
|
|
|
|
cwdExcluded := segment.cwdExcluded()
|
2021-02-27 20:05:51 -08:00
|
|
|
return (cwdIncluded && !cwdExcluded)
|
|
|
|
}
|
|
|
|
|
2021-10-20 08:01:19 -07:00
|
|
|
func (segment *Segment) cwdIncluded() bool {
|
2021-02-27 20:05:51 -08:00
|
|
|
value, ok := segment.Properties[IncludeFolders]
|
|
|
|
if !ok {
|
|
|
|
// IncludeFolders isn't specified, everything is included
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
list := parseStringArray(value)
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
// IncludeFolders is an empty array, everything is included
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-20 08:01:19 -07:00
|
|
|
return dirMatchesOneOf(segment.env, segment.env.getcwd(), list)
|
2021-02-27 20:05:51 -08:00
|
|
|
}
|
|
|
|
|
2021-10-20 08:01:19 -07:00
|
|
|
func (segment *Segment) cwdExcluded() bool {
|
2021-02-27 20:05:51 -08:00
|
|
|
value, ok := segment.Properties[ExcludeFolders]
|
|
|
|
if !ok {
|
|
|
|
value = segment.Properties[IgnoreFolders]
|
|
|
|
}
|
|
|
|
list := parseStringArray(value)
|
2021-10-20 08:01:19 -07:00
|
|
|
return dirMatchesOneOf(segment.env, segment.env.getcwd(), list)
|
2020-10-02 07:58:25 -07:00
|
|
|
}
|
|
|
|
|
2021-02-13 07:27:31 -08:00
|
|
|
func (segment *Segment) getColor(templates []string, defaultColor string) string {
|
|
|
|
if len(templates) == 0 {
|
|
|
|
return defaultColor
|
|
|
|
}
|
|
|
|
txtTemplate := &textTemplate{
|
|
|
|
Context: segment.writer,
|
2021-05-26 12:12:58 -07:00
|
|
|
Env: segment.env,
|
2021-02-13 07:27:31 -08:00
|
|
|
}
|
|
|
|
for _, template := range templates {
|
|
|
|
txtTemplate.Template = template
|
2021-04-11 06:24:03 -07:00
|
|
|
value, err := txtTemplate.render()
|
|
|
|
if err != nil || value == "" {
|
2021-02-13 07:27:31 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return defaultColor
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-13 07:27:31 -08:00
|
|
|
func (segment *Segment) foreground() string {
|
2021-11-26 01:37:33 -08:00
|
|
|
color := segment.Properties.getColor(ForegroundOverride, segment.Foreground)
|
2021-02-13 07:27:31 -08:00
|
|
|
return segment.getColor(segment.ForegroundTemplates, color)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (segment *Segment) background() string {
|
2021-11-26 01:37:33 -08:00
|
|
|
color := segment.Properties.getColor(BackgroundOverride, segment.Background)
|
2021-02-13 07:27:31 -08:00
|
|
|
return segment.getColor(segment.BackgroundTemplates, color)
|
|
|
|
}
|
|
|
|
|
2020-10-12 23:57:46 -07:00
|
|
|
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
|
2021-05-26 12:12:58 -07:00
|
|
|
segment.env = env
|
2019-03-13 04:14:30 -07:00
|
|
|
functions := map[SegmentType]SegmentWriter{
|
2021-08-15 12:11:02 -07:00
|
|
|
OWM: &owm{},
|
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{},
|
2020-10-25 08:32:14 -07:00
|
|
|
Ruby: &ruby{},
|
2021-02-07 01:55:09 -08:00
|
|
|
Aws: &aws{},
|
2021-03-17 00:16:19 -07:00
|
|
|
Java: &java{},
|
2021-03-27 09:04:09 -07:00
|
|
|
PoshGit: &poshgit{},
|
2021-04-02 10:36:50 -07:00
|
|
|
AZFunc: &azfunc{},
|
2021-05-14 04:39:49 -07:00
|
|
|
Crystal: &crystal{},
|
2021-05-14 12:26:26 -07:00
|
|
|
Dart: &dart{},
|
2021-06-04 10:27:39 -07:00
|
|
|
Nbgv: &nbgv{},
|
2021-07-04 13:53:10 -07:00
|
|
|
Rust: &rust{},
|
2021-11-13 14:46:06 -08:00
|
|
|
SysInfo: &sysinfo{},
|
2021-10-13 00:05:29 -07:00
|
|
|
Angular: &angular{},
|
2021-10-27 01:52:56 -07:00
|
|
|
PHP: &php{},
|
2021-11-23 01:34:35 -08:00
|
|
|
Nightscout: &nightscout{},
|
2021-11-22 16:32:05 -08:00
|
|
|
WiFi: &wifi{},
|
2021-11-24 04:47:30 -08:00
|
|
|
WinReg: &winreg{},
|
2019-03-13 04:14:30 -07:00
|
|
|
}
|
|
|
|
if writer, ok := functions[segment.Type]; ok {
|
2021-11-26 01:37:33 -08:00
|
|
|
writer.init(segment.Properties, env)
|
2019-03-13 04:14:30 -07:00
|
|
|
segment.writer = writer
|
2020-10-12 23:57:46 -07:00
|
|
|
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
|
|
|
|
2021-10-20 08:01:19 -07:00
|
|
|
func (segment *Segment) setStringValue(env environmentInfo) {
|
2020-12-30 13:01:39 -08:00
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// display a message explaining omp failed(with the err)
|
2021-11-11 23:40:08 -08:00
|
|
|
message := fmt.Sprintf("\noh-my-posh fatal error rendering %s segment:%s\n\n%s\n", segment.Type, err, debug.Stack())
|
2020-12-30 13:01:39 -08:00
|
|
|
fmt.Println(message)
|
|
|
|
segment.stringValue = "error"
|
|
|
|
segment.active = true
|
|
|
|
}()
|
2020-11-14 23:47:00 -08:00
|
|
|
err := segment.mapSegmentWithWriter(env)
|
2021-10-20 08:01:19 -07:00
|
|
|
if err != nil || !segment.shouldIncludeFolder() {
|
2020-11-14 23:47:00 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if segment.enabled() {
|
|
|
|
segment.stringValue = segment.string()
|
|
|
|
}
|
|
|
|
}
|