feat(debug): add plain support

resolves #3612
This commit is contained in:
Jan De Dobbeleer 2023-03-22 09:12:54 +01:00 committed by Jan De Dobbeleer
parent 6af47a61d5
commit 0692873cd4
6 changed files with 93 additions and 29 deletions

View file

@ -56,7 +56,7 @@ body:
id: logs
attributes:
label: Log output
description: Please copy and paste the output generated by `oh-my-posh debug`.
description: Please copy and paste the output generated by `oh-my-posh debug --plain`.
render: Shell
validations:
required: true

View file

@ -27,6 +27,7 @@ var debugCmd = &cobra.Command{
PWD: pwd,
Shell: shellName,
Version: cliVersion,
Plain: plain,
},
}
env.Init()
@ -36,6 +37,7 @@ var debugCmd = &cobra.Command{
writer := &ansi.Writer{
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),
AnsiColors: writerColors,
Plain: plain,
}
writer.Init(shell.GENERIC)
eng := &engine.Engine{
@ -51,5 +53,6 @@ var debugCmd = &cobra.Command{
func init() { //nolint:gochecknoinits
debugCmd.Flags().StringVar(&pwd, "pwd", "", "current working directory")
debugCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
debugCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)")
RootCmd.AddCommand(debugCmd)
}

View file

@ -6,6 +6,7 @@ import (
"time"
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/log"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/jandedobbeleer/oh-my-posh/src/template"
@ -252,9 +253,8 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
// debug will loop through your config file and output the timings for each segments
func (e *Engine) PrintDebug(startTime time.Time, version string) string {
var segmentTimings []*SegmentTiming
e.write(fmt.Sprintf("\n\x1b[38;2;191;207;240m\x1b[1mVersion:\x1b[0m %s\n", version))
e.write("\n\x1b[38;2;191;207;240m\x1b[1mSegments:\x1b[0m\n\n")
e.write(fmt.Sprintf("\n%s %s\n", log.Text("Version:").Green().Bold().Plain(), version))
e.write(log.Text("\nSegments:\n\n").Green().Bold().Plain().String())
// console title timing
titleStartTime := time.Now()
title := e.getTitleTemplateText()
@ -266,6 +266,7 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
duration: time.Since(titleStartTime),
}
largestSegmentNameLength := consoleTitleTiming.nameLength
var segmentTimings []*SegmentTiming
segmentTimings = append(segmentTimings, consoleTitleTiming)
// cache a pointer to the color cycle
cycle = &e.Config.Cycle
@ -283,19 +284,19 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
largestSegmentNameLength += 22 + 7
for _, segment := range segmentTimings {
duration := segment.duration.Milliseconds()
var active string
var active log.Text
if segment.active {
active = "\x1b[38;2;156;231;201mtrue\x1b[0m"
active = log.Text("true").Yellow()
} else {
active = "\x1b[38;2;204;137;214mfalse\x1b[0m"
active = log.Text("false").Purple()
}
segmentName := fmt.Sprintf("%s(%s)", segment.name, active)
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("\n\x1b[38;2;191;207;240m\x1b[1mRun duration:\x1b[0m %s\n", time.Since(startTime)))
e.write(fmt.Sprintf("\n\x1b[38;2;191;207;240m\x1b[1mCache path:\x1b[0m %s\n", e.Env.CachePath()))
e.write(fmt.Sprintf("\n\x1b[38;2;191;207;240m\x1b[1mConfig path:\x1b[0m %s\n", e.Env.Flags().Config))
e.write("\n\x1b[38;2;191;207;240m\x1b[1mLogs:\x1b[0m\n\n")
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("Cache path:").Green().Bold().Plain(), e.Env.CachePath()))
e.write(fmt.Sprintf("\n%s %s\n", log.Text("Config path:").Green().Bold().Plain(), e.Env.Flags().Config))
e.write(log.Text("\nLogs:\n\n").Green().Bold().Plain().String())
e.write(e.Env.Logs())
return e.string()
}

