mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-29 03:51:52 -08:00
fix(template): load cache before evaluation starts
This commit is contained in:
parent
03dfd0e469
commit
86291dc6bc
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue