fix: correct debug logic

relates to #2234
This commit is contained in:
Jan De Dobbeleer 2022-05-07 10:12:22 +02:00 committed by Jan De Dobbeleer
parent f8d0c00404
commit a7b0021179
14 changed files with 61 additions and 49 deletions

View file

@ -36,7 +36,7 @@ You can do the following:
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion, Version: cliVersion,
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
switch args[0] { switch args[0] {
case "path": case "path":

View file

@ -44,7 +44,7 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
Config: config, Config: config,
}, },
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
if len(output) == 0 { if len(output) == 0 {

View file

@ -54,7 +54,7 @@ Exports the config to an image file using customized output options.`,
Shell: shell.PLAIN, Shell: shell.PLAIN,
}, },
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
ansi := &color.Ansi{} ansi := &color.Ansi{}

View file

@ -44,7 +44,7 @@ A backup of the current config can be found at ~/myconfig.omp.json.bak.`,
Migrate: true, Migrate: true,
}, },
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
if write { if write {

View file

@ -23,9 +23,10 @@ var debugCmd = &cobra.Command{
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
CmdFlags: &environment.Flags{ CmdFlags: &environment.Flags{
Config: config, Config: config,
Debug: true,
}, },
} }
env.Init(true) env.Init()
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
ansi := &color.Ansi{} ansi := &color.Ansi{}

View file

@ -31,7 +31,7 @@ This command is used to get the value of the following variables:
env := &environment.ShellEnvironment{ env := &environment.ShellEnvironment{
Version: cliVersion, Version: cliVersion,
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
switch args[0] { switch args[0] {
case "millis": case "millis":

View file

@ -54,7 +54,7 @@ func runInit(shellName string) {
Strict: strict, Strict: strict,
}, },
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
if print { if print {
init := shell.PrintInit(env) init := shell.PrintInit(env)

View file

@ -61,7 +61,7 @@ var printCmd = &cobra.Command{
ShellVersion: shellVersion, ShellVersion: shellVersion,
}, },
} }
env.Init(false) env.Init()
defer env.Close() defer env.Close()
cfg := engine.LoadConfig(env) cfg := engine.LoadConfig(env)
ansi := &color.Ansi{} ansi := &color.Ansi{}

View file

@ -4,7 +4,6 @@ import (
"oh-my-posh/color" "oh-my-posh/color"
"oh-my-posh/environment" "oh-my-posh/environment"
"oh-my-posh/shell" "oh-my-posh/shell"
"strings"
"sync" "sync"
"time" "time"
) )
@ -49,8 +48,7 @@ func (b *Block) Init(env environment.Environment, writer color.Writer, ansi *col
b.env = env b.env = env
b.writer = writer b.writer = writer
b.ansi = ansi b.ansi = ansi
b.setEnabledSegments() b.executeSegmentLogic()
b.setSegmentsText()
} }
func (b *Block) InitPlain(env environment.Environment, config *Config) { func (b *Block) InitPlain(env environment.Environment, config *Config) {
@ -62,6 +60,13 @@ func (b *Block) InitPlain(env environment.Environment, config *Config) {
AnsiColors: config.MakeColors(env), AnsiColors: config.MakeColors(env),
} }
b.env = env b.env = env
b.executeSegmentLogic()
}
func (b *Block) executeSegmentLogic() {
if b.env.Flags().Debug {
return
}
b.setEnabledSegments() b.setEnabledSegments()
b.setSegmentsText() b.setSegmentsText()
} }
@ -76,7 +81,7 @@ func (b *Block) Enabled() bool {
return true return true
} }
for _, segment := range b.Segments { for _, segment := range b.Segments {
if segment.enabled { if segment.Enabled {
return true return true
} }
} }
@ -90,7 +95,7 @@ func (b *Block) setEnabledSegments() {
for _, segment := range b.Segments { for _, segment := range b.Segments {
go func(s *Segment) { go func(s *Segment) {
defer wg.Done() defer wg.Done()
s.setEnabled(b.env) s.SetEnabled(b.env)
}(segment) }(segment)
} }
} }
@ -102,19 +107,15 @@ func (b *Block) setSegmentsText() {
for _, segment := range b.Segments { for _, segment := range b.Segments {
go func(s *Segment) { go func(s *Segment) {
defer wg.Done() defer wg.Done()
if !s.enabled { s.SetText()
return
}
s.text = s.string()
s.enabled = len(strings.ReplaceAll(s.text, " ", "")) > 0
}(segment) }(segment)
} }
} }
func (b *Block) renderSegments() (string, int) { func (b *Block) RenderSegments() (string, int) {
defer b.writer.Reset() defer b.writer.Reset()
for _, segment := range b.Segments { for _, segment := range b.Segments {
if !segment.enabled && segment.Style != Accordion { if !segment.Enabled && segment.Style != Accordion {
continue continue
} }
b.setActiveSegment(segment) b.setActiveSegment(segment)
@ -135,7 +136,7 @@ func (b *Block) renderActiveSegment() {
b.writer.Write(color.Background, color.Foreground, b.activeSegment.text) b.writer.Write(color.Background, color.Foreground, b.activeSegment.text)
b.writer.Write(color.Transparent, color.Background, b.activeSegment.TrailingDiamond) b.writer.Write(color.Transparent, color.Background, b.activeSegment.TrailingDiamond)
case Accordion: case Accordion:
if b.activeSegment.enabled { if b.activeSegment.Enabled {
b.writer.Write(color.Background, color.Foreground, b.activeSegment.text) b.writer.Write(color.Background, color.Foreground, b.activeSegment.text)
} }
} }
@ -187,7 +188,7 @@ func (b *Block) getPowerlineColor() string {
return b.previousActiveSegment.background() return b.previousActiveSegment.background()
} }
func (b *Block) debug() (int, []*SegmentTiming) { func (b *Block) Debug() (int, []*SegmentTiming) {
var segmentTimings []*SegmentTiming var segmentTimings []*SegmentTiming
largestSegmentNameLength := 0 largestSegmentNameLength := 0
for _, segment := range b.Segments { for _, segment := range b.Segments {
@ -198,7 +199,9 @@ func (b *Block) debug() (int, []*SegmentTiming) {
largestSegmentNameLength = segmentTiming.nameLength largestSegmentNameLength = segmentTiming.nameLength
} }
start := time.Now() start := time.Now()
segmentTiming.active = segment.enabled segment.SetEnabled(b.env)
segment.SetText()
segmentTiming.active = segment.Enabled
if segmentTiming.active || segment.Style == Accordion { if segmentTiming.active || segment.Style == Accordion {
b.setActiveSegment(segment) b.setActiveSegment(segment)
b.renderActiveSegment() b.renderActiveSegment()

View file

@ -14,10 +14,10 @@ func TestBlockEnabled(t *testing.T) {
Type BlockType Type BlockType
}{ }{
{Case: "line break block", Expected: true, Type: LineBreak}, {Case: "line break block", Expected: true, Type: LineBreak},
{Case: "prompt enabled", Expected: true, Type: Prompt, Segments: []*Segment{{enabled: true}}}, {Case: "prompt enabled", Expected: true, Type: Prompt, Segments: []*Segment{{Enabled: true}}},
{Case: "prompt disabled", Expected: false, Type: Prompt, Segments: []*Segment{{enabled: false}}}, {Case: "prompt disabled", Expected: false, Type: Prompt, Segments: []*Segment{{Enabled: false}}},
{Case: "prompt enabled multiple", Expected: true, Type: Prompt, Segments: []*Segment{{enabled: false}, {enabled: true}}}, {Case: "prompt enabled multiple", Expected: true, Type: Prompt, Segments: []*Segment{{Enabled: false}, {Enabled: true}}},
{Case: "rprompt enabled multiple", Expected: true, Type: RPrompt, Segments: []*Segment{{enabled: false}, {enabled: true}}}, {Case: "rprompt enabled multiple", Expected: true, Type: RPrompt, Segments: []*Segment{{Enabled: false}, {Enabled: true}}},
} }
for _, tc := range cases { for _, tc := range cases {
block := &Block{ block := &Block{

View file

@ -129,7 +129,7 @@ func (e *Engine) renderBlock(block *Block) {
} }
switch block.Alignment { switch block.Alignment {
case Right: case Right:
text, length := block.renderSegments() text, length := block.RenderSegments()
if padText, OK := e.shouldFill(block, length); OK { if padText, OK := e.shouldFill(block, length); OK {
e.write(padText) e.write(padText)
} }
@ -138,12 +138,12 @@ func (e *Engine) renderBlock(block *Block) {
e.currentLineLength = 0 e.currentLineLength = 0
e.write(text) e.write(text)
case Left: case Left:
text, length := block.renderSegments() text, length := block.RenderSegments()
e.currentLineLength += length e.currentLineLength += length
e.write(text) e.write(text)
} }
case RPrompt: case RPrompt:
text, length := block.renderSegments() text, length := block.RenderSegments()
e.rpromptLength = length e.rpromptLength = length
if e.Env.Shell() == shell.BASH { if e.Env.Shell() == shell.BASH {
text = e.Ansi.FormatText(text) text = e.Ansi.FormatText(text)
@ -166,22 +166,22 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
e.write(fmt.Sprintf("\n\x1b[1mVersion:\x1b[0m %s\n", version)) e.write(fmt.Sprintf("\n\x1b[1mVersion:\x1b[0m %s\n", version))
e.write("\n\x1b[1mSegments:\x1b[0m\n\n") e.write("\n\x1b[1mSegments:\x1b[0m\n\n")
// console title timing // console title timing
titleStartTime := time.Now()
title := e.ConsoleTitle.GetTitle() title := e.ConsoleTitle.GetTitle()
title = strings.TrimPrefix(title, "\x1b]0;") title = strings.TrimPrefix(title, "\x1b]0;")
title = strings.TrimSuffix(title, "\a") title = strings.TrimSuffix(title, "\a")
duration := time.Since(startTime)
segmentTiming := &SegmentTiming{ segmentTiming := &SegmentTiming{
name: "ConsoleTitle", name: "ConsoleTitle",
nameLength: 12, nameLength: 12,
active: len(e.Config.ConsoleTitleTemplate) > 0, active: len(e.Config.ConsoleTitleTemplate) > 0,
text: title, text: title,
duration: duration, duration: time.Since(titleStartTime),
} }
segmentTimings = append(segmentTimings, segmentTiming) segmentTimings = append(segmentTimings, segmentTiming)
// loop each segments of each blocks // loop each segments of each blocks
for _, block := range e.Config.Blocks { for _, block := range e.Config.Blocks {
block.Init(e.Env, e.Writer, e.Ansi) block.Init(e.Env, e.Writer, e.Ansi)
longestSegmentName, timings := block.debug() longestSegmentName, timings := block.Debug()
segmentTimings = append(segmentTimings, timings...) segmentTimings = append(segmentTimings, timings...)
if longestSegmentName > largestSegmentNameLength { if longestSegmentName > largestSegmentNameLength {
largestSegmentNameLength = longestSegmentName largestSegmentNameLength = longestSegmentName
@ -243,7 +243,7 @@ func (e *Engine) PrintTooltip(tip string) string {
if !tooltip.writer.Enabled() { if !tooltip.writer.Enabled() {
return "" return ""
} }
tooltip.enabled = true tooltip.Enabled = true
// little hack to reuse the current logic // little hack to reuse the current logic
block := &Block{ block := &Block{
Alignment: Right, Alignment: Right,
@ -252,11 +252,11 @@ func (e *Engine) PrintTooltip(tip string) string {
switch e.Env.Shell() { switch e.Env.Shell() {
case shell.ZSH, shell.CMD, shell.FISH: case shell.ZSH, shell.CMD, shell.FISH:
block.Init(e.Env, e.Writer, e.Ansi) block.Init(e.Env, e.Writer, e.Ansi)
text, _ := block.renderSegments() text, _ := block.RenderSegments()
return text return text
case shell.PWSH, shell.PWSH5: case shell.PWSH, shell.PWSH5:
block.InitPlain(e.Env, e.Config) block.InitPlain(e.Env, e.Config)
text, length := block.renderSegments() text, length := block.RenderSegments()
e.write(e.Ansi.ClearAfter()) e.write(e.Ansi.ClearAfter())
e.write(e.Ansi.CarriageForward()) e.write(e.Ansi.CarriageForward())
e.write(e.Ansi.GetCursorForRightWrite(length, 0)) e.write(e.Ansi.GetCursorForRightWrite(length, 0))
@ -355,7 +355,7 @@ func (e *Engine) PrintRPrompt() string {
if !block.Enabled() { if !block.Enabled() {
return "" return ""
} }
text, length := block.renderSegments() text, length := block.RenderSegments()
e.rpromptLength = length e.rpromptLength = length
return text return text
} }

View file

@ -51,7 +51,7 @@ func BenchmarkEngineRender(b *testing.B) {
func engineRender() { func engineRender() {
env := &environment.ShellEnvironment{} env := &environment.ShellEnvironment{}
env.Init(false) env.Init()
defer env.Close() defer env.Close()
cfg := LoadConfig(env) cfg := LoadConfig(env)

View file

@ -9,6 +9,7 @@ import (
"oh-my-posh/segments" "oh-my-posh/segments"
"oh-my-posh/template" "oh-my-posh/template"
"runtime/debug" "runtime/debug"
"strings"
"time" "time"
) )
@ -29,7 +30,7 @@ type Segment struct {
Properties properties.Map `json:"properties,omitempty"` Properties properties.Map `json:"properties,omitempty"`
writer SegmentWriter writer SegmentWriter
enabled bool Enabled bool
text string text string
env environment.Environment env environment.Environment
backgroundCache string backgroundCache string
@ -325,7 +326,7 @@ func (segment *Segment) string() string {
return text return text
} }
func (segment *Segment) setEnabled(env environment.Environment) { func (segment *Segment) SetEnabled(env environment.Environment) {
defer func() { defer func() {
err := recover() err := recover()
if err == nil { if err == nil {
@ -334,14 +335,22 @@ func (segment *Segment) setEnabled(env environment.Environment) {
// display a message explaining omp failed(with the err) // display a message explaining omp failed(with the err)
message := fmt.Sprintf("\noh-my-posh fatal error rendering %s segment:%s\n\n%s\n", segment.Type, err, debug.Stack()) message := fmt.Sprintf("\noh-my-posh fatal error rendering %s segment:%s\n\n%s\n", segment.Type, err, debug.Stack())
fmt.Println(message) fmt.Println(message)
segment.enabled = true segment.Enabled = true
}() }()
err := segment.mapSegmentWithWriter(env) err := segment.mapSegmentWithWriter(env)
if err != nil || !segment.shouldIncludeFolder() { if err != nil || !segment.shouldIncludeFolder() {
return return
} }
if segment.writer.Enabled() { if segment.writer.Enabled() {
segment.enabled = true segment.Enabled = true
env.TemplateCache().AddSegmentData(string(segment.Type), segment.writer) env.TemplateCache().AddSegmentData(string(segment.Type), segment.writer)
} }
} }
func (segment *Segment) SetText() {
if !segment.Enabled {
return
}
segment.text = segment.string()
segment.Enabled = len(strings.ReplaceAll(segment.text, " ", "")) > 0
}

View file

@ -50,6 +50,7 @@ type Flags struct {
Migrate bool Migrate bool
TerminalWidth int TerminalWidth int
Strict bool Strict bool
Debug bool
} }
type CommandError struct { type CommandError struct {
@ -223,10 +224,9 @@ type ShellEnvironment struct {
fileCache *fileCache fileCache *fileCache
tmplCache *TemplateCache tmplCache *TemplateCache
logBuilder strings.Builder logBuilder strings.Builder
debug bool
} }
func (env *ShellEnvironment) Init(debug bool) { func (env *ShellEnvironment) Init() {
if env.CmdFlags == nil { if env.CmdFlags == nil {
env.CmdFlags = &Flags{} env.CmdFlags = &Flags{}
} }
@ -236,8 +236,7 @@ func (env *ShellEnvironment) Init(debug bool) {
env.cmdCache = &commandCache{ env.cmdCache = &commandCache{
commands: newConcurrentMap(), commands: newConcurrentMap(),
} }
if debug { if env.CmdFlags.Debug {
env.debug = true
log.SetOutput(&env.logBuilder) log.SetOutput(&env.logBuilder)
} }
} }
@ -294,7 +293,7 @@ func (env *ShellEnvironment) downloadConfig(location string) error {
} }
func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) { func (env *ShellEnvironment) trace(start time.Time, function string, args ...string) {
if !env.debug { if !env.CmdFlags.Debug {
return return
} }
elapsed := time.Since(start) elapsed := time.Since(start)
@ -303,7 +302,7 @@ func (env *ShellEnvironment) trace(start time.Time, function string, args ...str
} }
func (env *ShellEnvironment) log(lt logType, function, message string) { func (env *ShellEnvironment) log(lt logType, function, message string) {
if !env.debug { if !env.CmdFlags.Debug {
return return
} }
trace := fmt.Sprintf("%s: %s\n%s", lt, function, message) trace := fmt.Sprintf("%s: %s\n%s", lt, function, message)
@ -311,7 +310,7 @@ func (env *ShellEnvironment) log(lt logType, function, message string) {
} }
func (env *ShellEnvironment) debugF(function string, fn func() string) { func (env *ShellEnvironment) debugF(function string, fn func() string) {
if !env.debug { if !env.CmdFlags.Debug {
return return
} }
trace := fmt.Sprintf("%s: %s\n%s", Debug, function, fn()) trace := fmt.Sprintf("%s: %s\n%s", Debug, function, fn())