View file

@ -8,13 +8,20 @@ import (
"time"
)
var enabled bool
var log strings.Builder
var (
enabled bool
plain bool
log strings.Builder
)
func Enable() {
enabled = true
}
func Plain() {
plain = true
}
func Info(message string) {
if !enabled {
return
@ -28,7 +35,7 @@ func Trace(start time.Time, args ...string) {
}
elapsed := time.Since(start)
fn, _ := funcSpec()
header := fmt.Sprintf("%s(%s) - \x1b[38;2;156;231;201m%s\033[0m", fn, strings.Join(args, " "), elapsed)
header := fmt.Sprintf("%s(%s) - %s", fn, strings.Join(args, " "), Text(elapsed.String()).Yellow().Plain())
printLn(trace, header)
}

View file

@ -14,48 +14,98 @@ const (
trace
)
type Text string
func (t Text) Green() Text {
if plain {
return t
}
return "\x1b[38;2;191;207;240m" + t
}
func (t Text) Red() Text {
if plain {
return t
}
return "\x1b[38;2;253;122;140m" + t
}
func (t Text) Purple() Text {
if plain {
return t
}
return "\x1b[38;2;204;137;214m" + t
}
func (t Text) Yellow() Text {
if plain {
return t
}
return "\x1b[38;2;156;231;201m" + t
}
func (t Text) Bold() Text {
if plain {
return t
}
return "\x1b[1m" + t
}
func (t Text) Plain() Text {
if plain {
return t
}
return t + "\033[0m"
}
func (t Text) String() string {
return string(t)
}
func printLn(lt logType, args ...string) {
if len(args) == 0 {
return
}
var str string
var str Text
switch lt {
case debug:
str = "\x1b[38;2;191;207;240m[DEBUG] "
str = Text("[DEBUG] ").Green()
case bug:
str = "\x1b[38;2;253;122;140m[ERROR] "
str = Text("[ERROR] ").Red()
case trace:
str = "\x1b[38;2;204;137;214m[TRACE] "
str = Text("[TRACE] ").Purple()
}
// timestamp 156, 231, 201
str += fmt.Sprintf("\x1b[38;2;156;231;201m%s ", time.Now().Format("15:04:05.000"))
str += "\033[0m"
str += args[0]
str += Text(time.Now().Format("15:04:05.000") + " ").Yellow().Plain()
str += Text(args[0])
str += parseArgs(args...)
log.WriteString(str)
log.WriteString(str.String())
}
func parseArgs(args ...string) string {
func parseArgs(args ...string) Text {
if len(args) == 1 {
return "\n"
}
// display empty return values as NO DATA
if len(args[1]) == 0 {
return " \x1b[38;2;156;231;201m\u2192\033[0m \x1b[38;2;253;122;140mNO DATA\033[0m\n"
text := Text(" \u2192").Yellow()
text += Text(" NO DATA\n").Red().Plain()
return text
}
// print a single line for single output
splitted := strings.Split(args[1], "\n")
if len(splitted) == 1 {
return fmt.Sprintf(" \x1b[38;2;156;231;201m\u2192\033[0m %s\n", args[1])
text := Text(" \u2192").Yellow().Plain()
return Text(fmt.Sprintf("%s %s\n", text, args[1]))
}
// indent multiline output with 4 spaces
var str string
str += " \x1b[38;2;156;231;201m\u2193\033[0m\n"
var str Text
str += Text(" \u2193\n").Yellow().Plain()
for _, line := range splitted {
str += fmt.Sprintf(" %s\n", line)
str += Text(fmt.Sprintf(" %s\n", line))
}
return str
}

View file

@ -297,6 +297,9 @@ func (env *Shell) Init() {
if env.CmdFlags.Debug {
log.Enable()
}
if env.CmdFlags.Plain {
log.Plain()
}
env.fileCache = &fileCache{}
env.fileCache.Init(env.CachePath())
env.resolveConfigPath()