-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (58 loc) · 1.52 KB
/
main.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
package main
import (
"io/ioutil"
"path/filepath"
"github.com/gdamore/tcell/v2"
"github.com/malivvan/mate/view"
)
// Show a navigable tree view of the current directory.
func main() {
app := view.NewApplication()
defer app.HandlePanic()
app.EnableMouse(true)
rootDir := "."
root := view.NewTreeNode(rootDir)
root.SetColor(tcell.ColorRed.TrueColor())
tree := view.NewTreeView()
tree.SetRoot(root)
tree.SetCurrentNode(root)
// A helper function which adds the files and directories of the given path
// to the given target node.
add := func(target *view.TreeNode, path string) {
files, err := ioutil.ReadDir(path)
if err != nil {
panic(err)
}
for _, file := range files {
node := view.NewTreeNode(file.Name())
node.SetReference(filepath.Join(path, file.Name()))
node.SetSelectable(file.IsDir())
if file.IsDir() {
node.SetColor(tcell.ColorGreen.TrueColor())
}
target.AddChild(node)
}
}
// Add the current directory to the root node.
add(root, rootDir)
// If a directory was selected, open it.
tree.SetSelectedFunc(func(node *view.TreeNode) {
reference := node.GetReference()
if reference == nil {
return // Selecting the root node does nothing.
}
children := node.GetChildren()
if len(children) == 0 {
// Load and show files in this directory.
path := reference.(string)
add(node, path)
} else {
// Collapse if visible, expand if collapsed.
node.SetExpanded(!node.IsExpanded())
}
})
app.SetRoot(tree, true)
if err := app.Run(); err != nil {
panic(err)
}
}