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",
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()

View file

@ -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)

View file

@ -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

View file

@ -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 {

View file

@ -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

View file

@ -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)
}