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

View file

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

View file

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