feat(init): add --debug flag

This commit is contained in:
Jan De Dobbeleer 2024-07-25 13:11:48 +02:00 committed by Jan De Dobbeleer
parent 9e73b15a3b
commit 1a7f2d2d10
14 changed files with 58 additions and 34 deletions

6
src/cache/file.go vendored
View file

@ -21,10 +21,12 @@ func (fc *File) Init(cacheFilePath string) {
fc.cache = maps.NewConcurrent()
fc.cacheFilePath = cacheFilePath
log.Debug("Loading cache file:", fc.cacheFilePath)
log.Debug("loading cache file:", fc.cacheFilePath)
content, err := os.ReadFile(fc.cacheFilePath)
if err != nil {
// set to dirty so we create it on close
fc.dirty = true
log.Error(err)
return
}
@ -39,7 +41,7 @@ func (fc *File) Init(cacheFilePath string) {
continue
}
log.Debug("Loading cache key:", key)
log.Debug("loading cache key:", key)
fc.cache.Set(key, co)
}
}

View file

@ -2,11 +2,11 @@ package cli
import (
"fmt"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
"github.com/spf13/cobra"
)
@ -52,34 +52,36 @@ func init() {
initCmd.Flags().BoolVarP(&printOutput, "print", "p", false, "print the init script")
initCmd.Flags().BoolVarP(&strict, "strict", "s", false, "run in strict mode")
initCmd.Flags().BoolVarP(&manual, "manual", "m", false, "enable/disable manual mode")
initCmd.Flags().BoolVar(&debug, "debug", false, "enable/disable debug mode")
_ = initCmd.MarkPersistentFlagRequired("config")
RootCmd.AddCommand(initCmd)
}
func runInit(shellName string) {
var startTime time.Time
if debug {
startTime = time.Now()
}
env := &runtime.Terminal{
CmdFlags: &runtime.Flags{
Shell: shellName,
Config: configFlag,
Strict: strict,
Manual: manual,
Debug: debug,
},
}
env.Init()
defer env.Close()
cfg := config.Load(env)
// TODO: this can be removed I think
// allow overriding the upgrade notice from the config
if cfg.DisableNotice || cfg.AutoUpgrade {
env.Cache().Set(upgrade.CACHEKEY, "disabled", -1)
}
feats := cfg.Features()
if printOutput {
init := shell.PrintInit(env, feats)
if printOutput || debug {
init := shell.PrintInit(env, feats, &startTime)
fmt.Print(init)
return
}

View file

@ -112,7 +112,7 @@ func (segment *Segment) SetEnabled(env runtime.Environment) {
return
}
segment.env.DebugF("Segment: %s", segment.Name())
segment.env.DebugF("segment: %s", segment.Name())
// validate toggles
if toggles, OK := segment.env.Session().Get(cache.TOGGLECACHE); OK && len(toggles) > 0 {

View file

@ -20,7 +20,7 @@ func (e *Engine) PrintDebug(startTime time.Time, version string) string {
// console title timing
titleStartTime := time.Now()
e.Env.Debug("Segment: Title")
e.Env.Debug("segment: Title")
title := e.getTitleTemplateText()
consoleTitle := &config.Segment{
Alias: "ConsoleTitle",

View file

@ -225,10 +225,12 @@ func (term *Terminal) Init() {
if term.CmdFlags.Debug {
log.Enable()
log.Debug("debug mode enabled")
}
if term.CmdFlags.Plain {
log.Plain()
log.Debug("dlain mode enabled")
}
initCache := func(fileName string) *cache.File {
@ -256,13 +258,13 @@ func (term *Terminal) resolveConfigPath() {
defer term.Trace(time.Now())
if poshTheme := term.Getenv("POSH_THEME"); len(poshTheme) > 0 {
term.DebugF("Config set using POSH_THEME: %s", poshTheme)
term.DebugF("config set using POSH_THEME: %s", poshTheme)
term.CmdFlags.Config = poshTheme
return
}
if len(term.CmdFlags.Config) == 0 {
term.Debug("No config set, fallback to default config")
term.Debug("no config set, fallback to default config")
return
}
@ -285,7 +287,7 @@ func (term *Terminal) resolveConfigPath() {
// Cygwin path always needs the full path as we're on Windows but not really.
// Doing filepath actions will convert it to a Windows path and break the init script.
if isCygwin() {
term.Debug("Cygwin detected, using full path for config")
term.Debug("cygwin detected, using full path for config")
return
}
@ -901,6 +903,8 @@ func dirMatchesOneOf(dir, home, goos string, regexes []string) bool {
}
func (term *Terminal) SetPromptCount() {
defer term.Trace(time.Now())
countStr := os.Getenv("POSH_PROMPT_COUNT")
if len(countStr) > 0 {
// this counter is incremented by the shell

View file

@ -42,7 +42,7 @@ func (t *Azd) Enabled() bool {
}
if len(parentFilePath) == 0 {
t.env.Debug("No .azure folder found in parent directories")
t.env.Debug("no .azure folder found in parent directories")
return false
}

View file

@ -353,7 +353,7 @@ func (g *Git) hasWorktree(gitdir *runtime.FileInfo) bool {
matches := regex.FindNamedRegexMatch(`^gitdir: (?P<dir>.*)$`, content)
if matches == nil || len(matches["dir"]) == 0 {
g.env.Debug("No matches found, directory isn't a worktree")
g.env.Debug("no matches found, directory isn't a worktree")
return false
}

View file

@ -368,7 +368,7 @@ func (nba *Nba) getResult() (*NBAData, error) {
// Cache the fact a game was not found for 30 minutes so we don't call the API too often
cacheNotFoundTimeout := nba.props.GetInt(properties.CacheTimeout, 30)
nba.env.Debug("Validating cache data for " + teamName)
nba.env.Debug("validating cache data for " + teamName)
if cacheScheduleTimeout > 0 {
if data, err := nba.getCachedScheduleValue(cachedScheduleKey); err == nil {
@ -382,7 +382,7 @@ func (nba *Nba) getResult() (*NBAData, error) {
}
}
nba.env.Debug("Fetching available data for " + teamName)
nba.env.Debug("fetching available data for " + teamName)
data, err := nba.getAvailableGameData(teamName, httpTimeout)
if err != nil {

View file

@ -44,7 +44,7 @@ func (u *Umbraco) Enabled() bool {
}
if len(location) == 0 {
u.env.Debug("No umbraco folder found in parent directories")
u.env.Debug("no umbraco folder found in parent directories")
return false
}
@ -94,7 +94,7 @@ func (u *Umbraco) Init(props properties.Properties, env runtime.Environment) {
func (u *Umbraco) TryFindModernUmbraco(configPath string) bool {
// Check the passed in filepath is not empty
if len(configPath) == 0 {
u.env.Debug("UMBRACO: No .CSProj file path passed in")
u.env.Debug("no configPath provided")
return false
}
@ -111,7 +111,6 @@ func (u *Umbraco) TryFindModernUmbraco(configPath string) bool {
err := xml.Unmarshal([]byte(contents), &csProjPackages)
if err != nil {
u.env.Debug("UMBRACO: Error while trying to parse XML of .csproj file")
u.env.Debug(err.Error())
}
@ -131,7 +130,7 @@ func (u *Umbraco) TryFindModernUmbraco(configPath string) bool {
func (u *Umbraco) TryFindLegacyUmbraco(configPath string) bool {
// Check the passed in filepath is not empty
if len(configPath) == 0 {
u.env.Debug("UMBRACO: No web.config file path passed in")
u.env.Debug("no configPath provided")
return false
}
@ -148,7 +147,6 @@ func (u *Umbraco) TryFindLegacyUmbraco(configPath string) bool {
err := xml.Unmarshal([]byte(contents), &webConfigAppSettings)
if err != nil {
u.env.Debug("UMBRACO: Error while trying to parse XML of web.config file")
u.env.Debug(err.Error())
}

View file

@ -42,12 +42,12 @@ func (u *Unity) Enabled() bool {
func (u *Unity) GetUnityVersion() (string, error) {
projectDir, err := u.env.HasParentFilePath("ProjectSettings", false)
if err != nil {
u.env.Debug("No ProjectSettings parent folder found")
u.env.Debug("no ProjectSettings parent folder found")
return "", err
}
if !u.env.HasFilesInDir(projectDir.Path, "ProjectVersion.txt") {
u.env.Debug("No ProjectVersion.txt file found")
u.env.Debug("no ProjectVersion.txt file found")
return "", err
}

View file

@ -4,7 +4,9 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/jandedobbeleer/oh-my-posh/src/log"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
)
@ -64,7 +66,7 @@ func Init(env runtime.Environment, feats Features) string {
return fmt.Sprintf(command, executable, shell, config, additionalParams)
case ZSH, BASH, FISH, CMD, TCSH, XONSH:
return PrintInit(env, feats)
return PrintInit(env, feats, nil)
case NU:
createNuInit(env, feats)
return ""
@ -73,7 +75,7 @@ func Init(env runtime.Environment, feats Features) string {
}
}
func PrintInit(env runtime.Environment, features Features) string {
func PrintInit(env runtime.Environment, features Features, startTime *time.Time) string {
executable, err := getExecutablePath(env)
if err != nil {
return noExe
@ -127,5 +129,21 @@ func PrintInit(env runtime.Environment, features Features) string {
"::SHELL::", shell,
).Replace(script)
return features.Lines(shell).String(init)
shellScript := features.Lines(shell).String(init)
if !env.Flags().Debug {
return shellScript
}
var builder strings.Builder
builder.WriteString(fmt.Sprintf("\n%s %s\n", log.Text("Init duration:").Green().Bold().Plain(), time.Since(*startTime)))
builder.WriteString(log.Text("\nScript:\n\n").Green().Bold().Plain().String())
builder.WriteString(shellScript)
builder.WriteString(log.Text("\n\nLogs:\n\n").Green().Bold().Plain().String())
builder.WriteString(env.Logs())
return builder.String()
}

View file

@ -43,7 +43,7 @@ func createNuInit(env runtime.Environment, features Features) {
return
}
_, err = f.WriteString(PrintInit(env, features))
_, err = f.WriteString(PrintInit(env, features, nil))
if err != nil {
return
}

View file

@ -76,7 +76,7 @@ func (c *Context) init(t *Text) {
}
func (t *Text) Render() (string, error) {
t.Env.DebugF("Rendering template: %s", t.Template)
t.Env.DebugF("rendering template: %s", t.Template)
shell = t.Env.Flags().Shell

View file

@ -102,8 +102,8 @@ func Init(sh string) {
Shell = sh
Program = getTerminalName()
log.Debug("Terminal shell:", Shell)
log.Debug("Terminal program:", Program)
log.Debug("terminal program:", Program)
log.Debug("terminal shell:", Shell)
color.TrueColor = Program != AppleTerminal