refactor: use pointers to segment color

This commit is contained in:
Jan De Dobbeleer 2022-02-16 12:40:13 +01:00 committed by Jan De Dobbeleer
parent 1e2e65ee62
commit 8c8f3e411d
2 changed files with 26 additions and 21 deletions

View file

@ -41,8 +41,6 @@ type Block struct {
ansi *color.Ansi
activeSegment *Segment
previousActiveSegment *Segment
activeBackground string
activeForeground string
length int
}
@ -65,9 +63,7 @@ func (b *Block) initPlain(env environment.Environment, config *Config) {
func (b *Block) setActiveSegment(segment *Segment) {
b.activeSegment = segment
b.activeBackground = segment.background()
b.activeForeground = segment.foreground()
b.writer.SetColors(b.activeBackground, b.activeForeground)
b.writer.SetColors(segment.background(), segment.foreground())
}
func (b *Block) enabled() bool {
@ -112,14 +108,14 @@ func (b *Block) renderSegment(segment *Segment) {
b.writePowerline(false)
switch b.activeSegment.Style {
case Plain, Powerline:
b.writer.Write(b.activeBackground, b.activeForeground, segment.text)
b.writer.Write(color.Background, color.Foreground, segment.text)
case Diamond:
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
b.writer.Write(b.activeBackground, b.activeForeground, segment.text)
b.writer.Write(color.Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
b.writer.Write(color.Transparent, color.Background, b.activeSegment.LeadingDiamond)
b.writer.Write(color.Background, color.Foreground, segment.text)
b.writer.Write(color.Transparent, color.Background, b.activeSegment.TrailingDiamond)
}
b.previousActiveSegment = b.activeSegment
b.writer.SetParentColors(b.activeBackground, b.activeForeground)
b.writer.SetParentColors(b.previousActiveSegment.background(), b.previousActiveSegment.foreground())
}
func (b *Block) writePowerline(final bool) {
@ -136,18 +132,18 @@ func (b *Block) writePowerline(final bool) {
if len(symbol) == 0 {
return
}
background := b.activeSegment.background()
bgColor := color.Background
if final || b.activeSegment.Style != Powerline {
background = color.Transparent
bgColor = color.Transparent
}
if b.activeSegment.Style == Diamond && len(b.activeSegment.LeadingDiamond) == 0 {
background = b.activeSegment.background()
bgColor = color.Background
}
if b.activeSegment.InvertPowerline {
b.writer.Write(b.getPowerlineColor(), background, symbol)
b.writer.Write(b.getPowerlineColor(), bgColor, symbol)
return
}
b.writer.Write(background, b.getPowerlineColor(), symbol)
b.writer.Write(bgColor, b.getPowerlineColor(), symbol)
}
func (b *Block) getPowerlineColor() string {

View file

@ -26,10 +26,13 @@ type Segment struct {
LeadingDiamond string `json:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty"`
Properties properties.Map `json:"properties,omitempty"`
writer SegmentWriter
text string
active bool
env environment.Environment
writer SegmentWriter
text string
active bool
env environment.Environment
backgroundCache string
foregroundCache string
}
// SegmentTiming holds the timing context for a segment
@ -216,11 +219,17 @@ func (segment *Segment) shouldInvokeWithTip(tip string) bool {
}
func (segment *Segment) foreground() string {
return segment.getColor(segment.ForegroundTemplates, segment.Foreground)
if len(segment.foregroundCache) == 0 {
segment.foregroundCache = segment.getColor(segment.ForegroundTemplates, segment.Foreground)
}
return segment.foregroundCache
}
func (segment *Segment) background() string {
return segment.getColor(segment.BackgroundTemplates, segment.Background)
if len(segment.backgroundCache) == 0 {
segment.backgroundCache = segment.getColor(segment.BackgroundTemplates, segment.Background)
}
return segment.backgroundCache
}
func (segment *Segment) mapSegmentWithWriter(env environment.Environment) error {