diff --git a/src/cli/print.go b/src/cli/print.go index d61c88f9..ecafbde8 100644 --- a/src/cli/print.go +++ b/src/cli/print.go @@ -42,14 +42,14 @@ func createPrintCmd() *cobra.Command { Short: "Print the prompt/context", Long: "Print one of the prompts based on the location/use-case.", ValidArgs: []string{ - "debug", - "primary", - "secondary", - "transient", - "right", - "tooltip", - "valid", - "error", + prompt.DEBUG, + prompt.PRIMARY, + prompt.SECONDARY, + prompt.TRANSIENT, + prompt.RIGHT, + prompt.TOOLTIP, + prompt.VALID, + prompt.ERROR, }, Args: NoArgsOrOneValidArg, Run: func(cmd *cobra.Command, args []string) { @@ -71,7 +71,7 @@ func createPrintCmd() *cobra.Command { Shell: shellName, ShellVersion: shellVersion, Plain: plain, - Primary: args[0] == "primary", + Type: args[0], Cleared: cleared, NoExitCode: noStatus, Column: column, @@ -83,21 +83,21 @@ func createPrintCmd() *cobra.Command { defer eng.Env.Close() switch args[0] { - case "debug": + case prompt.DEBUG: fmt.Print(eng.ExtraPrompt(prompt.Debug)) - case "primary": + case prompt.PRIMARY: fmt.Print(eng.Primary()) - case "secondary": + case prompt.SECONDARY: fmt.Print(eng.ExtraPrompt(prompt.Secondary)) - case "transient": + case prompt.TRANSIENT: fmt.Print(eng.ExtraPrompt(prompt.Transient)) - case "right": + case prompt.RIGHT: fmt.Print(eng.RPrompt()) - case "tooltip": + case prompt.TOOLTIP: fmt.Print(eng.Tooltip(command)) - case "valid": + case prompt.VALID: fmt.Print(eng.ExtraPrompt(prompt.Valid)) - case "error": + case prompt.ERROR: fmt.Print(eng.ExtraPrompt(prompt.Error)) default: _ = cmd.Help() diff --git a/src/main_test.go b/src/main_test.go index a8c4fcfb..7aac6e2e 100644 --- a/src/main_test.go +++ b/src/main_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/jandedobbeleer/oh-my-posh/src/cli" + "github.com/jandedobbeleer/oh-my-posh/src/prompt" ) func BenchmarkInit(b *testing.B) { @@ -23,7 +24,7 @@ func BenchmarkInit(b *testing.B) { func BenchmarkPrimary(b *testing.B) { cmd := cli.RootCmd // needs to be a non-existing file as we panic otherwise - cmd.SetArgs([]string{"print", "primary", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish", "--silent"}) + cmd.SetArgs([]string{"print", prompt.PRIMARY, "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish", "--silent"}) out := bytes.NewBufferString("") cmd.SetOut(out) diff --git a/src/prompt/engine.go b/src/prompt/engine.go index 234974e1..532ac75b 100644 --- a/src/prompt/engine.go +++ b/src/prompt/engine.go @@ -26,6 +26,17 @@ type Engine struct { Plain bool } +const ( + PRIMARY = "primary" + TRANSIENT = "transient" + DEBUG = "debug" + SECONDARY = "secondary" + RIGHT = "right" + TOOLTIP = "tooltip" + VALID = "valid" + ERROR = "error" +) + func (e *Engine) write(text string) { e.prompt.WriteString(text) } @@ -457,6 +468,16 @@ func New(flags *runtime.Flags) *Engine { env.Init() cfg := config.Load(env) + // load the template cache for extra prompts prior to + // rendering any template + if flags.Type == DEBUG || + flags.Type == SECONDARY || + flags.Type == TRANSIENT || + flags.Type == VALID || + flags.Type == ERROR { + env.LoadTemplateCache() + } + template.Init(env) env.Var = cfg.Var diff --git a/src/prompt/extra.go b/src/prompt/extra.go index 2240b8ed..231900bd 100644 --- a/src/prompt/extra.go +++ b/src/prompt/extra.go @@ -21,8 +21,6 @@ const ( ) func (e *Engine) ExtraPrompt(promptType ExtraPromptType) string { - // populate env with latest context - e.Env.LoadTemplateCache() var prompt *config.Segment switch promptType { diff --git a/src/runtime/environment.go b/src/runtime/environment.go index 166ee0b5..bde949f5 100644 --- a/src/runtime/environment.go +++ b/src/runtime/environment.go @@ -18,6 +18,8 @@ const ( DARWIN = "darwin" LINUX = "linux" CMD = "cmd" + + PRIMARY = "primary" ) type Environment interface { @@ -83,17 +85,17 @@ type Flags struct { Shell string ShellVersion string PWD string + Type string + ErrorCode int PromptCount int - ExecutionTime float64 - JobCount int StackCount int Column int TerminalWidth int - ErrorCode int - Plain bool - Debug bool - Primary bool + ExecutionTime float64 + JobCount int HasTransient bool + Debug bool + Plain bool Strict bool Cleared bool NoExitCode bool diff --git a/src/runtime/terminal.go b/src/runtime/terminal.go index fb190061..12c8f7dd 100644 --- a/src/runtime/terminal.go +++ b/src/runtime/terminal.go @@ -581,7 +581,7 @@ func (term *Terminal) Session() cache.Cache { func (term *Terminal) saveTemplateCache() { // only store this when in a primary prompt // and when we have a transient prompt in the config - canSave := term.CmdFlags.Primary && term.CmdFlags.HasTransient + canSave := term.CmdFlags.Type == PRIMARY && term.CmdFlags.HasTransient if !canSave { return } @@ -755,7 +755,7 @@ func (term *Terminal) setPromptCount() { } // Only update the count if we're generating a primary prompt. - if term.CmdFlags.Primary { + if term.CmdFlags.Type == PRIMARY { count++ term.Session().Set(cache.PROMPTCOUNTCACHE, strconv.Itoa(count), cache.ONEDAY) }