Skip to content

Commit

Permalink
fix(template): load cache before evaluation starts
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Nov 7, 2024
1 parent 03dfd0e commit 86291dc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
34 changes: 17 additions & 17 deletions src/cli/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions src/prompt/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/prompt/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 8 additions & 6 deletions src/runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const (
DARWIN = "darwin"
LINUX = "linux"
CMD = "cmd"

PRIMARY = "primary"
)

type Environment interface {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 86291dc

Please sign in to comment.