From da6fe01eca531635c09627c60bd38d49bb092906 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 21 Mar 2021 08:41:06 +1100 Subject: allow toggling on/off file tree mode --- pkg/commands/models/file.go | 15 ++++++---- pkg/commands/models/status_line_node.go | 49 ++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 7 deletions(-) (limited to 'pkg/commands/models') diff --git a/pkg/commands/models/file.go b/pkg/commands/models/file.go index 32e90f718..3e28ca46f 100644 --- a/pkg/commands/models/file.go +++ b/pkg/commands/models/file.go @@ -1,8 +1,6 @@ package models import ( - "strings" - "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -10,6 +8,7 @@ import ( // duplicating this for now type File struct { Name string + PreviousName string HasStagedChanges bool HasUnstagedChanges bool Tracked bool @@ -25,12 +24,16 @@ type File struct { const RENAME_SEPARATOR = " -> " func (f *File) IsRename() bool { - return strings.Contains(f.Name, RENAME_SEPARATOR) + return f.PreviousName != "" } // Names returns an array containing just the filename, or in the case of a rename, the after filename and the before filename func (f *File) Names() []string { - return strings.Split(f.Name, RENAME_SEPARATOR) + result := []string{f.Name} + if f.PreviousName != "" { + result = append(result, f.PreviousName) + } + return result } // returns true if the file names are the same or if a a file rename includes the filename of the other @@ -73,6 +76,6 @@ func (f *File) GetIsTracked() bool { } func (f *File) GetPath() string { - names := f.Names() - return names[len(names)-1] + // TODO: remove concept of name; just use path + return f.Name } diff --git a/pkg/commands/models/status_line_node.go b/pkg/commands/models/status_line_node.go index 3962e0c42..84dc638ba 100644 --- a/pkg/commands/models/status_line_node.go +++ b/pkg/commands/models/status_line_node.go @@ -2,7 +2,10 @@ package models import ( "fmt" + "os" + "path/filepath" "sort" + "strings" ) type StatusLineNode struct { @@ -67,6 +70,30 @@ func (s *StatusLineNode) getNodeAtIndexAux(index int, collapsedPaths map[string] return nil, offset } +func (s *StatusLineNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { + return s.getIndexForPathAux(path, collapsedPaths) +} + +func (s *StatusLineNode) getIndexForPathAux(path string, collapsedPaths map[string]bool) (int, bool) { + offset := 0 + + if s.Path == path { + return offset, true + } + + if !collapsedPaths[s.GetPath()] { + for _, child := range s.Children { + offsetChange, found := child.getIndexForPathAux(path, collapsedPaths) + offset += offsetChange + 1 + if found { + return offset, true + } + } + } + + return offset, false +} + func (s *StatusLineNode) IsLeaf() bool { return len(s.Children) == 0 } @@ -161,7 +188,6 @@ func (s *StatusLineNode) compressAux() *StatusLineNode { for i := range s.Children { for s.Children[i].HasExactlyOneChild() { grandchild := s.Children[i].Children[0] - grandchild.Name = fmt.Sprintf("%s/%s", s.Children[i].Name, grandchild.Name) s.Children[i] = grandchild } } @@ -215,3 +241,24 @@ func (s *StatusLineNode) ForEachFile(cb func(*File) error) error { return nil } + +func (s *StatusLineNode) NameAtDepth(depth int) string { + splitName := strings.Split(s.Name, string(os.PathSeparator)) + name := filepath.Join(splitName[depth:]...) + + if s.File != nil && s.File.IsRename() { + splitPrevName := strings.Split(s.File.PreviousName, string(os.PathSeparator)) + + 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 := filepath.Join(splitName[0:depth]...) == filepath.Join(splitPrevName[0:depth]...) + if sameParentDir { + prevName = filepath.Join(splitPrevName[depth:]...) + } + + return fmt.Sprintf("%s%s%s", prevName, " -> ", name) + } + + return name +} -- cgit v1.2.3