forked from xtaci/safebox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir.go
140 lines (121 loc) · 3 KB
/
dir.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func showDirWindow(inputField *tview.InputField) {
windowName := "showDirWindow"
rootDir := "/"
node := tview.NewTreeNode(rootDir).SetColor(tcell.ColorRed)
tree := tview.NewTreeView().
SetRoot(node).
SetCurrentNode(node)
tree.SetBorder(true).SetTitle("Directory")
// A helper function which adds the files and directories of the given path
// to the given target node.
add := func(target *tview.TreeNode, path string) {
files, err := ioutil.ReadDir(path)
if err != nil {
panic(err)
}
for _, file := range files {
node := tview.NewTreeNode(file.Name()).
SetReference(filepath.Join(path, file.Name())).
SetSelectable(file.IsDir())
if file.IsDir() {
node.SetColor(tcell.ColorGreen)
}
target.AddChild(node)
}
}
add(node, rootDir)
// path route
var route []string
// recursive to root
dir := inputField.GetText()
// get file info
fi, _ := os.Stat(dir)
for dir != rootDir {
if dir == "." {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
dir = wd
}
dir = filepath.Dir(dir)
route = append(route, dir)
}
// create a path to inputfield
prevNode := node
for i := len(route) - 1; i >= 0; i-- {
children := prevNode.GetChildren()
for _, children := range children {
dir := children.GetReference().(string)
if dir == route[i] {
tree.SetCurrentNode(children)
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
for _, file := range files {
node := tview.NewTreeNode(file.Name()).
SetReference(filepath.Join(route[i], file.Name())).
SetSelectable(true)
if file.IsDir() {
node.SetColor(tcell.ColorGreen)
} else {
// select the given file
if os.SameFile(fi, file) {
tree.SetCurrentNode(node)
}
}
children.AddChild(node)
}
children.Expand()
prevNode = children
break
}
}
}
// Change input text based on tree selection
tree.SetChangedFunc(func(node *tview.TreeNode) {
reference := node.GetReference()
if reference == nil {
return // Selecting the root node does nothing.
}
path := reference.(string)
// set this path to input text
inputField.SetText(path)
})
// If a directory was selected, open it.
tree.SetSelectedFunc(func(node *tview.TreeNode) {
reference := node.GetReference()
if reference == nil {
return // Selecting the root node does nothing.
}
children := node.GetChildren()
path := reference.(string)
// check if the selected path is dir
fi, err := os.Stat(inputField.GetText())
if err != nil {
panic(err)
}
if fi.IsDir() {
if len(children) == 0 {
// Load and show files in this directory.
add(node, path)
} else {
// Collapse if visible, expand if collapsed.
node.SetExpanded(!node.IsExpanded())
}
} else {
// file selected, close window
root.RemovePage(windowName)
}
})
root.AddPage(windowName, popup(80, 30, tree), true, true)
}