fix(yaml): parse colors correctly

This commit is contained in:
Jan De Dobbeleer 2024-07-04 21:49:40 +02:00 committed by Jan De Dobbeleer
parent 45472c82d2
commit 23b314879b
3 changed files with 54 additions and 37 deletions

View file

@ -27,14 +27,16 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond,
LeadingDiamond: "\ue0b6",
TrailingDiamond: "\ue0b0",
Set: color.Set{Foreground: "p:black", Background: "p:yellow"},
Foreground: "p:black",
Background: "p:yellow",
Template: " {{ if .SSHSession }}\ueba9 {{ end }}{{ .UserName }} ",
},
{
Type: PATH,
Style: Powerline,
PowerlineSymbol: "\ue0b0",
Set: color.Set{Foreground: "p:white", Background: "p:orange"},
Foreground: "p:white",
Background: "p:orange",
Properties: properties.Map{
properties.Style: "folder",
},
@ -44,7 +46,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Type: GIT,
Style: Powerline,
PowerlineSymbol: "\ue0b0",
Set: color.Set{Foreground: "p:black", Background: "p:green"},
Foreground: "p:black",
Background: "p:green",
BackgroundTemplates: []string{
"{{ if or (.Working.Changed) (.Staging.Changed) }}p:yellow{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}p:red{{ end }}",
@ -67,7 +70,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Type: ROOT,
Style: Powerline,
PowerlineSymbol: "\ue0b0",
Set: color.Set{Foreground: "p:white", Background: "p:yellow"},
Foreground: "p:white",
Background: "p:yellow",
Template: " \uf0e7 ",
},
{
@ -75,7 +79,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond,
LeadingDiamond: "<transparent,background>\ue0b0</>",
TrailingDiamond: "\ue0b4",
Set: color.Set{Foreground: "p:white", Background: "p:blue"},
Foreground: "p:white",
Background: "p:blue",
BackgroundTemplates: []string{
exitBackgroundTemplate,
},
@ -90,10 +95,11 @@ func Default(env runtime.Environment, warning bool) *Config {
Type: RPrompt,
Segments: []*Segment{
{
Type: NODE,
Style: Plain,
Set: color.Set{Foreground: "p:green", Background: "transparent"},
Template: "\ue718 ",
Type: NODE,
Style: Plain,
Foreground: "p:green",
Background: "transparent",
Template: "\ue718 ",
Properties: properties.Map{
segments.HomeEnabled: false,
segments.FetchPackageManager: false,
@ -101,19 +107,21 @@ func Default(env runtime.Environment, warning bool) *Config {
},
},
{
Type: GOLANG,
Style: Plain,
Set: color.Set{Foreground: "p:blue", Background: "transparent"},
Template: "\ue626 ",
Type: GOLANG,
Style: Plain,
Foreground: "p:blue",
Background: "transparent",
Template: "\ue626 ",
Properties: properties.Map{
properties.FetchVersion: false,
},
},
{
Type: PYTHON,
Style: Plain,
Set: color.Set{Foreground: "p:yellow", Background: "transparent"},
Template: "\ue235 ",
Type: PYTHON,
Style: Plain,
Foreground: "p:yellow",
Background: "transparent",
Template: "\ue235 ",
Properties: properties.Map{
properties.FetchVersion: false,
segments.DisplayMode: "files",
@ -121,16 +129,18 @@ func Default(env runtime.Environment, warning bool) *Config {
},
},
{
Type: SHELL,
Style: Plain,
Set: color.Set{Foreground: "p:white", Background: "transparent"},
Template: "in <p:blue><b>{{ .Name }}</b></> ",
Type: SHELL,
Style: Plain,
Foreground: "p:white",
Background: "transparent",
Template: "in <p:blue><b>{{ .Name }}</b></> ",
},
{
Type: TIME,
Style: Plain,
Set: color.Set{Foreground: "p:white", Background: "transparent"},
Template: "at <p:blue><b>{{ .CurrentDate | date \"15:04:05\" }}</b></>",
Type: TIME,
Style: Plain,
Foreground: "p:white",
Background: "transparent",
Template: "at <p:blue><b>{{ .CurrentDate | date \"15:04:05\" }}</b></>",
},
},
},
@ -146,12 +156,14 @@ func Default(env runtime.Environment, warning bool) *Config {
"yellow": "#F3AE35",
},
SecondaryPrompt: &Segment{
Set: color.Set{Foreground: "p:black", Background: "transparent"},
Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> > </><p:yellow,transparent>\ue0b0</> ",
Foreground: "p:black",
Background: "transparent",
Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> > </><p:yellow,transparent>\ue0b0</> ",
},
TransientPrompt: &Segment{
Set: color.Set{Foreground: "p:black", Background: "transparent"},
Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> {{ .Folder }} </><p:yellow,transparent>\ue0b0</> ",
Foreground: "p:black",
Background: "transparent",
Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> {{ .Folder }} </><p:yellow,transparent>\ue0b0</> ",
},
Tooltips: []*Segment{
{
@ -159,7 +171,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond,
LeadingDiamond: "\ue0b0",
TrailingDiamond: "\ue0b4",
Set: color.Set{Foreground: "p:white", Background: "p:orange"},
Foreground: "p:white",
Background: "p:orange",
Template: " \ue7ad {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} ",
Properties: properties.Map{
properties.DisplayDefault: true,
@ -171,7 +184,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond,
LeadingDiamond: "\ue0b0",
TrailingDiamond: "\ue0b4",
Set: color.Set{Foreground: "p:white", Background: "p:blue"},
Foreground: "p:white",
Background: "p:blue",
Template: " \uebd8 {{ .Name }} ",
Properties: properties.Map{
properties.DisplayDefault: true,

View file

@ -53,7 +53,9 @@ type Segment struct {
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
color.Set
Background color.Ansi `json:"background" toml:"background"`
Foreground color.Ansi `json:"foreground" toml:"foreground"`
// color.Set
Enabled bool `json:"-" toml:"-"`
@ -214,19 +216,19 @@ func (segment *Segment) cwdExcluded() bool {
func (segment *Segment) ResolveForeground() color.Ansi {
if len(segment.ForegroundTemplates) != 0 {
match := segment.ForegroundTemplates.FirstMatch(segment.writer, segment.env, segment.Foreground.String())
segment.Set.Foreground = color.Ansi(match)
segment.Foreground = color.Ansi(match)
}
return segment.Set.Foreground
return segment.Foreground
}
func (segment *Segment) ResolveBackground() color.Ansi {
if len(segment.BackgroundTemplates) != 0 {
match := segment.BackgroundTemplates.FirstMatch(segment.writer, segment.env, segment.Background.String())
segment.Set.Background = color.Ansi(match)
segment.Background = color.Ansi(match)
}
return segment.Set.Background
return segment.Background
}
func (segment *Segment) ResolveStyle() SegmentStyle {

View file

@ -306,7 +306,8 @@ func (e *Engine) renderBlockSegments(block *config.Block) (string, int) {
for i, segment := range block.Segments {
if colors, newCycle := cycle.Loop(); colors != nil {
cycle = &newCycle
segment.Set = *colors
segment.Foreground = colors.Foreground
segment.Background = colors.Background
}
if i == 0 && len(block.LeadingDiamond) > 0 {