Skip to content

Commit

Permalink
fix(yaml): parse colors correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jul 4, 2024
1 parent 45472c8 commit 23b3148
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 37 deletions.
76 changes: 45 additions & 31 deletions src/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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 }}",
Expand All @@ -67,15 +70,17 @@ 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 ",
},
{
Type: STATUS,
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,
},
Expand All @@ -90,47 +95,52 @@ 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,
segments.DisplayMode: "files",
},
},
{
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",
segments.FetchVirtualEnv: false,
},
},
{
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></>",
},
},
},
Expand All @@ -146,20 +156,23 @@ 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{
{
Type: AWS,
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,
Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions src/config/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/prompt/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 23b3148

Please sign in to comment.