feat(debug): use actual prompt logic

This commit is contained in:
Jan De Dobbeleer 2023-08-06 15:37:25 +02:00 committed by Jan De Dobbeleer
parent 744b1f042c
commit 4bab59fbd8
3 changed files with 39 additions and 55 deletions

View file

@ -3,7 +3,6 @@ package engine
import ( import (
"strings" "strings"
"sync" "sync"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/ansi" "github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
@ -77,9 +76,6 @@ func (b *Block) InitPlain(env platform.Environment, config *Config) {
} }
func (b *Block) executeSegmentLogic() { func (b *Block) executeSegmentLogic() {
if b.env.Flags().Debug {
return
}
if shouldHideForWidth(b.env, b.MinWidth, b.MaxWidth) { if shouldHideForWidth(b.env, b.MinWidth, b.MaxWidth) {
return return
} }
@ -266,29 +262,3 @@ func (b *Block) getPowerlineColor() string {
} }
return b.previousActiveSegment.background() return b.previousActiveSegment.background()
} }
func (b *Block) Debug() (int, []*SegmentTiming) {
var segmentTimings []*SegmentTiming
largestSegmentNameLength := 0
for _, segment := range b.Segments {
var segmentTiming SegmentTiming
segmentTiming.name = string(segment.Type)
segmentTiming.nameLength = len(segmentTiming.name)
if segmentTiming.nameLength > largestSegmentNameLength {
largestSegmentNameLength = segmentTiming.nameLength
}
b.env.DebugF("Segment: %s", segmentTiming.name)
start := time.Now()
segment.SetEnabled(b.env)
segment.SetText()
segmentTiming.active = segment.Enabled
if segmentTiming.active || segment.style() == Accordion {
b.setActiveSegment(segment)
b.renderActiveSegment()
segmentTiming.text, _ = b.writer.String()
}
segmentTiming.duration = time.Since(start)
segmentTimings = append(segmentTimings, &segmentTiming)
}
return largestSegmentNameLength, segmentTimings
}

View file

@ -16,47 +16,50 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
sh += fmt.Sprintf(" (%s)", shellVersion) sh += fmt.Sprintf(" (%s)", shellVersion)
} }
e.write(fmt.Sprintf("\n%s %s\n", log.Text("Shell:").Green().Bold().Plain(), sh)) e.write(fmt.Sprintf("\n%s %s\n", log.Text("Shell:").Green().Bold().Plain(), sh))
e.write(log.Text("\nSegments:\n\n").Green().Bold().Plain().String())
// console title timing // console title timing
titleStartTime := time.Now() titleStartTime := time.Now()
e.Env.Debug("Segment: Title") e.Env.Debug("Segment: Title")
title := e.getTitleTemplateText() title := e.getTitleTemplateText()
consoleTitleTiming := &SegmentTiming{ consoleTitle := &Segment{
name: "ConsoleTitle", name: "ConsoleTitle",
nameLength: 12, nameLength: 12,
active: len(e.Config.ConsoleTitleTemplate) > 0, Enabled: len(e.Config.ConsoleTitleTemplate) > 0,
text: title, text: title,
duration: time.Since(titleStartTime), duration: time.Since(titleStartTime),
} }
largestSegmentNameLength := consoleTitleTiming.nameLength largestSegmentNameLength := consoleTitle.nameLength
// render prompt
e.write(log.Text("\nPrompt:\n\n").Green().Bold().Plain().String())
e.write(e.Primary())
e.write(log.Text("\n\nSegments:\n\n").Green().Bold().Plain().String())
var segments []*Segment
segments = append(segments, consoleTitle)
var segmentTimings []*SegmentTiming
segmentTimings = append(segmentTimings, consoleTitleTiming)
// cache a pointer to the color cycle
cycle = &e.Config.Cycle
// loop each segments of each blocks
for _, block := range e.Config.Blocks { for _, block := range e.Config.Blocks {
block.Init(e.Env, e.Writer) for _, segment := range block.Segments {
longestSegmentName, timings := block.Debug() segments = append(segments, segment)
segmentTimings = append(segmentTimings, timings...) if segment.nameLength > largestSegmentNameLength {
if longestSegmentName > largestSegmentNameLength { largestSegmentNameLength = segment.nameLength
largestSegmentNameLength = longestSegmentName }
} }
} }
// 22 is the color for false/true and 7 is the reset color // 22 is the color for false/true and 7 is the reset color
largestSegmentNameLength += 22 + 7 largestSegmentNameLength += 22 + 7
for _, segment := range segmentTimings { for _, segment := range segments {
duration := segment.duration.Milliseconds() duration := segment.duration.Milliseconds()
var active log.Text var active log.Text
if segment.active { if segment.Enabled {
active = log.Text("true").Yellow() active = log.Text("true").Yellow()
} else { } else {
active = log.Text("false").Purple() active = log.Text("false").Purple()
} }
segmentName := fmt.Sprintf("%s(%s)", segment.name, active.Plain()) segmentName := fmt.Sprintf("%s(%s)", segment.Name(), active.Plain())
e.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.text)) e.write(fmt.Sprintf("%-*s - %3d ms\n", largestSegmentNameLength, segmentName, duration))
} }
e.write(fmt.Sprintf("\n%s %s\n", log.Text("Run duration:").Green().Bold().Plain(), time.Since(startTime))) e.write(fmt.Sprintf("\n%s %s\n", log.Text("Run duration:").Green().Bold().Plain(), time.Since(startTime)))

View file

@ -49,15 +49,10 @@ type Segment struct {
text string text string
styleCache SegmentStyle styleCache SegmentStyle
name string name string
}
// SegmentTiming holds the timing context for a segment // debug info
type SegmentTiming struct {
name string
nameLength int
active bool
text string
duration time.Duration duration time.Duration
nameLength int
} }
// SegmentWriter is the interface used to define what and if to write to the prompt // SegmentWriter is the interface used to define what and if to write to the prompt
@ -480,10 +475,24 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
fmt.Println(message) fmt.Println(message)
segment.Enabled = true segment.Enabled = true
}() }()
// segment timings for debug purposes
var start time.Time
if env.Flags().Debug {
start = time.Now()
segment.nameLength = len(segment.Name())
defer func() {
segment.duration = time.Since(start)
}()
}
err := segment.mapSegmentWithWriter(env) err := segment.mapSegmentWithWriter(env)
if err != nil || !segment.shouldIncludeFolder() { if err != nil || !segment.shouldIncludeFolder() {
return return
} }
segment.env.DebugF("Segment: %s", segment.Name())
// validate toggles // validate toggles
if toggles, OK := segment.env.Cache().Get(platform.TOGGLECACHE); OK && len(toggles) > 0 { if toggles, OK := segment.env.Cache().Get(platform.TOGGLECACHE); OK && len(toggles) > 0 {
list := strings.Split(toggles, ",") list := strings.Split(toggles, ",")
@ -493,9 +502,11 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
} }
} }
} }
if shouldHideForWidth(segment.env, segment.MinWidth, segment.MaxWidth) { if shouldHideForWidth(segment.env, segment.MinWidth, segment.MaxWidth) {
return return
} }
if segment.writer.Enabled() { if segment.writer.Enabled() {
segment.Enabled = true segment.Enabled = true
env.TemplateCache().AddSegmentData(segment.Name(), segment.writer) env.TemplateCache().AddSegmentData(segment.Name(), segment.writer)