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, Style: Diamond,
LeadingDiamond: "\ue0b6", LeadingDiamond: "\ue0b6",
TrailingDiamond: "\ue0b0", TrailingDiamond: "\ue0b0",
Set: color.Set{Foreground: "p:black", Background: "p:yellow"}, Foreground: "p:black",
Background: "p:yellow",
Template: " {{ if .SSHSession }}\ueba9 {{ end }}{{ .UserName }} ", Template: " {{ if .SSHSession }}\ueba9 {{ end }}{{ .UserName }} ",
}, },
{ {
Type: PATH, Type: PATH,
Style: Powerline, Style: Powerline,
PowerlineSymbol: "\ue0b0", PowerlineSymbol: "\ue0b0",
Set: color.Set{Foreground: "p:white", Background: "p:orange"}, Foreground: "p:white",
Background: "p:orange",
Properties: properties.Map{ Properties: properties.Map{
properties.Style: "folder", properties.Style: "folder",
}, },
@ -44,7 +46,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Type: GIT, Type: GIT,
Style: Powerline, Style: Powerline,
PowerlineSymbol: "\ue0b0", PowerlineSymbol: "\ue0b0",
Set: color.Set{Foreground: "p:black", Background: "p:green"}, Foreground: "p:black",
Background: "p:green",
BackgroundTemplates: []string{ BackgroundTemplates: []string{
"{{ if or (.Working.Changed) (.Staging.Changed) }}p:yellow{{ end }}", "{{ if or (.Working.Changed) (.Staging.Changed) }}p:yellow{{ end }}",
"{{ if and (gt .Ahead 0) (gt .Behind 0) }}p:red{{ 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, Type: ROOT,
Style: Powerline, Style: Powerline,
PowerlineSymbol: "\ue0b0", PowerlineSymbol: "\ue0b0",
Set: color.Set{Foreground: "p:white", Background: "p:yellow"}, Foreground: "p:white",
Background: "p:yellow",
Template: " \uf0e7 ", Template: " \uf0e7 ",
}, },
{ {
@ -75,7 +79,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond, Style: Diamond,
LeadingDiamond: "<transparent,background>\ue0b0</>", LeadingDiamond: "<transparent,background>\ue0b0</>",
TrailingDiamond: "\ue0b4", TrailingDiamond: "\ue0b4",
Set: color.Set{Foreground: "p:white", Background: "p:blue"}, Foreground: "p:white",
Background: "p:blue",
BackgroundTemplates: []string{ BackgroundTemplates: []string{
exitBackgroundTemplate, exitBackgroundTemplate,
}, },
@ -92,7 +97,8 @@ func Default(env runtime.Environment, warning bool) *Config {
{ {
Type: NODE, Type: NODE,
Style: Plain, Style: Plain,
Set: color.Set{Foreground: "p:green", Background: "transparent"}, Foreground: "p:green",
Background: "transparent",
Template: "\ue718 ", Template: "\ue718 ",
Properties: properties.Map{ Properties: properties.Map{
segments.HomeEnabled: false, segments.HomeEnabled: false,
@ -103,7 +109,8 @@ func Default(env runtime.Environment, warning bool) *Config {
{ {
Type: GOLANG, Type: GOLANG,
Style: Plain, Style: Plain,
Set: color.Set{Foreground: "p:blue", Background: "transparent"}, Foreground: "p:blue",
Background: "transparent",
Template: "\ue626 ", Template: "\ue626 ",
Properties: properties.Map{ Properties: properties.Map{
properties.FetchVersion: false, properties.FetchVersion: false,
@ -112,7 +119,8 @@ func Default(env runtime.Environment, warning bool) *Config {
{ {
Type: PYTHON, Type: PYTHON,
Style: Plain, Style: Plain,
Set: color.Set{Foreground: "p:yellow", Background: "transparent"}, Foreground: "p:yellow",
Background: "transparent",
Template: "\ue235 ", Template: "\ue235 ",
Properties: properties.Map{ Properties: properties.Map{
properties.FetchVersion: false, properties.FetchVersion: false,
@ -123,13 +131,15 @@ func Default(env runtime.Environment, warning bool) *Config {
{ {
Type: SHELL, Type: SHELL,
Style: Plain, Style: Plain,
Set: color.Set{Foreground: "p:white", Background: "transparent"}, Foreground: "p:white",
Background: "transparent",
Template: "in <p:blue><b>{{ .Name }}</b></> ", Template: "in <p:blue><b>{{ .Name }}</b></> ",
}, },
{ {
Type: TIME, Type: TIME,
Style: Plain, Style: Plain,
Set: color.Set{Foreground: "p:white", Background: "transparent"}, Foreground: "p:white",
Background: "transparent",
Template: "at <p:blue><b>{{ .CurrentDate | date \"15:04:05\" }}</b></>", Template: "at <p:blue><b>{{ .CurrentDate | date \"15:04:05\" }}</b></>",
}, },
}, },
@ -146,11 +156,13 @@ func Default(env runtime.Environment, warning bool) *Config {
"yellow": "#F3AE35", "yellow": "#F3AE35",
}, },
SecondaryPrompt: &Segment{ SecondaryPrompt: &Segment{
Set: color.Set{Foreground: "p:black", Background: "transparent"}, Foreground: "p:black",
Background: "transparent",
Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> > </><p:yellow,transparent>\ue0b0</> ", Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> > </><p:yellow,transparent>\ue0b0</> ",
}, },
TransientPrompt: &Segment{ TransientPrompt: &Segment{
Set: color.Set{Foreground: "p:black", Background: "transparent"}, Foreground: "p:black",
Background: "transparent",
Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> {{ .Folder }} </><p:yellow,transparent>\ue0b0</> ", Template: "<p:yellow,transparent>\ue0b6</><,p:yellow> {{ .Folder }} </><p:yellow,transparent>\ue0b0</> ",
}, },
Tooltips: []*Segment{ Tooltips: []*Segment{
@ -159,7 +171,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond, Style: Diamond,
LeadingDiamond: "\ue0b0", LeadingDiamond: "\ue0b0",
TrailingDiamond: "\ue0b4", TrailingDiamond: "\ue0b4",
Set: color.Set{Foreground: "p:white", Background: "p:orange"}, Foreground: "p:white",
Background: "p:orange",
Template: " \ue7ad {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} ", Template: " \ue7ad {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} ",
Properties: properties.Map{ Properties: properties.Map{
properties.DisplayDefault: true, properties.DisplayDefault: true,
@ -171,7 +184,8 @@ func Default(env runtime.Environment, warning bool) *Config {
Style: Diamond, Style: Diamond,
LeadingDiamond: "\ue0b0", LeadingDiamond: "\ue0b0",
TrailingDiamond: "\ue0b4", TrailingDiamond: "\ue0b4",
Set: color.Set{Foreground: "p:white", Background: "p:blue"}, Foreground: "p:white",
Background: "p:blue",
Template: " \uebd8 {{ .Name }} ", Template: " \uebd8 {{ .Name }} ",
Properties: properties.Map{ Properties: properties.Map{
properties.DisplayDefault: true, properties.DisplayDefault: true,

View file

@ -53,7 +53,9 @@ type Segment struct {
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"` MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"` MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
Filler string `json:"filler,omitempty" toml:"filler,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:"-"` Enabled bool `json:"-" toml:"-"`
@ -214,19 +216,19 @@ func (segment *Segment) cwdExcluded() bool {
func (segment *Segment) ResolveForeground() color.Ansi { func (segment *Segment) ResolveForeground() color.Ansi {
if len(segment.ForegroundTemplates) != 0 { if len(segment.ForegroundTemplates) != 0 {
match := segment.ForegroundTemplates.FirstMatch(segment.writer, segment.env, segment.Foreground.String()) 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 { func (segment *Segment) ResolveBackground() color.Ansi {
if len(segment.BackgroundTemplates) != 0 { if len(segment.BackgroundTemplates) != 0 {
match := segment.BackgroundTemplates.FirstMatch(segment.writer, segment.env, segment.Background.String()) 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 { 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 { for i, segment := range block.Segments {
if colors, newCycle := cycle.Loop(); colors != nil { if colors, newCycle := cycle.Loop(); colors != nil {
cycle = &newCycle cycle = &newCycle
segment.Set = *colors segment.Foreground = colors.Foreground
segment.Background = colors.Background
} }
if i == 0 && len(block.LeadingDiamond) > 0 { if i == 0 && len(block.LeadingDiamond) > 0 {