diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-03-22 08:11:39 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-03-30 21:57:00 +1100 |
commit | 3103247e8f6b24bc2c29739d052036f98c11859e (patch) | |
tree | 7b0992ebcf61bea24ade1a5c4e2ee497b4f0f4b1 /pkg/gui/filetree | |
parent | 1629a7d2802bfd1becafde8b69cca513f300c330 (diff) |
refactor
Diffstat (limited to 'pkg/gui/filetree')
-rw-r--r-- | pkg/gui/filetree/file_change_node.go | 93 |
1 files changed, 41 insertions, 52 deletions
diff --git a/pkg/gui/filetree/file_change_node.go b/pkg/gui/filetree/file_change_node.go index 240525cba..0515f976c 100644 --- a/pkg/gui/filetree/file_change_node.go +++ b/pkg/gui/filetree/file_change_node.go @@ -29,12 +29,53 @@ func (s *FileChangeNode) GetHasInlineMergeConflicts() bool { return s.AnyFile(func(file *models.File) bool { return file.HasInlineMergeConflicts }) } +func (s *FileChangeNode) GetIsTracked() bool { + return s.AnyFile(func(file *models.File) bool { return file.Tracked }) +} + func (s *FileChangeNode) AnyFile(test func(file *models.File) bool) bool { return s.Any(func(node *FileChangeNode) bool { return node.IsLeaf() && test(node.File) }) } +func (s *FileChangeNode) ForEachFile(cb func(*models.File) error) error { + if s.File != nil { + if err := cb(s.File); err != nil { + return err + } + } + + for _, child := range s.Children { + if err := child.ForEachFile(cb); err != nil { + return err + } + } + + return nil +} + +func (s *FileChangeNode) NameAtDepth(depth int) string { + splitName := strings.Split(s.Path, 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 := len(splitName) == len(splitPrevName) && 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 +} + func (s *FileChangeNode) Any(test func(node *FileChangeNode) bool) bool { if test(s) { return true @@ -158,21 +199,6 @@ func (s *FileChangeNode) sortChildren() { s.Children = sortedChildren } -// returns true if any descendant file is tracked -func (s *FileChangeNode) GetIsTracked() bool { - if s.File != nil { - return s.File.GetIsTracked() - } - - for _, child := range s.Children { - if child.GetIsTracked() { - return true - } - } - - return false -} - func (s *FileChangeNode) GetPath() string { return s.Path } @@ -233,22 +259,6 @@ func (s *FileChangeNode) Description() string { return s.GetPath() } -func (s *FileChangeNode) ForEachFile(cb func(*models.File) error) error { - if s.File != nil { - if err := cb(s.File); err != nil { - return err - } - } - - for _, child := range s.Children { - if err := child.ForEachFile(cb); err != nil { - return err - } - } - - return nil -} - func (s *FileChangeNode) GetLeaves() []*FileChangeNode { if s.IsLeaf() { return []*FileChangeNode{s} @@ -261,24 +271,3 @@ func (s *FileChangeNode) GetLeaves() []*FileChangeNode { return output } - -func (s *FileChangeNode) NameAtDepth(depth int) string { - splitName := strings.Split(s.Path, 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 := len(splitName) == len(splitPrevName) && 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 -} |