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 (
"strings"
"sync"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
"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() {
if b.env.Flags().Debug {
return
}
if shouldHideForWidth(b.env, b.MinWidth, b.MaxWidth) {
return
}
@ -266,29 +262,3 @@ func (b *Block) getPowerlineColor() string {
}
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)
}
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
titleStartTime := time.Now()
e.Env.Debug("Segment: Title")
title := e.getTitleTemplateText()
consoleTitleTiming := &SegmentTiming{
consoleTitle := &Segment{
name: "ConsoleTitle",
nameLength: 12,
active: len(e.Config.ConsoleTitleTemplate) > 0,
Enabled: len(e.Config.ConsoleTitleTemplate) > 0,
text: title,
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 {
block.Init(e.Env, e.Writer)
longestSegmentName, timings := block.Debug()
segmentTimings = append(segmentTimings, timings...)
if longestSegmentName > largestSegmentNameLength {
largestSegmentNameLength = longestSegmentName
for _, segment := range block.Segments {
segments = append(segments, segment)
if segment.nameLength > largestSegmentNameLength {
largestSegmentNameLength = segment.nameLength
}
}
}
// 22 is the color for false/true and 7 is the reset color
largestSegmentNameLength += 22 + 7
for _, segment := range segmentTimings {
for _, segment := range segments {
duration := segment.duration.Milliseconds()
var active log.Text
if segment.active {
if segment.Enabled {
active = log.Text("true").Yellow()
} else {
active = log.Text("false").Purple()
}
segmentName := fmt.Sprintf("%s(%s)", segment.name, active.Plain())
e.write(fmt.Sprintf("%-*s - %3d ms - %s\n", largestSegmentNameLength, segmentName, duration, segment.text))
segmentName := fmt.Sprintf("%s(%s)", segment.Name(), active.Plain())
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)))

View file

@ -49,15 +49,10 @@ type Segment struct {
text string
styleCache SegmentStyle
name string
}
// SegmentTiming holds the timing context for a segment
type SegmentTiming struct {
name string
nameLength int
active bool
text string
// debug info
duration time.Duration
nameLength int
}
// 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)
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)
if err != nil || !segment.shouldIncludeFolder() {
return
}
segment.env.DebugF("Segment: %s", segment.Name())
// validate toggles
if toggles, OK := segment.env.Cache().Get(platform.TOGGLECACHE); OK && len(toggles) > 0 {
list := strings.Split(toggles, ",")
@ -493,9 +502,11 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
}
}
}
if shouldHideForWidth(segment.env, segment.MinWidth, segment.MaxWidth) {
return
}
if segment.writer.Enabled() {
segment.Enabled = true
env.TemplateCache().AddSegmentData(segment.Name(), segment.writer)