chore: allow silent rendering for benchmark testing

This commit is contained in:
Jan De Dobbeleer 2024-08-06 08:28:36 +02:00 committed by Jan De Dobbeleer
parent 84ac954c86
commit 3bc476488a
5 changed files with 47 additions and 81 deletions

View file

@ -7,14 +7,12 @@ import (
"github.com/jandedobbeleer/oh-my-posh/src/config" "github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/runtime" "github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/shell" "github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var (
printOutput bool printOutput bool
strict bool strict bool
manual bool
debug bool debug bool
supportedShells = []string{ supportedShells = []string{
@ -51,7 +49,42 @@ See the documentation to initialize your shell: https://ohmyposh.dev/docs/instal
_ = cmd.Help() _ = cmd.Help()
return 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().BoolVarP(&strict, "strict", "s", false, "run in strict mode")
initCmd.Flags().BoolVar(&debug, "debug", false, "enable/disable debug 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") _ = initCmd.MarkPersistentFlagRequired("config")
return initCmd 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)
}

View file

@ -19,7 +19,6 @@ var (
terminalWidth int terminalWidth int
eval bool eval bool
cleared bool cleared bool
cached bool
jobCount int jobCount int
saveCache bool saveCache bool
@ -124,15 +123,7 @@ func createPrintCmd() *cobra.Command {
printCmd.Flags().IntVar(&jobCount, "job-count", 0, "number of background jobs") printCmd.Flags().IntVar(&jobCount, "job-count", 0, "number of background jobs")
printCmd.Flags().BoolVar(&saveCache, "save-cache", false, "save updated cache to file") printCmd.Flags().BoolVar(&saveCache, "save-cache", false, "save updated cache to file")
// Deprecated flags, should be kept to avoid breaking CLI integration. // Hide flags that are for internal use only.
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")
_ = printCmd.Flags().MarkHidden("save-cache") _ = printCmd.Flags().MarkHidden("save-cache")
return printCmd return printCmd

View file

@ -1,17 +1,15 @@
package cli package cli
import ( import (
"fmt"
"os" "os"
"strings"
"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var ( var (
configFlag string configFlag string
displayVersion bool shellName string
silent bool
) )
var RootCmd = &cobra.Command{ 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 experience, regardless of where you are. For a detailed guide
on getting started, have a look at the docs at https://ohmyposh.dev`, on getting started, have a look at the docs at https://ohmyposh.dev`,
Run: func(cmd *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, _ []string) {
if initialize {
runInit(strings.ToLower(shellName))
return
}
if displayVersion {
fmt.Println(build.Version)
return
}
_ = cmd.Help() _ = cmd.Help()
}, },
} }
@ -41,21 +31,10 @@ func Execute() {
} }
} }
// Backwards compatibility
var (
shellName string
initialize bool
)
func init() { func init() {
RootCmd.PersistentFlags().StringVarP(&configFlag, "config", "c", "", "config file path") 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. // Hide flags that are deprecated or for internal use only.
_ = RootCmd.Flags().MarkHidden("init") RootCmd.PersistentFlags().BoolVar(&silent, "silent", false, "do not print anything")
_ = RootCmd.Flags().MarkHidden("shell") _ = RootCmd.Flags().MarkHidden("silent")
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
"github.com/jandedobbeleer/oh-my-posh/src/cli" "github.com/jandedobbeleer/oh-my-posh/src/cli"
@ -10,7 +11,7 @@ import (
func BenchmarkInit(b *testing.B) { func BenchmarkInit(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{"init", "fish", "--print"}) cmd.SetArgs([]string{"init", "fish", "--print", "--silent"})
out := bytes.NewBufferString("") out := bytes.NewBufferString("")
cmd.SetOut(out) cmd.SetOut(out)
@ -22,11 +23,13 @@ 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"}) cmd.SetArgs([]string{"print", "primary", "--pwd", "/Users/jan/Code/oh-my-posh/src", "--shell", "fish", "--silent"})
out := bytes.NewBufferString("") out := bytes.NewBufferString("")
cmd.SetOut(out) cmd.SetOut(out)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = cmd.Execute() _ = cmd.Execute()
} }
fmt.Println("")
} }

View file

@ -91,7 +91,6 @@ type Flags struct {
TerminalWidth int TerminalWidth int
ErrorCode int ErrorCode int
Plain bool Plain bool
Manual bool
Debug bool Debug bool
Primary bool Primary bool
HasTransient bool HasTransient bool