1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/filetree/file_node.go

190 lines
4.4 KiB
Go
Raw Normal View History

2021-03-21 06:58:15 +02:00
package filetree
import (
"fmt"
2021-03-21 06:58:15 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
)
type FileNode struct {
Children []*FileNode
2021-03-21 06:58:15 +02:00
File *models.File
2021-03-21 01:46:43 +02:00
Path string // e.g. '/path/to/mydir'
2021-03-21 06:01:13 +02:00
CompressionLevel int // equal to the number of forward slashes you'll see in the path when it's rendered in tree mode
}
2021-03-30 14:56:59 +02:00
// methods satisfying ListItem interface
func (s *FileNode) ID() string {
2021-03-30 14:56:59 +02:00
return s.GetPath()
}
func (s *FileNode) Description() string {
2021-03-30 14:56:59 +02:00
return s.GetPath()
}
2021-03-30 14:56:59 +02:00
// methods satisfying INode interface
2021-03-21 23:11:39 +02:00
func (s *FileNode) IsLeaf() bool {
2021-03-30 14:56:59 +02:00
return s.File != nil
}
func (s *FileNode) GetPath() string {
2021-03-30 14:56:59 +02:00
return s.Path
2021-03-21 23:11:39 +02:00
}
func (s *FileNode) GetChildren() []INode {
2021-03-30 14:56:59 +02:00
result := make([]INode, len(s.Children))
for i, child := range s.Children {
result[i] = child
2021-03-21 23:11:39 +02:00
}
2021-03-30 14:56:59 +02:00
return result
2021-03-21 23:11:39 +02:00
}
func (s *FileNode) SetChildren(children []INode) {
castChildren := make([]*FileNode, len(children))
2021-03-30 14:56:59 +02:00
for i, child := range children {
castChildren[i] = child.(*FileNode)
}
2021-03-30 14:56:59 +02:00
s.Children = castChildren
}
func (s *FileNode) GetCompressionLevel() int {
2021-03-30 14:56:59 +02:00
return s.CompressionLevel
}
func (s *FileNode) SetCompressionLevel(level int) {
2021-03-30 14:56:59 +02:00
s.CompressionLevel = level
}
2021-03-30 14:56:59 +02:00
// methods utilising generic functions for INodes
2021-03-20 23:41:06 +02:00
func (s *FileNode) Sort() {
2021-03-30 14:56:59 +02:00
sortNode(s)
2021-03-20 23:41:06 +02:00
}
func (s *FileNode) ForEachFile(cb func(*models.File) error) error {
2021-03-30 14:56:59 +02:00
return forEachLeaf(s, func(n INode) error {
castNode := n.(*FileNode)
2021-03-30 14:56:59 +02:00
return cb(castNode.File)
})
}
func (s *FileNode) Any(test func(node *FileNode) bool) bool {
2021-03-30 14:56:59 +02:00
return any(s, func(n INode) bool {
castNode := n.(*FileNode)
2021-03-30 14:56:59 +02:00
return test(castNode)
})
}
func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode {
2021-03-30 14:56:59 +02:00
results := flatten(n, collapsedPaths)
nodes := make([]*FileNode, len(results))
2021-03-30 14:56:59 +02:00
for i, result := range results {
nodes[i] = result.(*FileNode)
}
2021-03-30 14:56:59 +02:00
return nodes
}
func (node *FileNode) GetNodeAtIndex(index int, collapsedPaths map[string]bool) *FileNode {
if node == nil {
return nil
}
result := getNodeAtIndex(node, index, collapsedPaths)
if result == nil {
// not sure how this can be nil: we probably are missing a mutex somewhere
return nil
}
return result.(*FileNode)
}
func (node *FileNode) GetIndexForPath(path string, collapsedPaths map[string]bool) (int, bool) {
2021-03-30 14:56:59 +02:00
return getIndexForPath(node, path, collapsedPaths)
}
2021-03-14 09:46:22 +02:00
func (node *FileNode) Size(collapsedPaths map[string]bool) int {
2021-04-02 01:51:17 +02:00
if node == nil {
return 0
}
2021-03-30 14:56:59 +02:00
return size(node, collapsedPaths)
2021-03-14 09:46:22 +02:00
}
func (s *FileNode) Compress() {
2021-03-30 14:56:59 +02:00
// 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
}
2021-03-30 14:56:59 +02:00
compressAux(s)
}
2021-03-30 14:56:59 +02:00
// This ignores the root
func (node *FileNode) GetPathsMatching(test func(*FileNode) bool) []string {
2021-03-30 14:56:59 +02:00
return getPathsMatching(node, func(n INode) bool {
return test(n.(*FileNode))
2021-03-30 14:56:59 +02:00
})
}
func (s *FileNode) GetLeaves() []*FileNode {
2021-03-30 14:56:59 +02:00
leaves := getLeaves(s)
castLeaves := make([]*FileNode, len(leaves))
2021-03-30 14:56:59 +02:00
for i := range leaves {
castLeaves[i] = leaves[i].(*FileNode)
}
2021-03-30 14:56:59 +02:00
return castLeaves
}
2021-03-15 13:29:34 +02:00
2021-03-30 14:56:59 +02:00
// extra methods
2021-03-15 13:29:34 +02:00
func (s *FileNode) GetHasUnstagedChanges() bool {
2021-03-30 14:56:59 +02:00
return s.AnyFile(func(file *models.File) bool { return file.HasUnstagedChanges })
}
2021-03-15 13:29:34 +02:00
func (s *FileNode) GetHasStagedChanges() bool {
2021-03-30 14:56:59 +02:00
return s.AnyFile(func(file *models.File) bool { return file.HasStagedChanges })
}
2021-03-15 13:29:34 +02:00
func (s *FileNode) GetHasInlineMergeConflicts() bool {
2021-03-30 14:56:59 +02:00
return s.AnyFile(func(file *models.File) bool { return file.HasInlineMergeConflicts })
2021-03-15 13:29:34 +02:00
}
2021-03-15 14:00:20 +02:00
func (s *FileNode) GetIsTracked() bool {
2021-03-30 14:56:59 +02:00
return s.AnyFile(func(file *models.File) bool { return file.Tracked })
2021-03-15 14:00:20 +02:00
}
func (s *FileNode) AnyFile(test func(file *models.File) bool) bool {
return s.Any(func(node *FileNode) bool {
2021-03-30 14:56:59 +02:00
return node.IsLeaf() && test(node.File)
})
2021-03-15 14:00:20 +02:00
}
2021-03-16 00:07:00 +02:00
func (s *FileNode) NameAtDepth(depth int) string {
2021-04-08 13:24:49 +02:00
splitName := split(s.Path)
name := join(splitName[depth:])
2021-03-21 01:46:43 +02:00
2021-03-30 14:56:59 +02:00
if s.File != nil && s.File.IsRename() {
2021-04-08 13:24:49 +02:00
splitPrevName := split(s.File.PreviousName)
2021-03-30 14:56:59 +02:00
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
2021-04-08 13:24:49 +02:00
sameParentDir := len(splitName) == len(splitPrevName) && join(splitName[0:depth]) == join(splitPrevName[0:depth])
2021-03-30 14:56:59 +02:00
if sameParentDir {
2021-04-08 13:24:49 +02:00
prevName = join(splitPrevName[depth:])
2021-03-30 14:56:59 +02:00
}
return fmt.Sprintf("%s%s%s", prevName, " → ", name)
2021-03-21 01:46:43 +02:00
}
2021-03-30 14:56:59 +02:00
return name
2021-03-21 01:46:43 +02:00
}