From 21049be2337c3ea8c5c30761eb9b8f5f9569950a Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 8 Apr 2021 21:24:49 +1000 Subject: support file tree mode on windows --- pkg/gui/filetree/build_tree.go | 27 ++++++++++++++++----------- pkg/gui/filetree/collapsed_paths.go | 11 +++-------- pkg/gui/filetree/commit_file_node.go | 8 ++------ pkg/gui/filetree/file_node.go | 13 +++++-------- 4 files changed, 26 insertions(+), 33 deletions(-) (limited to 'pkg/gui/filetree') diff --git a/pkg/gui/filetree/build_tree.go b/pkg/gui/filetree/build_tree.go index ab7ac40a4..128ec075c 100644 --- a/pkg/gui/filetree/build_tree.go +++ b/pkg/gui/filetree/build_tree.go @@ -1,8 +1,6 @@ package filetree import ( - "os" - "path/filepath" "sort" "strings" @@ -14,18 +12,17 @@ func BuildTreeFromFiles(files []*models.File) *FileNode { var curr *FileNode for _, file := range files { - split := strings.Split(file.Name, string(os.PathSeparator)) + splitPath := split(file.Name) curr = root outer: - for i := range split { + for i := range splitPath { var setFile *models.File - isFile := i == len(split)-1 + isFile := i == len(splitPath)-1 if isFile { setFile = file } - path := filepath.Join(split[:i+1]...) - + path := join(splitPath[:i+1]) for _, existingChild := range curr.Children { if existingChild.Path == path { curr = existingChild @@ -61,17 +58,17 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileNode { var curr *CommitFileNode for _, file := range files { - split := strings.Split(file.Name, string(os.PathSeparator)) + splitPath := split(file.Name) curr = root outer: - for i := range split { + for i := range splitPath { var setFile *models.CommitFile - isFile := i == len(split)-1 + isFile := i == len(splitPath)-1 if isFile { setFile = file } - path := filepath.Join(split[:i+1]...) + path := join(splitPath[:i+1]) for _, existingChild := range curr.Children { if existingChild.Path == path { @@ -108,3 +105,11 @@ func BuildFlatTreeFromFiles(files []*models.File) *FileNode { return &FileNode{Children: sortedFiles} } + +func split(str string) []string { + return strings.Split(str, "/") +} + +func join(strs []string) string { + return strings.Join(strs, "/") +} diff --git a/pkg/gui/filetree/collapsed_paths.go b/pkg/gui/filetree/collapsed_paths.go index 60860414f..02c0b4303 100644 --- a/pkg/gui/filetree/collapsed_paths.go +++ b/pkg/gui/filetree/collapsed_paths.go @@ -1,17 +1,12 @@ package filetree -import ( - "os" - "strings" -) - type CollapsedPaths map[string]bool func (cp CollapsedPaths) ExpandToPath(path string) { // need every directory along the way - split := strings.Split(path, string(os.PathSeparator)) - for i := range split { - dir := strings.Join(split[0:i+1], string(os.PathSeparator)) + splitPath := split(path) + for i := range splitPath { + dir := join(splitPath[0 : i+1]) cp[dir] = false } } diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go index 0edaf9c8e..173281035 100644 --- a/pkg/gui/filetree/commit_file_node.go +++ b/pkg/gui/filetree/commit_file_node.go @@ -1,10 +1,6 @@ package filetree import ( - "os" - "path/filepath" - "strings" - "github.com/jesseduffield/lazygit/pkg/commands/models" ) @@ -169,8 +165,8 @@ func (s *CommitFileNode) AnyFile(test func(file *models.CommitFile) bool) bool { } func (s *CommitFileNode) NameAtDepth(depth int) string { - splitName := strings.Split(s.Path, string(os.PathSeparator)) - name := filepath.Join(splitName[depth:]...) + splitName := split(s.Path) + name := join(splitName[depth:]) return name } diff --git a/pkg/gui/filetree/file_node.go b/pkg/gui/filetree/file_node.go index 802ff1ddf..4df8bf5bf 100644 --- a/pkg/gui/filetree/file_node.go +++ b/pkg/gui/filetree/file_node.go @@ -2,9 +2,6 @@ package filetree import ( "fmt" - "os" - "path/filepath" - "strings" "github.com/jesseduffield/lazygit/pkg/commands/models" ) @@ -171,18 +168,18 @@ func (s *FileNode) AnyFile(test func(file *models.File) bool) bool { } func (s *FileNode) NameAtDepth(depth int) string { - splitName := strings.Split(s.Path, string(os.PathSeparator)) - name := filepath.Join(splitName[depth:]...) + splitName := split(s.Path) + name := join(splitName[depth:]) if s.File != nil && s.File.IsRename() { - splitPrevName := strings.Split(s.File.PreviousName, string(os.PathSeparator)) + splitPrevName := split(s.File.PreviousName) prevName := s.File.PreviousName // if the file has just been renamed inside the same directory, we can shave off // the prefix for the previous path too. Otherwise we'll keep it unchanged - sameParentDir := len(splitName) == len(splitPrevName) && filepath.Join(splitName[0:depth]...) == filepath.Join(splitPrevName[0:depth]...) + sameParentDir := len(splitName) == len(splitPrevName) && join(splitName[0:depth]) == join(splitPrevName[0:depth]) if sameParentDir { - prevName = filepath.Join(splitPrevName[depth:]...) + prevName = join(splitPrevName[depth:]) } return fmt.Sprintf("%s%s%s", prevName, " → ", name) -- cgit v1.2.3