refactor: display powerline correct in --debug

This commit is contained in:
Jan De Dobbeleer 2020-12-31 14:00:46 +01:00 committed by Jan De Dobbeleer
parent 488d25fc7b
commit 07ac2c35cd

View file

@ -25,8 +25,6 @@ type SegmentTiming struct {
stringValue string stringValue string
enabledDuration time.Duration enabledDuration time.Duration
stringDuration time.Duration stringDuration time.Duration
background string
foreground string
} }
func (e *engine) getPowerlineColor(foreground bool) string { func (e *engine) getPowerlineColor(foreground bool) string {
@ -170,8 +168,8 @@ func (e *engine) render() {
// debug will lool through your config file and output the timings for each segments // debug will lool through your config file and output the timings for each segments
func (e *engine) debug() { func (e *engine) debug() {
var segmentTimings []SegmentTiming var segmentTimings []SegmentTiming
nameMaxLength := 0 largestSegmentNameLength := 0
e.renderer.print("\nHere are the timings of segments in your prompt:\n") e.renderer.print("\n\x1b[1mHere are the timings of segments in your prompt:\x1b[0m\n\n")
// loop each segments of each blocks // loop each segments of each blocks
for _, block := range e.settings.Blocks { for _, block := range e.settings.Blocks {
for _, segment := range block.Segments { for _, segment := range block.Segments {
@ -179,56 +177,45 @@ func (e *engine) debug() {
if err != nil || segment.shouldIgnoreFolder(e.env.getcwd()) { if err != nil || segment.shouldIgnoreFolder(e.env.getcwd()) {
return return
} }
var segmentTiming SegmentTiming var segmentTiming SegmentTiming
segmentTiming.name = string(segment.Type) segmentTiming.name = string(segment.Type)
segmentTiming.nameLength = len(segmentTiming.name) segmentTiming.nameLength = len(segmentTiming.name)
if segmentTiming.nameLength > largestSegmentNameLength {
if segmentTiming.nameLength > nameMaxLength { largestSegmentNameLength = segmentTiming.nameLength
nameMaxLength = segmentTiming.nameLength
} }
// enabled() timing
segmentTiming.background = segment.Background
segmentTiming.foreground = segment.Foreground
// enabled timing
start := time.Now() start := time.Now()
segmentTiming.enabled = segment.enabled() segmentTiming.enabled = segment.enabled()
segmentTiming.enabledDuration = time.Since(start) segmentTiming.enabledDuration = time.Since(start)
// string() timing
// string timing
if segmentTiming.enabled { if segmentTiming.enabled {
start = time.Now() start = time.Now()
segmentTiming.stringValue = segment.string() segmentTiming.stringValue = segment.string()
segmentTiming.stringDuration = time.Since(start) segmentTiming.stringDuration = time.Since(start)
e.previousActiveSegment = nil
// not pretty rendering could be refactored for a better separation of concern
e.activeSegment = segment e.activeSegment = segment
e.endPowerline()
e.activeSegment.Background = segment.props.background e.activeSegment.Background = segment.props.background
e.activeSegment.Foreground = segment.props.foreground e.activeSegment.Foreground = segment.props.foreground
e.renderSegmentText(segmentTiming.stringValue) e.renderSegmentText(segmentTiming.stringValue)
if e.activeSegment.Style == Powerline {
e.writePowerLineSeparator(Transparent, e.activeSegment.Background, true)
}
segmentTiming.stringValue = e.color.string() segmentTiming.stringValue = e.color.string()
e.color.buffer.Reset() e.color.buffer.Reset()
} }
segmentTimings = append(segmentTimings, segmentTiming) segmentTimings = append(segmentTimings, segmentTiming)
} }
} }
// pad the output so the tabs render correctly
// 7 => (false) largestSegmentNameLength += 7
nameMaxLength += 7
for _, segment := range segmentTimings { for _, segment := range segmentTimings {
duration := segment.enabledDuration.Milliseconds() duration := segment.enabledDuration.Milliseconds()
if segment.enabled { if segment.enabled {
duration += segment.stringDuration.Milliseconds() duration += segment.stringDuration.Milliseconds()
} }
e.renderer.print(fmt.Sprintf("%-*s - %3d ms - %s\n", nameMaxLength, fmt.Sprintf("%s(%t)", segment.name, segment.enabled), segmentName := fmt.Sprintf("%s(%t)", segment.name, segment.enabled)
duration, segment.stringValue)) e.renderer.print(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.stringValue))
} }
fmt.Print(e.renderer.string()) fmt.Print(e.renderer.string())
} }