refactor: cleaner segment render logic

This commit is contained in:
Jan De Dobbeleer 2021-12-16 10:57:28 +01:00 committed by Jan De Dobbeleer
parent df78bad3b5
commit 1282cb24d8

View file

@ -95,41 +95,42 @@ func (b *Block) renderSegments() string {
if !segment.active { if !segment.active {
continue continue
} }
b.setActiveSegment(segment) b.renderSegment(segment)
b.writer.setColors(b.activeBackground, b.activeForeground)
b.endPowerline()
b.renderSegmentText(segment.stringValue)
}
if b.previousActiveSegment != nil && b.previousActiveSegment.Style == Powerline {
b.writePowerLineSeparator(Transparent, b.previousActiveSegment.background(), true)
} }
b.writePowerline(true)
b.writer.clearParentColors() b.writer.clearParentColors()
return b.writer.string() return b.writer.string()
} }
func (b *Block) endPowerline() { func (b *Block) writePowerline(end bool) {
if b.previousActiveSegment == nil || b.activeSegment == nil { resolvePowerlineSymbol := func() string {
var symbol string
if b.previousActiveSegment != nil && b.previousActiveSegment.Style == Powerline {
symbol = b.previousActiveSegment.PowerlineSymbol
} else if b.activeSegment.Style == Powerline {
symbol = b.activeSegment.PowerlineSymbol
}
return symbol
}
symbol := resolvePowerlineSymbol()
if len(symbol) == 0 {
return return
} }
if b.activeSegment.Style != Powerline && background := b.activeSegment.background()
b.previousActiveSegment.Style == Powerline { if end || b.activeSegment.Style != Powerline {
b.writePowerLineSeparator(b.getPowerlineColor(false), b.previousActiveSegment.background(), true) background = Transparent
} }
} if b.activeSegment.Style == Diamond && len(b.activeSegment.LeadingDiamond) == 0 {
background = b.activeSegment.background()
func (b *Block) writePowerLineSeparator(background, foreground string, end bool) {
symbol := b.activeSegment.PowerlineSymbol
if end {
symbol = b.previousActiveSegment.PowerlineSymbol
} }
if b.activeSegment.InvertPowerline { if b.activeSegment.InvertPowerline {
b.writer.write(foreground, background, symbol) b.writer.write(b.getPowerlineColor(), background, symbol)
return return
} }
b.writer.write(background, foreground, symbol) b.writer.write(background, b.getPowerlineColor(), symbol)
} }
func (b *Block) getPowerlineColor(foreground bool) string { func (b *Block) getPowerlineColor() string {
if b.previousActiveSegment == nil { if b.previousActiveSegment == nil {
return Transparent return Transparent
} }
@ -137,45 +138,30 @@ func (b *Block) getPowerlineColor(foreground bool) string {
return b.previousActiveSegment.background() return b.previousActiveSegment.background()
} }
if b.activeSegment.Style == Diamond && len(b.activeSegment.LeadingDiamond) == 0 { if b.activeSegment.Style == Diamond && len(b.activeSegment.LeadingDiamond) == 0 {
return b.activeBackground return b.previousActiveSegment.background()
} }
if !foreground && b.activeSegment.Style != Powerline { if b.previousActiveSegment.Style != Powerline {
return Transparent
}
if foreground && b.previousActiveSegment.Style != Powerline {
return Transparent return Transparent
} }
return b.previousActiveSegment.background() return b.previousActiveSegment.background()
} }
func (b *Block) renderSegmentText(text string) { func (b *Block) renderSegment(segment *Segment) {
b.setActiveSegment(segment)
b.writePowerline(false)
b.writer.setColors(b.activeBackground, b.activeForeground)
switch b.activeSegment.Style { switch b.activeSegment.Style {
case Plain: case Plain, Powerline:
b.renderPlainSegment(text) b.renderText(segment.stringValue)
case Diamond: case Diamond:
b.renderDiamondSegment(text) b.writer.write(Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
case Powerline: b.renderText(segment.stringValue)
b.renderPowerLineSegment(text) b.writer.write(Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
} }
b.previousActiveSegment = b.activeSegment b.previousActiveSegment = b.activeSegment
b.writer.setParentColors(b.activeBackground, b.activeForeground) b.writer.setParentColors(b.activeBackground, b.activeForeground)
} }
func (b *Block) renderPowerLineSegment(text string) {
b.writePowerLineSeparator(b.activeBackground, b.getPowerlineColor(true), false)
b.renderText(text)
}
func (b *Block) renderPlainSegment(text string) {
b.renderText(text)
}
func (b *Block) renderDiamondSegment(text string) {
b.writer.write(Transparent, b.activeBackground, b.activeSegment.LeadingDiamond)
b.renderText(text)
b.writer.write(Transparent, b.activeBackground, b.activeSegment.TrailingDiamond)
}
func (b *Block) renderText(text string) { func (b *Block) renderText(text string) {
defaultValue := " " defaultValue := " "
b.writer.write(b.activeBackground, b.activeForeground, b.activeSegment.getValue(Prefix, defaultValue)) b.writer.write(b.activeBackground, b.activeForeground, b.activeSegment.getValue(Prefix, defaultValue))
@ -204,13 +190,12 @@ func (b *Block) debug() (int, []*SegmentTiming) {
// string() timing // string() timing
if segmentTiming.enabled { if segmentTiming.enabled {
start = time.Now() start = time.Now()
segmentTiming.stringValue = segment.string() segment.stringValue = segment.string()
segmentTiming.stringDuration = time.Since(start) segmentTiming.stringDuration = time.Since(start)
b.previousActiveSegment = nil b.previousActiveSegment = nil
b.setActiveSegment(segment) b.renderSegment(segment)
b.renderSegmentText(segmentTiming.stringValue)
if b.activeSegment.Style == Powerline { if b.activeSegment.Style == Powerline {
b.writePowerLineSeparator(Transparent, b.activeBackground, true) b.writePowerline(false)
} }
segmentTiming.stringValue = b.writer.string() segmentTiming.stringValue = b.writer.string()
b.writer.reset() b.writer.reset()