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
|
package models
import (
"fmt"
"sort"
)
type StatusLineNode struct {
Children []*StatusLineNode
File *File
Name string // e.g. 'mydir'
Path string // e.g. '/path/to/mydir'
Collapsed bool
}
func (s *StatusLineNode) GetShortStatus() string {
// need to see if any child has unstaged changes.
if s.IsLeaf() {
return s.File.ShortStatus
}
firstChar := " "
secondChar := " "
if s.GetHasStagedChanges() {
firstChar = "M"
}
if s.GetHasUnstagedChanges() {
secondChar = "M"
}
return firstChar + secondChar
}
func (s *StatusLineNode) GetHasUnstagedChanges() bool {
if s.IsLeaf() {
return s.File.HasUnstagedChanges
}
for _, child := range s.Children {
if child.GetHasUnstagedChanges() {
return true
}
}
return false
}
func (s *StatusLineNode) GetHasStagedChanges() bool {
if s.IsLeaf() {
return s.File.HasStagedChanges
}
for _, child := range s.Children {
if child.GetHasStagedChanges() {
return true
}
}
return false
}
func (s *StatusLineNode) GetNodeAtIndex(index int) *StatusLineNode {
node, _ := s.getNodeAtIndexAux(index)
return node
}
func (s *StatusLineNode) getNodeAtIndexAux(index int) (*StatusLineNode, int) {
offset := 1
if index == 0 {
return s, offset
}
for _, child := range s.Children {
node, offsetChange := child.getNodeAtIndexAux(index - offset)
offset += offsetChange
if node != nil {
return node, offset
}
}
return nil, offset
}
func (s *StatusLineNode) IsLeaf() bool {
return len(s.Children) == 0
}
func (s *StatusLineNode) Size() int {
output := 1
for _, child := range s.Children {
output += child.Size()
}
return output
}
func (s *StatusLineNode) Flatten() []*StatusLineNode {
arr := []*StatusLineNode{s}
for _, child := range s.Children {
arr = append(arr, child.Flatten()...)
}
return arr
}
func (s *StatusLineNode) Sort() {
s.sortChildren()
for _, child := range s.Children {
child.Sort()
}
}
func (s *StatusLineNode) sortChildren() {
if s.IsLeaf() {
return
}
sortedChildren := make([]*StatusLineNode, len(s.Children))
copy(sortedChildren, s.Children)
sort.Slice(sortedChildren, func(i, j int) bool {
if !sortedChildren[i].IsLeaf() && sortedChildren[j].IsLeaf() {
return true
}
if sortedChildren[i].IsLeaf() && !sortedChildren[j].IsLeaf() {
return false
}
return sortedChildren[i].Name < sortedChildren[j].Name
})
// TODO: think about making this in-place
s.Children = sortedChildren
}
func (s *StatusLineNode) GetIsTracked() bool {
if s.File != nil {
return s.File.GetIsTracked()
}
// pretty sure I'm allowed to do this
return true
}
func (s *StatusLineNode) GetPath() string {
return s.Path
}
func (s *StatusLineNode) HasExactlyOneChild() bool {
return len(s.Children) == 1
}
func (s *StatusLineNode) Compress() {
if s == nil {
return
}
s.compressAux()
}
func (s *StatusLineNode) compressAux() *StatusLineNode {
if s.IsLeaf() {
return s
}
for i, child := range s.Children {
if child.HasExactlyOneChild() {
grandchild := child.Children[0]
grandchild.Name = fmt.Sprintf("%s/%s", child.Name, grandchild.Name)
s.Children[i] = grandchild
}
}
for i, child := range s.Children {
s.Children[i] = child.compressAux()
}
return s
}
|