2021-03-31 14:26:53 +02:00
|
|
|
package filetree
|
|
|
|
|
|
|
|
import (
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2021-03-31 14:26:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2022-01-16 05:46:53 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2021-03-31 14:26:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-19 00:38:49 +02:00
|
|
|
var (
|
|
|
|
_ INode = &CommitFileNode{}
|
|
|
|
_ types.ListItem = &CommitFileNode{}
|
|
|
|
)
|
2021-03-31 14:26:53 +02:00
|
|
|
|
|
|
|
func (s *CommitFileNode) ID() string {
|
|
|
|
return s.GetPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommitFileNode) Description() string {
|
|
|
|
return s.GetPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// methods satisfying INode interface
|
|
|
|
|
2022-01-21 15:13:51 +02:00
|
|
|
func (s *CommitFileNode) IsNil() bool {
|
|
|
|
return s == nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
func (s *CommitFileNode) IsLeaf() bool {
|
|
|
|
return s.File != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommitFileNode) GetPath() string {
|
|
|
|
return s.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommitFileNode) GetChildren() []INode {
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(s.Children, func(child *CommitFileNode) INode {
|
|
|
|
return child
|
|
|
|
})
|
2021-03-31 14:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommitFileNode) SetChildren(children []INode) {
|
2022-03-19 10:12:58 +02:00
|
|
|
castChildren := slices.Map(children, func(child INode) *CommitFileNode {
|
|
|
|
return child.(*CommitFileNode)
|
|
|
|
})
|
2021-03-31 14:26:53 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-19 03:26:30 +02:00
|
|
|
func (n *CommitFileNode) Flatten(collapsedPaths *CollapsedPaths) []*CommitFileNode {
|
2021-03-31 14:26:53 +02:00
|
|
|
results := flatten(n, collapsedPaths)
|
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(results, func(result INode) *CommitFileNode {
|
|
|
|
return result.(*CommitFileNode)
|
|
|
|
})
|
2021-03-31 14:26:53 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:26:30 +02:00
|
|
|
func (node *CommitFileNode) GetNodeAtIndex(index int, collapsedPaths *CollapsedPaths) *CommitFileNode {
|
2021-04-03 04:43:43 +02:00
|
|
|
if node == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-05 06:24:28 +02:00
|
|
|
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.(*CommitFileNode)
|
2021-03-31 14:26:53 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 03:26:30 +02:00
|
|
|
func (node *CommitFileNode) GetIndexForPath(path string, collapsedPaths *CollapsedPaths) (int, bool) {
|
2021-03-31 14:26:53 +02:00
|
|
|
return getIndexForPath(node, path, collapsedPaths)
|
|
|
|
}
|
|
|
|
|
2022-03-19 03:26:30 +02:00
|
|
|
func (node *CommitFileNode) Size(collapsedPaths *CollapsedPaths) int {
|
2021-04-02 01:51:17 +02:00
|
|
|
if node == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:26:53 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CommitFileNode) GetLeaves() []*CommitFileNode {
|
|
|
|
leaves := getLeaves(s)
|
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(leaves, func(leaf INode) *CommitFileNode {
|
|
|
|
return leaf.(*CommitFileNode)
|
|
|
|
})
|
2021-03-31 14:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-04-08 13:24:49 +02:00
|
|
|
splitName := split(s.Path)
|
|
|
|
name := join(splitName[depth:])
|
2021-03-31 14:26:53 +02:00
|
|
|
|
|
|
|
return name
|
|
|
|
}
|