chore: allow silent rendering for benchmark testing

This commit is contained in:
Jan De Dobbeleer 2024-08-06 08:28:36 +02:00
parent f4666863d5
commit 81055063e3
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/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)
}

View file

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

View file

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

View file

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

View file

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