-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiletree.go
73 lines (59 loc) · 1.45 KB
/
filetree.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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"os"
"path/filepath"
)
func (a *FileBundlerApp) loadDirectory(path string) error {
root := tview.NewTreeNode(filepath.Base(path))
root.SetReference(path)
root.SetSelectable(true)
root.SetColor(tcell.ColorBlue.TrueColor())
// Populate the root node immediately
err := a.populateNode(root)
if err != nil {
return err
}
a.ui.fileTree.SetRoot(root)
a.ui.fileTree.SetCurrentNode(root)
a.currentPath = path
return nil
}
func (a *FileBundlerApp) populateNode(node *tview.TreeNode) error {
path := node.GetReference().(string)
entries, err := os.ReadDir(path)
if err != nil {
return err
}
for _, entry := range entries {
if shouldSkip(entry.Name(), a.config) {
continue
}
childPath := filepath.Join(path, entry.Name())
child := tview.NewTreeNode(entry.Name())
child.SetReference(childPath)
child.SetSelectable(true)
if entry.IsDir() {
child.SetColor(tcell.ColorBlue.TrueColor())
child.SetText("📁 " + entry.Name())
} else {
child.SetText("📄 " + entry.Name())
child.SetColor(tcell.ColorWhite.TrueColor())
}
node.AddChild(child)
}
return nil
}
// Move shouldSkip to be a standalone function
func shouldSkip(name string, config *Config) bool {
if !config.IncludeHidden && len(name) > 0 && name[0] == '.' {
return true
}
for _, pattern := range config.ExcludePatterns {
if pattern == name {
return true
}
}
return false
}