From 3bc476488a36d4d7e6fde8f40fad18266ec95ad4 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 6 Aug 2024 08:28:36 +0200 Subject: [PATCH] chore: allow silent rendering for benchmark testing --- src/cli/init.go | 78 ++++++++++++++++++-------------------- src/cli/print.go | 11 +----- src/cli/root.go | 31 +++------------ src/main_test.go | 7 +++- src/runtime/environment.go | 1 - 5 files changed, 47 insertions(+), 81 deletions(-) diff --git a/src/cli/init.go b/src/cli/init.go index 1f4cf395..3583ad2d 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -7,14 +7,12 @@ import ( "github.com/jandedobbeleer/oh-my-posh/src/config" "github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/shell" - "github.com/spf13/cobra" ) var ( printOutput bool strict bool - manual bool debug bool supportedShells = []string{ @@ -51,7 +49,42 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal _ = cmd.Help() return } - runInit(args[0]) + + var startTime time.Time + if debug { + startTime = time.Now() + } + + env := &runtime.Terminal{ + CmdFlags: &runtime.Flags{ + Shell: shellName, + Config: configFlag, + Strict: strict, + Debug: debug, + }, + } + + env.Init() + defer env.Close() + + cfg := config.Load(env) + + feats := cfg.Features() + + var output string + + switch { + case printOutput, debug: + output = shell.PrintInit(env, feats, &startTime) + default: + output = shell.Init(env, feats) + } + + if silent { + return + } + + fmt.Print(output) }, } @@ -59,46 +92,7 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal initCmd.Flags().BoolVarP(&strict, "strict", "s", false, "run in strict mode") initCmd.Flags().BoolVar(&debug, "debug", false, "enable/disable debug mode") - // Deprecated flags, should be kept to avoid breaking CLI integration. - initCmd.Flags().BoolVarP(&manual, "manual", "m", false, "enable/disable manual mode") - - // Hide flags that are deprecated or for internal use only. - _ = initCmd.Flags().MarkHidden("manual") - _ = initCmd.MarkPersistentFlagRequired("config") return initCmd } - -func runInit(shellName string) { - var startTime time.Time - if debug { - startTime = time.Now() - } - - env := &runtime.Terminal{ - CmdFlags: &runtime.Flags{ - Shell: shellName, - Config: configFlag, - Strict: strict, - Debug: debug, - Init: true, - }, - } - - env.Init() - defer env.Close() - - cfg := config.Load(env) - - feats := cfg.Features() - - if printOutput || debug { - init := shell.PrintInit(env, feats, &startTime) - fmt.Print(init) - return - } - - init := shell.Init(env, feats) - fmt.Print(init) -} diff --git a/src/cli/print.go b/src/cli/print.go index 9dcdafad..d61c88f9 100644 --- a/src/cli/print.go +++ b/src/cli/print.go @@ -19,7 +19,6 @@ var ( terminalWidth int eval bool cleared bool - cached bool jobCount int saveCache bool @@ -124,15 +123,7 @@ func createPrintCmd() *cobra.Command { printCmd.Flags().IntVar(&jobCount, "job-count", 0, "number of background jobs") printCmd.Flags().BoolVar(&saveCache, "save-cache", false, "save updated cache to file") - // Deprecated flags, should be kept to avoid breaking CLI integration. - printCmd.Flags().IntVarP(&status, "error", "e", 0, "last exit code") - printCmd.Flags().BoolVar(&noStatus, "no-exit-code", false, "no valid exit code (cancelled or no command yet)") - printCmd.Flags().BoolVar(&cached, "cached", false, "use a cached prompt") - - // Hide flags that are deprecated or for internal use only. - _ = printCmd.Flags().MarkHidden("error") - _ = printCmd.Flags().MarkHidden("no-exit-code") - _ = printCmd.Flags().MarkHidden("cached") + // Hide flags that are for internal use only. _ = printCmd.Flags().MarkHidden("save-cache") return printCmd diff --git a/src/cli/root.go b/src/cli/root.go index 4d4c8242..90650df4 100644 --- a/src/cli/root.go +++ b/src/cli/root.go @@ -1,17 +1,15 @@ package cli import ( - "fmt" "os" - "strings" - "github.com/jandedobbeleer/oh-my-posh/src/build" "github.com/spf13/cobra" ) var ( - configFlag string - displayVersion bool + configFlag string + shellName string + silent bool ) var RootCmd = &cobra.Command{ @@ -22,14 +20,6 @@ It can use the same configuration everywhere to offer a consistent experience, regardless of where you are. For a detailed guide on getting started, have a look at the docs at https://ohmyposh.dev`, Run: func(cmd *cobra.Command, _ []string) { - if initialize { - runInit(strings.ToLower(shellName)) - return - } - if displayVersion { - fmt.Println(build.Version) - return - } _ = cmd.Help() }, } @@ -41,21 +31,10 @@ func Execute() { } } -// Backwards compatibility -var ( - shellName string - initialize bool -) - func init() { RootCmd.PersistentFlags().StringVarP(&configFlag, "config", "c", "", "config file path") - RootCmd.Flags().BoolVar(&displayVersion, "version", false, "version") - - // Deprecated flags, should be kept to avoid breaking CLI integration. - RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init") - RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell") // Hide flags that are deprecated or for internal use only. - _ = RootCmd.Flags().MarkHidden("init") - _ = RootCmd.Flags().MarkHidden("shell") + RootCmd.PersistentFlags().BoolVar(&silent, "silent", false, "do not print anything") + _ = RootCmd.Flags().MarkHidden("silent") } diff --git a/src/main_test.go b/src/main_test.go index c60fbe0b..a8c4fcfb 100644 --- a/src/main_test.go +++ b/src/main_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "fmt" "testing" "github.com/jandedobbeleer/oh-my-posh/src/cli" @@ -10,7 +11,7 @@ import ( func BenchmarkInit(b *testing.B) { cmd := cli.RootCmd // needs to be a non-existing file as we panic otherwise - cmd.SetArgs([]string{"init", "fish", "--print"}) + cmd.SetArgs([]string{"init", "fish", "--print", "--silent"}) out := bytes.NewBufferString("") cmd.SetOut(out) @@ -22,11 +23,13 @@ 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"}) + cmd.SetArgs([]string{"print", "primary", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish", "--silent"}) out := bytes.NewBufferString("") cmd.SetOut(out) for i := 0; i < b.N; i++ { _ = cmd.Execute() } + + fmt.Println("") } diff --git a/src/runtime/environment.go b/src/runtime/environment.go index 46c4e650..166ee0b5 100644 --- a/src/runtime/environment.go +++ b/src/runtime/environment.go @@ -91,7 +91,6 @@ type Flags struct { TerminalWidth int ErrorCode int Plain bool - Manual bool Debug bool Primary bool HasTransient bool