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

View file

@ -8,6 +8,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", "--config", "err.omp.json"})
out := bytes.NewBufferString("")
cmd.SetOut(out)
@ -16,3 +17,15 @@ func BenchmarkInit(b *testing.B) {
_ = 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()
}
}