diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2021-03-31 23:26:53 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-04-02 11:00:15 +1100 |
commit | 9e85d37fb949bbc83f28cb079f2ac4b45ae895ce (patch) | |
tree | baec08e7f7616129d1a23a1834ac9146138efe87 /pkg/gui/filetree | |
parent | 8dee06f83a1aebe9eb085e67b790f166f9d205af (diff) |
refactor to no longer call these things file changes
Diffstat (limited to 'pkg/gui/filetree')
-rw-r--r-- | pkg/gui/filetree/build_tree.go | 24 | ||||
-rw-r--r-- | pkg/gui/filetree/commit_file_change_node.go | 162 | ||||
-rw-r--r-- | pkg/gui/filetree/commit_file_manager.go (renamed from pkg/gui/filetree/commit_file_change_manager.go) | 36 | ||||
-rw-r--r-- | pkg/gui/filetree/commit_file_node.go | 162 | ||||
-rw-r--r-- | pkg/gui/filetree/file_manager.go (renamed from pkg/gui/filetree/file_change_manager.go) | 34 | ||||
-rw-r--r-- | pkg/gui/filetree/file_manager_test.go (renamed from pkg/gui/filetree/file_change_manager_test.go) | 18 | ||||
-rw-r--r-- | pkg/gui/filetree/file_node.go (renamed from pkg/gui/filetree/file_change_node.go) | 74 | ||||
-rw-r--r-- | pkg/gui/filetree/file_node_test.go (renamed from pkg/gui/filetree/file_change_node_test.go) | 30 |
8 files changed, 270 insertions, 270 deletions
diff --git a/pkg/gui/filetree/build_tree.go b/pkg/gui/filetree/build_tree.go index d6fe9b98d..ab7ac40a4 100644 --- a/pkg/gui/filetree/build_tree.go +++ b/pkg/gui/filetree/build_tree.go @@ -9,10 +9,10 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" ) -func BuildTreeFromFiles(files []*models.File) *FileChangeNode { - root := &FileChangeNode{} +func BuildTreeFromFiles(files []*models.File) *FileNode { + root := &FileNode{} - var curr *FileChangeNode + var curr *FileNode for _, file := range files { split := strings.Split(file.Name, string(os.PathSeparator)) curr = root @@ -33,7 +33,7 @@ func BuildTreeFromFiles(files []*models.File) *FileChangeNode { } } - newChild := &FileChangeNode{ + newChild := &FileNode{ Path: path, File: setFile, } @@ -49,17 +49,17 @@ func BuildTreeFromFiles(files []*models.File) *FileChangeNode { return root } -func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode { +func BuildFlatTreeFromCommitFiles(files []*models.CommitFile) *CommitFileNode { rootAux := BuildTreeFromCommitFiles(files) sortedFiles := rootAux.GetLeaves() - return &CommitFileChangeNode{Children: sortedFiles} + return &CommitFileNode{Children: sortedFiles} } -func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode { - root := &CommitFileChangeNode{} +func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileNode { + root := &CommitFileNode{} - var curr *CommitFileChangeNode + var curr *CommitFileNode for _, file := range files { split := strings.Split(file.Name, string(os.PathSeparator)) curr = root @@ -80,7 +80,7 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode } } - newChild := &CommitFileChangeNode{ + newChild := &CommitFileNode{ Path: path, File: setFile, } @@ -96,7 +96,7 @@ func BuildTreeFromCommitFiles(files []*models.CommitFile) *CommitFileChangeNode return root } -func BuildFlatTreeFromFiles(files []*models.File) *FileChangeNode { +func BuildFlatTreeFromFiles(files []*models.File) *FileNode { rootAux := BuildTreeFromFiles(files) sortedFiles := rootAux.GetLeaves() @@ -106,5 +106,5 @@ func BuildFlatTreeFromFiles(files []*models.File) *FileChangeNode { return sortedFiles[i].File != nil && sortedFiles[i].File.HasMergeConflicts && !(sortedFiles[j].File != nil && sortedFiles[j].File.HasMergeConflicts) }) - return &FileChangeNode{Children: sortedFiles} + return &FileNode{Children: sortedFiles} } diff --git a/pkg/gui/filetree/commit_file_change_node.go b/pkg/gui/filetree/commit_file_change_node.go deleted file mode 100644 index 011c1d954..000000000 --- a/pkg/gui/filetree/commit_file_change_node.go +++ /dev/null @@ -1,162 +0,0 @@ -package filetree - -import ( - "os" - "path/filepath" - "strings" - - "github.com/jesseduffield/lazygit/pkg/commands/models" -) - -type CommitFileChangeNode struct { - Children []*CommitFileChangeNode - File *models.CommitFile - Path string // e.g. '/path/to/mydir' - CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode -} - -// methods satisfying ListItem interface - -func (s *CommitFileChangeNode) ID() string { - return s.GetPath() -} - -func (s *CommitFileChangeNode) Description() string { - return s.GetPath() -} - -// methods satisfying INode interface - -func (s *CommitFileChangeNode) IsLeaf() bool { - return s.File != nil -} - -func (s *CommitFileChangeNode) GetPath() string { - return s.Path -} - -func (s *CommitFileChangeNode) GetChildren() []INode { - result := make([]INode, len(s.Children)) - for i, child := range s.Children { - result[i] = child - } - - return result -} - -func (s *CommitFileChangeNode) SetChildren(children []INode) { - castChildren := make([]*CommitFileChangeNode, len(children)) - for i, child := range children { - castChildren[i] = child.(*CommitFileChangeNode) - } - - s.Children = castChildren -} - -func (s *CommitFileChangeNode) GetCompressionLevel() int { - return s.CompressionLevel -} - -func (s *CommitFileChangeNode) SetCompressionLevel(level int) { - s.CompressionLevel = level -} - -// methods utilising generic functions for INodes - -func (s *CommitFileChangeNode) Sort() { - sortNode(s) -} - -func (s *CommitFileChangeNode) ForEachFile(cb func(*models.CommitFile) error) error { - return forEachLeaf(s, func(n INode) error { - castNode := n.(*CommitFileChangeNode) - return cb(castNode.File) - }) -} - -func (s *CommitFileChangeNode) Any(test func(node *CommitFileChangeNode) bool) bool { - return any(s, func(n INode) bool { - castNode := n.(*CommitFileChangeNode) - return test(castNode) - }) -} - -func (s *CommitFileChangeNode) Every(test func(node *CommitFileChangeNode) bool) bool { - return every(s, func(n INode) bool { - castNode := n.(*CommitFileChangeNode) - return test(castNode) - }) -} - -func (s *CommitFileChangeNode) EveryFile(test func(file *models.CommitFile) bool) bool { - return every(s, func(n INode) bool { - castNode := n.(*CommitFileChangeNode) - - return castNode.File == nil || test(castNode.File) - }) -} - -func (n *CommitFileChangeNode) Flatten(collapsedPaths map[string]bool) []*CommitFileChangeNode { - results := flatten(n, collapsedPaths) - nodes := make([]*CommitFileChangeNode, len(results)) - for i, result := range results { - nodes[i] = result.(*CommitFileChangeNode) - } - - return nodes -} - -func (node *CommitFileChangeNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileChangeNode { - return getNodeAtIndex(node, index, collapsedPaths).(*CommitFileChangeNode) -} - -func (node *CommitFileChangeNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { - return getIndexForPath(node, path, collapsedPaths) -} - -func (node *CommitFileChangeNode) Size(collapsedPaths map[string]bool) int { - return size(node, collapsedPaths) -} - -func (s *CommitFileChangeNode) Compress() { - // with these functions I try to only have type conversion code on the actual struct, - // but comparing interface values to nil is fraught with danger so I'm duplicating - // that code here. - if s == nil { - return - } - - compressAux(s) -} - -// This ignores the root -func (node *CommitFileChangeNode) GetPathsMatching(test func(*CommitFileChangeNode) bool) []string { - return getPathsMatching(node, func(n INode) bool { - return test(n.(*CommitFileChangeNode)) - }) -} - -func (s *CommitFileChangeNode) GetLeaves() []*CommitFileChangeNode { - leaves := getLeaves(s) - castLeaves := make([]*CommitFileChangeNode, len(leaves)) - for i := range leaves { - castLeaves[i] = leaves[i].(*CommitFileChangeNode) - } - - return castLeaves -} - -// extra methods - -func (s *CommitFileChangeNode) AnyFile(test func(file *models.CommitFile) bool) bool { - return s.Any(func(node *CommitFileChangeNode) bool { - return node.IsLeaf() && test(node.File) - }) -} - -func (s *CommitFileChangeNode) NameAtDepth(depth int) string { - splitName := strings.Split(s.Path, string(os.PathSeparator)) - name := filepath.Join(splitName[depth:]...) - - return name -} diff --git a/pkg/gui/filetree/commit_file_change_manager.go b/pkg/gui/filetree/commit_file_manager.go index ee05ca3b9..3fd89b63e 100644 --- a/pkg/gui/filetree/commit_file_change_manager.go +++ b/pkg/gui/filetree/commit_file_manager.go @@ -7,9 +7,9 @@ import ( "github.com/sirupsen/logrus" ) -type CommitFileChangeManager struct { +type CommitFileManager struct { files []*models.CommitFile - tree *CommitFileChangeNode + tree *CommitFileNode showTree bool log *logrus.Entry collapsedPaths CollapsedPaths @@ -17,12 +17,12 @@ type CommitFileChangeManager struct { parent string } -func (m *CommitFileChangeManager) GetParent() string { +func (m *CommitFileManager) GetParent() string { return m.parent } -func NewCommitFileChangeManager(files []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileChangeManager { - return &CommitFileChangeManager{ +func NewCommitFileManager(files []*models.CommitFile, log *logrus.Entry, showTree bool) *CommitFileManager { + return &CommitFileManager{ files: files, log: log, showTree: showTree, @@ -30,26 +30,26 @@ func NewCommitFileChangeManager(files []*models.CommitFile, log *logrus.Entry, s } } -func (m *CommitFileChangeManager) ExpandToPath(path string) { +func (m *CommitFileManager) ExpandToPath(path string) { m.collapsedPaths.ExpandToPath(path) } -func (m *CommitFileChangeManager) ToggleShowTree() { +func (m *CommitFileManager) ToggleShowTree() { m.showTree = !m.showTree m.SetTree() } -func (m *CommitFileChangeManager) GetItemAtIndex(index int) *CommitFileChangeNode { +func (m *CommitFileManager) GetItemAtIndex(index int) *CommitFileNode { // need to traverse the three depth first until we get to the index. return m.tree.GetNodeAtIndex(index+1, m.collapsedPaths) // ignoring root } -func (m *CommitFileChangeManager) GetIndexForPath(path string) (int, bool) { +func (m *CommitFileManager) GetIndexForPath(path string) (int, bool) { index, found := m.tree.GetIndexForPath(path, m.collapsedPaths) return index - 1, found } -func (m *CommitFileChangeManager) GetAllItems() []*CommitFileChangeNode { +func (m *CommitFileManager) GetAllItems() []*CommitFileNode { if m.tree == nil { return nil } @@ -57,22 +57,22 @@ func (m *CommitFileChangeManager) GetAllItems() []*CommitFileChangeNode { return m.tree.Flatten(m.collapsedPaths)[1:] // ignoring root } -func (m *CommitFileChangeManager) GetItemsLength() int { +func (m *CommitFileManager) GetItemsLength() int { return m.tree.Size(m.collapsedPaths) - 1 // ignoring root } -func (m *CommitFileChangeManager) GetAllFiles() []*models.CommitFile { +func (m *CommitFileManager) GetAllFiles() []*models.CommitFile { return m.files } -func (m *CommitFileChangeManager) SetFiles(files []*models.CommitFile, parent string) { +func (m *CommitFileManager) SetFiles(files []*models.CommitFile, parent string) { m.files = files m.parent = parent m.SetTree() } -func (m *CommitFileChangeManager) SetTree() { +func (m *CommitFileManager) SetTree() { if m.showTree { m.tree = BuildTreeFromCommitFiles(m.files) } else { @@ -80,17 +80,17 @@ func (m *CommitFileChangeManager) SetTree() { } } -func (m *CommitFileChangeManager) IsCollapsed(path string) bool { +func (m *CommitFileManager) IsCollapsed(path string) bool { return m.collapsedPaths.IsCollapsed(path) } -func (m *CommitFileChangeManager) ToggleCollapsed(path string) { +func (m *CommitFileManager) ToggleCollapsed(path string) { m.collapsedPaths.ToggleCollapsed(path) } -func (m *CommitFileChangeManager) Render(diffName string, patchManager *patch.PatchManager) []string { +func (m *CommitFileManager) Render(diffName string, patchManager *patch.PatchManager) []string { return renderAux(m.tree, m.collapsedPaths, "", -1, func(n INode, depth int) string { - castN := n.(*CommitFileChangeNode) + castN := n.(*CommitFileNode) // This is a little convoluted because we're dealing with either a leaf or a non-leaf. // But this code actually applies to both. If it's a leaf, the status will just diff --git a/pkg/gui/filetree/commit_file_node.go b/pkg/gui/filetree/commit_file_node.go new file mode 100644 index 000000000..1f1be2a3f --- /dev/null +++ b/pkg/gui/filetree/commit_file_node.go @@ -0,0 +1,162 @@ +package filetree + +import ( + "os" + "path/filepath" + "strings" + + "github.com/jesseduffield/lazygit/pkg/commands/models" +) + +type CommitFileNode struct { + Children []*CommitFileNode + File *models.CommitFile + Path string // e.g. '/path/to/mydir' + CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode +} + +// methods satisfying ListItem interface + +func (s *CommitFileNode) ID() string { + return s.GetPath() +} + +func (s *CommitFileNode) Description() string { + return s.GetPath() +} + +// methods satisfying INode interface + +func (s *CommitFileNode) IsLeaf() bool { + return s.File != nil +} + +func (s *CommitFileNode) GetPath() string { + return s.Path +} + +func (s *CommitFileNode) GetChildren() []INode { + result := make([]INode, len(s.Children)) + for i, child := range s.Children { + result[i] = child + } + + return result +} + +func (s *CommitFileNode) SetChildren(children []INode) { + castChildren := make([]*CommitFileNode, len(children)) + for i, child := range children { + castChildren[i] = child.(*CommitFileNode) + } + + s.Children = castChildren +} + +func (s *CommitFileNode) GetCompressionLevel() int { + return s.CompressionLevel +} + +func (s *CommitFileNode) SetCompressionLevel(level int) { + s.CompressionLevel = level +} + +// methods utilising generic functions for INodes + +func (s *CommitFileNode) Sort() { + sortNode(s) +} + +func (s *CommitFileNode) ForEachFile(cb func(*models.CommitFile) error) error { + return forEachLeaf(s, func(n INode) error { + castNode := n.(*CommitFileNode) + return cb(castNode.File) + }) +} + +func (s *CommitFileNode) Any(test func(node *CommitFileNode) bool) bool { + return any(s, func(n INode) bool { + castNode := n.(*CommitFileNode) + return test(castNode) + }) +} + +func (s *CommitFileNode) Every(test func(node *CommitFileNode) bool) bool { + return every(s, func(n INode) bool { + castNode := n.(*CommitFileNode) + return test(castNode) + }) +} + +func (s *CommitFileNode) EveryFile(test func(file *models.CommitFile) bool) bool { + return every(s, func(n INode) bool { + castNode := n.(*CommitFileNode) + + return castNode.File == nil || test(castNode.File) + }) +} + +func (n *CommitFileNode) Flatten(collapsedPaths map[string]bool) []*CommitFileNode { + results := flatten(n, collapsedPaths) + nodes := make([]*CommitFileNode, len(results)) + for i, result := range results { + nodes[i] = result.(*CommitFileNode) + } + + return nodes +} + +func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *CommitFileNode { + return getNodeAtIndex(node, index, collapsedPaths).(*CommitFileNode) +} + +func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { + return getIndexForPath(node, path, collapsedPaths) +} + +func (node *CommitFileNode) Size(collapsedPaths map[string]bool) int { + return size(node, collapsedPaths) +} + +func (s *CommitFileNode) Compress() { + // with these functions I try to only have type conversion code on the actual struct, + // but comparing interface values to nil is fraught with danger so I'm duplicating + // that code here. + if s == nil { + return + } + + compressAux(s) +} + +// This ignores the root +func (node *CommitFileNode) GetPathsMatching(test func(*CommitFileNode) bool) []string { + return getPathsMatching(node, func(n INode) bool { + return test(n.(*CommitFileNode)) + }) +} + +func (s *CommitFileNode) GetLeaves() []*CommitFileNode { + leaves := getLeaves(s) + castLeaves := make([]*CommitFileNode, len(leaves)) + for i := range leaves { + castLeaves[i] = leaves[i].(*CommitFileNode) + } + + return castLeaves +} + +// extra methods + +func (s *CommitFileNode) AnyFile(test func(file *models.CommitFile) bool) bool { + return s.Any(func(node *CommitFileNode) bool { + return node.IsLeaf() && test(node.File) + }) +} + +func (s *CommitFileNode) NameAtDepth(depth int) string { + splitName := strings.Split(s.Path, string(os.PathSeparator)) + name := filepath.Join(splitName[depth:]...) + + return name +} diff --git a/pkg/gui/filetree/file_change_manager.go b/pkg/gui/filetree/file_manager.go index b663e127b..6fd688bc2 100644 --- a/pkg/gui/filetree/file_change_manager.go +++ b/pkg/gui/filetree/file_manager.go @@ -6,16 +6,16 @@ import ( "github.com/sirupsen/logrus" ) -type FileChangeManager struct { +type FileManager struct { files []*models.File - tree *FileChangeNode + tree *FileNode showTree bool log *logrus.Entry collapsedPaths CollapsedPaths } -func NewFileChangeManager(files []*models.File, log *logrus.Entry, showTree bool) *FileChangeManager { - return &FileChangeManager{ +func NewFileChangeManager(files []*models.File, log *logrus.Entry, showTree bool) *FileManager { + return &FileManager{ files: files, log: log, showTree: showTree, @@ -23,26 +23,26 @@ func NewFileChangeManager(files []*models.File, log *logrus.Entry, showTree bool } } -func (m *FileChangeManager) ExpandToPath(path string) { +func (m *FileManager) ExpandToPath(path string) { m.collapsedPaths.ExpandToPath(path) } -func (m *FileChangeManager) ToggleShowTree() { +func (m *FileManager) ToggleShowTree() { m.showTree = !m.showTree m.SetTree() } -func (m *FileChangeManager) GetItemAtIndex(index int) *FileChangeNode { +func (m *FileManager) GetItemAtIndex(index int) *FileNode { // need to traverse the three depth first until we get to the index. return m.tree.GetNodeAtIndex(index+1, m.collapsedPaths) // ignoring root } -func (m *FileChangeManager) GetIndexForPath(path string) (int, bool) { +func (m *FileManager) GetIndexForPath(path string) (int, bool) { index, found := m.tree.GetIndexForPath(path, m.collapsedPaths) return index - 1, found } -func (m *FileChangeManager) GetAllItems() []*FileChangeNode { +func (m *FileManager) GetAllItems() []*FileNode { if m.tree == nil { return nil } @@ -50,21 +50,21 @@ func (m *FileChangeManager) GetAllItems() []*FileChangeNode { return m.tree.Flatten(m.collapsedPaths)[1:] // ignoring root } -func (m *FileChangeManager) GetItemsLength() int { +func (m *FileManager) GetItemsLength() int { return m.tree.Size(m.collapsedPaths) - 1 // ignoring root } -func (m *FileChangeManager) GetAllFiles() []*models.File { +func (m *FileManager) GetAllFiles() []*models.File { return m.files } -func (m *FileChangeManager) SetFiles(files []*models.File) { +func (m *FileManager) SetFiles(files []*models.File) { m.files = files m.SetTree() } -func (m *FileChangeManager) SetTree() { +func (m *FileManager) SetTree() { if m.showTree { m.tree = BuildTreeFromFiles(m.files) } else { @@ -72,17 +72,17 @@ func (m *FileChangeManager) SetTree() { } } -func (m *FileChangeManager) IsCollapsed(path string) bool { +func (m *FileManager) IsCollapsed(path string) bool { return m.collapsedPaths.IsCollapsed(path) } -func (m *FileChangeManager) ToggleCollapsed(path string) { +func (m *FileManager) ToggleCollapsed(path string) { m.collapsedPaths.ToggleCollapsed(path) } -func (m *FileChangeManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string { +func (m *FileManager) Render(diffName string, submoduleConfigs []*models.SubmoduleConfig) []string { return renderAux(m.tree, m.collapsedPaths, "", -1, func(n INode, depth int) string { - castN := n.(*FileChangeNode) + castN := n.(*FileNode) return presentation.GetFileLine(castN.GetHasUnstagedChanges(), castN.GetHasStagedChanges(), castN.NameAtDepth(depth), diffName, submoduleConfigs, castN.File) }) } diff --git a/pkg/gui/filetree/file_change_manager_test.go b/pkg/gui/filetree/file_manager_test.go index fb9022fec..038c03364 100644 --- a/pkg/gui/filetree/file_change_manager_test.go +++ b/pkg/gui/filetree/file_manager_test.go @@ -10,7 +10,7 @@ import ( func TestRender(t *testing.T) { scenarios := []struct { name string - root *FileChangeNode + root *FileNode collapsedPaths map[string]bool expected []string }{ @@ -21,9 +21,9 @@ func TestRender(t *testing.T) { }, { name: "leaf node", - root: &FileChangeNode{ + root: &FileNode{ Path: "", - Children: []*FileChangeNode{ + Children: []*FileNode{ {File: &models.File{Name: "test", ShortStatus: " M", HasStagedChanges: true}, Path: "test"}, }, }, @@ -31,12 +31,12 @@ func TestRender(t *testing.T) { }, { name: "big example", - root: &FileChangeNode{ + root: &FileNode{ Path: "", - Children: []*FileChangeNode{ + Children: []*FileNode{ { Path: "dir1", - Children: []*FileChangeNode{ + Children: []*FileNode{ { File: &models.File{Name: "dir1/file2", ShortStatus: "M ", HasUnstagedChanges: true}, Path: "dir1/file2", @@ -49,10 +49,10 @@ func TestRender(t *testing.T) { }, { Path: "dir2", - Children: []*FileChangeNode{ + Children: []*FileNode{ { Path: "dir2/dir2", - Children: []*FileChangeNode{ + Children: []*FileNode{ { File: &models.File{Name: "dir2/dir2/file3", ShortStatus: " M", HasStagedChanges: true}, Path: "dir2/dir2/file3", @@ -83,7 +83,7 @@ func TestRender(t *testing.T) { for _, s := range scenarios { s := s t.Run(s.name, func(t *testing.T) { - mngr := &FileChangeManager{tree: s.root, collapsedPaths: s.collapsedPaths} + mngr := &FileManager{tree: s.root, collapsedPaths: s.collapsedPaths} result := mngr.Render("", nil) assert.EqualValues(t, s.expected, result) }) diff --git a/pkg/gui/filetree/file_change_node.go b/pkg/gui/filetree/file_node.go index a99a47ac1..cf3e69aa8 100644 --- a/pkg/gui/filetree/file_change_node.go +++ b/pkg/gui/filetree/file_node.go @@ -9,8 +9,8 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/models" ) -type FileChangeNode struct { - Children []*FileChangeNode +type FileNode struct { + Children []*FileNode File *models.File Path string // e.g. '/path/to/mydir' CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode @@ -18,25 +18,25 @@ type FileChangeNode struct { // methods satisfying ListItem interface -func (s *FileChangeNode) ID() string { +func (s *FileNode) ID() string { return s.GetPath() } -func (s *FileChangeNode) Description() string { +func (s *FileNode) Description() string { return s.GetPath() } // methods satisfying INode interface -func (s *FileChangeNode) IsLeaf() bool { +func (s *FileNode) IsLeaf() bool { return s.File != nil } -func (s *FileChangeNode) GetPath() string { +func (s *FileNode) GetPath() string { return s.Path } -func (s *FileChangeNode) GetChildren() []INode { +func (s *FileNode) GetChildren() []INode { result := make([]INode, len(s.Children)) for i, child := range s.Children { result[i] = child @@ -45,66 +45,66 @@ func (s *FileChangeNode) GetChildren() []INode { return result } -func (s *FileChangeNode) SetChildren(children []INode) { - castChildren := make([]*FileChangeNode, len(children)) +func (s *FileNode) SetChildren(children []INode) { + castChildren := make([]*FileNode, len(children)) for i, child := range children { - castChildren[i] = child.(*FileChangeNode) + castChildren[i] = child.(*FileNode) } s.Children = castChildren } -func (s *FileChangeNode) GetCompressionLevel() int { +func (s *FileNode) GetCompressionLevel() int { return s.CompressionLevel } -func (s *FileChangeNode) SetCompressionLevel(level int) { +func (s *FileNode) SetCompressionLevel(level int) { s.CompressionLevel = level } // methods utilising generic functions for INodes -func (s *FileChangeNode) Sort() { +func (s *FileNode) Sort() { sortNode(s) } -func (s *FileChangeNode) ForEachFile(cb func(*models.File) error) error { +func (s *FileNode) ForEachFile(cb func(*models.File) error) error { return forEachLeaf(s, func(n INode) error { - castNode := n.(*FileChangeNode) + castNode := n.(*FileNode) return cb(castNode.File) }) } -func (s *FileChangeNode) Any(test func(node *FileChangeNode) bool) bool { +func (s *FileNode) Any(test func(node *FileNode) bool) bool { return any(s, func(n INode) bool { - castNode := n.(*FileChangeNode) + castNode := n.(*FileNode) return test(castNode) }) } -func (n *FileChangeNode) Flatten(collapsedPaths map[string]bool) []*FileChangeNode { +func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode { results := flatten(n, collapsedPaths) - nodes := make([]*FileChangeNode, len(results)) + nodes := make([]*FileNode, len(results)) for i, result := range results { - nodes[i] = result.(*FileChangeNode) + nodes[i] = result.(*FileNode) } return nodes } -func (node *FileChangeNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileChangeNode { - return getNodeAtIndex(node, index, collapsedPaths).(*FileChangeNode) +func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileNode { + return getNodeAtIndex(node, index, collapsedPaths).(*FileNode) } -func (node *FileChangeNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { +func (node *FileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) { return getIndexForPath(node, path, collapsedPaths) } -func (node *FileChangeNode) Size(collapsedPaths map[string]bool) int { +func (node *FileNode) Size(collapsedPaths map[string]bool) int { return size(node, collapsedPaths) } -func (s *FileChangeNode) Compress() { +func (s *FileNode) Compress() { // with these functions I try to only have type conversion code on the actual struct, // but comparing interface values to nil is fraught with danger so I'm duplicating // that code here. @@ -116,17 +116,17 @@ func (s *FileChangeNode) Compress() { } // This ignores the root -func (node *FileChangeNode) GetPathsMatching(test func(*FileChangeNode) bool) []string { +func (node *FileNode) GetPathsMatching(test func(*FileNode) bool) []string { return getPathsMatching(node, func(n INode) bool { - return test(n.(*FileChangeNode)) + return test(n.(*FileNode)) }) } -func (s *FileChangeNode) GetLeaves() []*FileChangeNode { +func (s *FileNode) GetLeaves() []*FileNode { leaves := getLeaves(s) - castLeaves := make([]*FileChangeNode, len(leaves)) + castLeaves := make([]*FileNode, len(leaves)) for i := range leaves { - castLeaves[i] = leaves[i].(*FileChangeNode) + castLeaves[i] = leaves[i].(*FileNode) } return castLeaves @@ -134,29 +134,29 @@ func (s *FileChangeNode) GetLeaves() []*FileChangeNode { // extra methods -func (s *FileChangeNode) GetHasUnstagedChanges() bool { +func (s *FileNode) GetHasUnstagedChanges() bool { return s.AnyFile(func(file *models.File) bool { return file.HasUnstagedChanges }) } -func (s *FileChangeNode) GetHasStagedChanges() bool { +func (s *FileNode) GetHasStagedChanges() bool { return s.AnyFile(func(file *models.File) bool { return file.HasStagedChanges }) } -func (s *FileChangeNode) GetHasInlineMergeConflicts() bool { +func (s *FileNode) GetHasInlineMergeConflicts() bool { return s.AnyFile(func(file *models.File) bool { return file.HasInlineMergeConflicts }) } -func (s *FileChangeNode) GetIsTracked() bool { +func (s *FileNode) 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 { +func (s *FileNode) AnyFile(test func(file *models.File) bool) bool { + return s.Any(func(node *FileNode) bool { return node.IsLeaf() && test(node.File) }) } -func (s *FileChangeNode) NameAtDepth(depth int) string { +func (s *FileNode) NameAtDepth(depth int) string { splitName := strings.Split(s.Path, string(os.PathSeparator)) name := filepath.Join(splitName[depth:]...) diff --git a/pkg/gui/filetree/file_change_node_test.go b/pkg/gui/filetree/file_node_test.go index dbb21a6cd..d47754e69 100644 --- a/pkg/gui/filetree/file_change_node_test.go +++ b/pkg/gui/filetree/file_node_test.go @@ -10,8 +10,8 @@ import ( func TestCompress(t *testing.T) { scenarios := []struct { name string - root |