refactor: list entire directory content

This commit is contained in:
Jan De Dobbeleer 2022-04-16 19:52:59 +02:00 committed by Jan De Dobbeleer
parent cf6c7b230d
commit 0cd8bfe6b6
3 changed files with 24 additions and 16 deletions

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -138,7 +139,7 @@ type Environment interface {
DirMatchesOneOf(dir string, regexes []string) bool DirMatchesOneOf(dir string, regexes []string) bool
HasCommand(command string) bool HasCommand(command string) bool
FileContent(file string) string FileContent(file string) string
FolderList(path string) []string LsDir(path string) []fs.DirEntry
RunCommand(command string, args ...string) (string, error) RunCommand(command string, args ...string) (string, error)
RunShellCommand(shell, command string) string RunShellCommand(shell, command string) string
ExecutionTime() float64 ExecutionTime() float64
@ -410,21 +411,21 @@ func (env *ShellEnvironment) FileContent(file string) string {
return fileContent return fileContent
} }
func (env *ShellEnvironment) FolderList(path string) []string { func (env *ShellEnvironment) LsDir(path string) []fs.DirEntry {
defer env.trace(time.Now(), "FolderList", path) defer env.trace(time.Now(), "LsDir", path)
content, err := os.ReadDir(path) entries, err := os.ReadDir(path)
if err != nil { if err != nil {
env.log(Error, "FolderList", err.Error()) env.log(Error, "LsDir", err.Error())
return nil return nil
} }
var folderNames []string env.debugF("LsDir", func() string {
for _, s := range content { var entriesStr string
if s.IsDir() { for _, entry := range entries {
folderNames = append(folderNames, s.Name()) entriesStr += entry.Name() + "\n"
} }
} return entriesStr
env.debugF("FolderList", func() string { return strings.Join(folderNames, ",") }) })
return folderNames return entries
} }
func (env *ShellEnvironment) PathSeparator() string { func (env *ShellEnvironment) PathSeparator() string {

View file

@ -1,6 +1,7 @@
package mock package mock
import ( import (
"io/fs"
"oh-my-posh/environment" "oh-my-posh/environment"
"github.com/distatus/battery" "github.com/distatus/battery"
@ -46,9 +47,9 @@ func (env *MockedEnvironment) FileContent(file string) string {
return args.String(0) return args.String(0)
} }
func (env *MockedEnvironment) FolderList(path string) []string { func (env *MockedEnvironment) LsDir(path string) []fs.DirEntry {
args := env.Called(path) args := env.Called(path)
return args.Get(0).([]string) return args.Get(0).([]fs.DirEntry)
} }
func (env *MockedEnvironment) PathSeparator() string { func (env *MockedEnvironment) PathSeparator() string {

View file

@ -495,8 +495,14 @@ func (g *Git) getWorktreeContext() int {
if !g.env.HasFolder(g.gitRootFolder + "/worktrees") { if !g.env.HasFolder(g.gitRootFolder + "/worktrees") {
return 0 return 0
} }
worktreeFolders := g.env.FolderList(g.gitRootFolder + "/worktrees") worktreeFolders := g.env.LsDir(g.gitRootFolder + "/worktrees")
return len(worktreeFolders) var count int
for _, folder := range worktreeFolders {
if folder.IsDir() {
count++
}
}
return count
} }
func (g *Git) getOriginURL(upstream string) string { func (g *Git) getOriginURL(upstream string) string {