1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package filetree
import (
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
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
}
var (
_ INode = &FileNode{}
_ types.ListItem = &FileNode{}
)
func (s *FileNode) ID() string {
return s.GetPath()
}
func (s *FileNode) Description() string {
return s.GetPath()
}
// methods satisfying INode interface
// interfaces values whose concrete value is nil are not themselves nil
// hence the existence of this method
func (s *FileNode) IsNil() bool {
return s == nil
}
func (s *FileNode) IsLeaf() bool {
return s.File != nil
}
func (s *FileNode) GetPath() string {
return s.Path
}
func (s *FileNode) GetChildren() []INode {
result := make([]INode, len(s.Children))
for i, child := range s.Children {
result[i] = child
}
return result
}
func (s *FileNode) SetChildren(children []INode) {
castChildren := make([]*FileNode, len(children))
for i, child := range children {
castChildren[i] = child.(*FileNode)
}
s.Children = castChildren
}
func (s *FileNode) GetCompressionLevel() int {
return s.CompressionLevel
}
func (s *FileNode) SetCompressionLevel(level int) {
s.CompressionLevel = level
}
// methods utilising generic functions for INodes
func (s *FileNode) Sort() {
sortNode(s)
}
func (s *FileNode) ForEachFile(cb func(*models.File) error) error {
return forEachLeaf(s, func(n INode) error {
castNode := n.(*FileNode)
return cb(castNode.File)
})
}
func (s *FileNode) Any(test func(node *FileNode) bool) bool {
return any(s, func(n INode) bool {
castNode := n.(*FileNode)
return test(castNode)
})
}
func (n *FileNode) Flatten(collapsedPaths map[string]bool) []*FileNode {
results := flatten(n, collapsedPaths)
nodes := make([]*FileNode, len(results))
for i, result := range results {
nodes[i] = result.(*FileNode)
}
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) {
return getIndexForPath(node, path, collapsedPaths)
}
func (node *FileNode) Size(collapsedPaths map[string]bool) int {
if node == nil {
return 0
}
return size(node, collapsedPaths)
}
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.
if s == nil {
return
}
compressAux(s)
}
func (node *FileNode) GetFilePathsMatching(test func(*models.File) bool) []string {
return getPathsMatching(node, func(n INode) bool {
castNode := n.(*FileNode)
if castNode.File == nil {
return false
}
return test(castNode.File)
})
}
func (s *FileNode) GetLeaves() []*FileNode {
leaves := getLeaves(s)
castLeaves := make([]*FileNode, len(leaves))
for i := range leaves {
castLeaves[i] = leaves[i].(*FileNode)
}
return castLeaves
}
// extra methods
func (s *FileNode) GetHasUnstagedChanges() bool {
return s.AnyFile(func(file *models.File) bool { return file.HasUnstagedChanges })
}
func (s *FileNode) GetHasStagedChanges() bool {
return s.AnyFile(func(file *models.File) bool { return file.HasStagedChanges })
}
func (s *FileNode) GetHasInlineMergeConflicts() bool {
return s.AnyFile(func(file *models.File) bool { return file.HasInlineMergeConflicts })
}
func (s *FileNode) GetIsTracked() bool {
return s.AnyFile(func(file *models.File) bool { return file.Tracked })
}
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 *FileNode) NameAtDepth(depth int) string {
splitName := split(s.Path)
name := join(splitName[depth:])
if s.File != nil && s.File.IsRename() {
splitPrevName := split(s.File.PreviousName)
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) && join(splitName[0:depth]) == join(splitPrevName[0:depth])
if sameParentDir {
prevName = join(splitPrevName[depth:])
}
return prevName + " → " + name
}
return name
}
|