-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath-node.go
299 lines (262 loc) · 7.33 KB
/
path-node.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package core
import (
"path"
"regexp"
"sort"
"strings"
"github.com/orochaa/go-clack/core/internals"
)
type PathNode struct {
Index int
Depth int
Path string
Name string
Parent *PathNode
IsDir bool
IsOpen bool
Children []*PathNode
IsSelected bool
FileSystem FileSystem
OnlyShowDir bool
}
type PathNodeOptions struct {
OnlyShowDir bool
FileSystem FileSystem
}
// NewPathNode initializes a new PathNode with the provided root path and options.
// It sets up the root node, configures the file system, and opens the node to populate its children.
//
// Parameters:
// - rootPath (string): The root path for the node.
// - options (PathNodeOptions): Configuration options for the node.
// - OnlyShowDir (bool): Whether to only show directories (default: false).
// - FileSystem (FileSystem): The file system implementation to use (default: OSFileSystem).
//
// Returns:
// - *PathNode: A new instance of PathNode.
func NewPathNode(rootPath string, options PathNodeOptions) *PathNode {
if options.FileSystem == nil {
options.FileSystem = internals.OSFileSystem{}
}
root := &PathNode{
Path: rootPath,
Name: rootPath,
IsDir: true,
OnlyShowDir: options.OnlyShowDir,
FileSystem: options.FileSystem,
}
root.Open()
return root
}
// Open opens the current PathNode, reads its directory entries, and populates its children.
// If the node is not a directory or is already open, this function does nothing.
func (p *PathNode) Open() {
if !p.IsDir || p.IsOpen {
return
}
entries, err := p.FileSystem.ReadDir(p.Path)
if err != nil {
return
}
for _, entry := range entries {
if p.OnlyShowDir && !entry.IsDir() {
continue
}
p.Children = append(p.Children, &PathNode{
Depth: p.Depth + 1,
Path: path.Join(p.Path, entry.Name()),
Name: entry.Name(),
Parent: p,
IsDir: entry.IsDir(),
FileSystem: p.FileSystem,
OnlyShowDir: p.OnlyShowDir,
})
}
sort.SliceStable(p.Children, func(i, j int) bool {
if p.Children[i].IsDir != p.Children[j].IsDir {
return p.Children[i].IsDir
}
return strings.ToLower(p.Children[i].Name) < strings.ToLower(p.Children[j].Name)
})
for i, child := range p.Children {
child.Index = i
}
p.IsOpen = true
}
// Close closes the current PathNode by clearing its children and marking it as closed.
func (p *PathNode) Close() {
p.Children = []*PathNode(nil)
p.IsOpen = false
}
// TraverseNodes traverses the node and its children, applying the provided visit function to each node.
//
// Parameters:
// - visit (func(node *PathNode)): A function to apply to each node during traversal.
func (p *PathNode) TraverseNodes(visit func(node *PathNode)) {
var traverse func(node *PathNode)
traverse = func(node *PathNode) {
visit(node)
if !node.IsDir {
return
}
for _, child := range node.Children {
traverse(child)
}
}
traverse(p)
}
// Flat returns a flattened list of all nodes in the tree, starting from the current node.
//
// Returns:
// - []*PathNode: A slice of all nodes in the tree.
func (p *PathNode) Flat() []*PathNode {
var options []*PathNode
p.TraverseNodes(func(node *PathNode) {
options = append(options, node)
})
return options
}
// FilteredFlat returns a filtered and flattened list of nodes based on the provided search term.
// If the search term is empty or invalid, it returns the full flattened list.
//
// Parameters:
// - search (string): The search term to filter nodes by.
// - currentNode (*PathNode): The current node to filter relative to.
//
// Returns:
// - []*PathNode: A slice of filtered nodes.
func (p *PathNode) FilteredFlat(search string, currentNode *PathNode) []*PathNode {
searchRegex, err := regexp.Compile("(?i)" + search)
if err != nil || search == "" {
return p.Flat()
}
var options []*PathNode
p.TraverseNodes(func(node *PathNode) {
if node.Depth == currentNode.Depth && node.Depth > 0 {
if matched := searchRegex.MatchString(node.Name); matched {
options = append(options, node)
}
} else {
options = append(options, node)
}
})
return options
}
// Layer returns the children of the current node's parent, representing the current layer in the tree.
//
// Returns:
// - []*PathNode: A slice of nodes in the current layer.
func (p *PathNode) Layer() []*PathNode {
if p.IsRoot() {
return []*PathNode{p}
}
return p.Parent.Children
}
// FilteredLayer returns a filtered list of nodes in the current layer based on the provided search term.
// If the search term is empty or invalid, it returns the full layer.
//
// Parameters:
// - search (string): The search term to filter nodes by.
//
// Returns:
// - []*PathNode: A slice of filtered nodes in the current layer.
func (p *PathNode) FilteredLayer(search string) []*PathNode {
searchRegex, err := regexp.Compile("(?i)" + search)
if err != nil || search == "" {
return p.Layer()
}
var layer []*PathNode
for _, node := range p.Layer() {
if matched := searchRegex.MatchString(node.Name); matched {
layer = append(layer, node)
}
}
return layer
}
// FirstChild returns the first child of the current node.
// If the node has no children, it returns nil.
//
// Returns:
// - *PathNode: The first child node.
func (p *PathNode) FirstChild() *PathNode {
if len(p.Children) == 0 {
return nil
}
return p.Children[0]
}
// LastChild returns the last child of the current node.
// If the node has no children, it returns nil.
//
// Returns:
// - *PathNode: The last child node.
func (p *PathNode) LastChild() *PathNode {
if len(p.Children) == 0 {
return nil
}
return p.Children[len(p.Children)-1]
}
// PrevChild returns the previous child relative to the provided index.
// If the index is out of bounds, it wraps around to the last child.
//
// Parameters:
// - index (int): The index of the current child.
//
// Returns:
// - *PathNode: The previous child node.
func (p *PathNode) PrevChild(index int) *PathNode {
if index <= 0 {
return p.LastChild()
}
return p.Children[index-1]
}
// NextChild returns the next child relative to the provided index.
// If the index is out of bounds, it wraps around to the first child.
//
// Parameters:
// - index (int): The index of the current child.
//
// Returns:
// - *PathNode: The next child node
func (p *PathNode) NextChild(index int) *PathNode {
if index+1 >= len(p.Children) {
return p.FirstChild()
}
return p.Children[index+1]
}
// IsRoot checks if the current node is the root node.
// It returns true if the node has no parent, false otherwise.
//
// Returns:
// - bool: True if the node is the root, false otherwise.
func (p *PathNode) IsRoot() bool {
return p.Parent == nil
}
// IsEqual checks if the current node is equal to another node based on their paths.
//
// Parameters:
// - node (*PathNode): The node to compare with.
//
// Returns:
// - bool: True if the nodes are equal, false otherwise.
func (p *PathNode) IsEqual(node *PathNode) bool {
return node.Path == p.Path
}
// IndexOf returns the index of a given node in the provided options slice.
// If the node is not found, it returns -1.
//
// Parameters:
// - node (*PathNode): The node to find the index of.
// - options ([]*PathNode): The slice of nodes to search in.
//
// Returns:
// - int: The index of the node, or -1 if not found.
func (p *PathNode) IndexOf(node *PathNode, options []*PathNode) int {
if node != nil {
for i, option := range options {
if option.IsEqual(node) {
return i
}
}
}
return -1
}