feat(print): improve performance

This commit is contained in:
Jan De Dobbeleer 2022-11-02 15:04:20 +01:00 committed by Jan De Dobbeleer
parent b73ac4bdc2
commit fdb05d8407
2 changed files with 19 additions and 8 deletions

View file

@ -365,28 +365,26 @@ func (env *ShellEnvironment) Pwd() string {
func (env *ShellEnvironment) HasFiles(pattern string) bool { func (env *ShellEnvironment) HasFiles(pattern string) bool {
defer env.Trace(time.Now(), "HasFiles", pattern) defer env.Trace(time.Now(), "HasFiles", pattern)
cwd := env.Pwd() cwd := env.Pwd()
pattern = cwd + env.PathSeparator() + pattern fileSystem := os.DirFS(cwd)
matches, err := filepath.Glob(pattern) matches, err := fs.Glob(fileSystem, pattern)
if err != nil { if err != nil {
env.Log(Error, "HasFiles", err.Error()) env.Log(Error, "HasFiles", err.Error())
return false return false
} }
for _, match := range matches { for _, match := range matches {
f, _ := os.Stat(match) file, err := fs.Stat(fileSystem, match)
if f.IsDir() { if err != nil || file.IsDir() {
continue continue
} }
env.Log(Debug, "HasFiles", "true")
return true return true
} }
env.Log(Debug, "HasFiles", "false")
return false return false
} }
func (env *ShellEnvironment) HasFilesInDir(dir, pattern string) bool { func (env *ShellEnvironment) HasFilesInDir(dir, pattern string) bool {
defer env.Trace(time.Now(), "HasFilesInDir", pattern) defer env.Trace(time.Now(), "HasFilesInDir", pattern)
pattern = dir + env.PathSeparator() + pattern fileSystem := os.DirFS(dir)
matches, err := filepath.Glob(pattern) matches, err := fs.Glob(fileSystem, pattern)
if err != nil { if err != nil {
env.Log(Error, "HasFilesInDir", err.Error()) env.Log(Error, "HasFilesInDir", err.Error())
return false return false

View file

@ -8,6 +8,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
cmd.SetArgs([]string{"init", "fish", "--print", "--config", "err.omp.json"}) cmd.SetArgs([]string{"init", "fish", "--print", "--config", "err.omp.json"})
out := bytes.NewBufferString("") out := bytes.NewBufferString("")
cmd.SetOut(out) cmd.SetOut(out)
@ -16,3 +17,15 @@ func BenchmarkInit(b *testing.B) {
_ = cmd.Execute() _ = cmd.Execute()
} }
} }
func BenchmarkPrimary(b *testing.B) {
cmd := cli.RootCmd
// needs to be a non-existing file as we panic otherwise
cmd.SetArgs([]string{"print", "primary", "--config", "err.omp.json", "--pwd", "/Users/jan/Code/oh-my-posh/src"})
out := bytes.NewBufferString("")
cmd.SetOut(out)
for i := 0; i < b.N; i++ {
_ = cmd.Execute()
}
}