fix(template): load cache before evaluation starts

This commit is contained in:
Jan De Dobbeleer 2024-11-07 08:42:21 +01:00 committed by Jan De Dobbeleer
parent 03dfd0e469
commit 86291dc6bc
6 changed files with 50 additions and 28 deletions

View file

@ -42,14 +42,14 @@ func createPrintCmd() *cobra.Command {
Short: "Print the prompt/context", Short: "Print the prompt/context",
Long: "Print one of the prompts based on the location/use-case.", Long: "Print one of the prompts based on the location/use-case.",
ValidArgs: []string{ ValidArgs: []string{
"debug", prompt.DEBUG,
"primary", prompt.PRIMARY,
"secondary", prompt.SECONDARY,
"transient", prompt.TRANSIENT,
"right", prompt.RIGHT,
"tooltip", prompt.TOOLTIP,
"valid", prompt.VALID,
"error", prompt.ERROR,
}, },
Args: NoArgsOrOneValidArg, Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -71,7 +71,7 @@ func createPrintCmd() *cobra.Command {
Shell: shellName, Shell: shellName,
ShellVersion: shellVersion, ShellVersion: shellVersion,
Plain: plain, Plain: plain,
Primary: args[0] == "primary", Type: args[0],
Cleared: cleared, Cleared: cleared,
NoExitCode: noStatus, NoExitCode: noStatus,
Column: column, Column: column,
@ -83,21 +83,21 @@ func createPrintCmd() *cobra.Command {
defer eng.Env.Close() defer eng.Env.Close()
switch args[0] { switch args[0] {
case "debug": case prompt.DEBUG:
fmt.Print(eng.ExtraPrompt(prompt.Debug)) fmt.Print(eng.ExtraPrompt(prompt.Debug))
case "primary": case prompt.PRIMARY:
fmt.Print(eng.Primary()) fmt.Print(eng.Primary())
case "secondary": case prompt.SECONDARY:
fmt.Print(eng.ExtraPrompt(prompt.Secondary)) fmt.Print(eng.ExtraPrompt(prompt.Secondary))
case "transient": case prompt.TRANSIENT:
fmt.Print(eng.ExtraPrompt(prompt.Transient)) fmt.Print(eng.ExtraPrompt(prompt.Transient))
case "right": case prompt.RIGHT:
fmt.Print(eng.RPrompt()) fmt.Print(eng.RPrompt())
case "tooltip": case prompt.TOOLTIP:
fmt.Print(eng.Tooltip(command)) fmt.Print(eng.Tooltip(command))
case "valid": case prompt.VALID:
fmt.Print(eng.ExtraPrompt(prompt.Valid)) fmt.Print(eng.ExtraPrompt(prompt.Valid))
case "error": case prompt.ERROR:
fmt.Print(eng.ExtraPrompt(prompt.Error)) fmt.Print(eng.ExtraPrompt(prompt.Error))
default: default:
_ = cmd.Help() _ = cmd.Help()

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/cli" "github.com/jandedobbeleer/oh-my-posh/src/cli"
"github.com/jandedobbeleer/oh-my-posh/src/prompt"
) )
func BenchmarkInit(b *testing.B) { func BenchmarkInit(b *testing.B) {
@ -23,7 +24,7 @@ func BenchmarkInit(b *testing.B) {
func BenchmarkPrimary(b *testing.B) { func BenchmarkPrimary(b *testing.B) {
cmd := cli.RootCmd cmd := cli.RootCmd
// needs to be a non-existing file as we panic otherwise // 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("") out := bytes.NewBufferString("")
cmd.SetOut(out) cmd.SetOut(out)

View file

@ -26,6 +26,17 @@ type Engine struct {
Plain bool 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) { func (e *Engine) write(text string) {
e.prompt.WriteString(text) e.prompt.WriteString(text)
} }
@ -457,6 +468,16 @@ func New(flags *runtime.Flags) *Engine {
env.Init() env.Init()
cfg := config.Load(env) 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) template.Init(env)
env.Var = cfg.Var env.Var = cfg.Var

View file

@ -21,8 +21,6 @@ const (
) )
func (e *Engine) ExtraPrompt(promptType ExtraPromptType) string { func (e *Engine) ExtraPrompt(promptType ExtraPromptType) string {
// populate env with latest context
e.Env.LoadTemplateCache()
var prompt *config.Segment var prompt *config.Segment
switch promptType { switch promptType {

View file

@ -18,6 +18,8 @@ const (
DARWIN = "darwin" DARWIN = "darwin"
LINUX = "linux" LINUX = "linux"
CMD = "cmd" CMD = "cmd"
PRIMARY = "primary"
) )
type Environment interface { type Environment interface {
@ -83,17 +85,17 @@ type Flags struct {
Shell string Shell string
ShellVersion string ShellVersion string
PWD string PWD string
Type string
ErrorCode int
PromptCount int PromptCount int
ExecutionTime float64
JobCount int
StackCount int StackCount int
Column int Column int
TerminalWidth int TerminalWidth int
ErrorCode int ExecutionTime float64
Plain bool JobCount int
Debug bool
Primary bool
HasTransient bool HasTransient bool
Debug bool
Plain bool
Strict bool Strict bool
Cleared bool Cleared bool
NoExitCode bool NoExitCode bool

View file

@ -581,7 +581,7 @@ func (term *Terminal) Session() cache.Cache {
func (term *Terminal) saveTemplateCache() { func (term *Terminal) saveTemplateCache() {
// only store this when in a primary prompt // only store this when in a primary prompt
// and when we have a transient prompt in the config // 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 { if !canSave {
return return
} }
@ -755,7 +755,7 @@ func (term *Terminal) setPromptCount() {
} }
// Only update the count if we're generating a primary prompt. // Only update the count if we're generating a primary prompt.
if term.CmdFlags.Primary { if term.CmdFlags.Type == PRIMARY {
count++ count++
term.Session().Set(cache.PROMPTCOUNTCACHE, strconv.Itoa(count), cache.ONEDAY) term.Session().Set(cache.PROMPTCOUNTCACHE, strconv.Itoa(count), cache.ONEDAY)
} }