This repository has been archived by the owner on Apr 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
navigate.go
112 lines (84 loc) · 1.83 KB
/
navigate.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
package gotree
import (
"io/ioutil"
"strings"
)
type File struct {
isDir bool
name string
}
type Navigator struct {
display *Displayer
currentPath string
rootPath string
files []File
currentLine int
}
func NewNavigator(display *Displayer, path string) *Navigator {
files := make([]File, 0)
return &Navigator{display, path, path, files, 0}
}
func (n *Navigator) InitDir(path string) {
n.display.Start()
n.display.Breadcrumb(path)
c := make(chan File)
go n.fetchFiles(path, c)
first := true
i := 0
for f := range c {
n.display.Line(i+1, f, first)
first = false
i += 1
}
n.display.Stop()
n.currentPath = path
n.currentLine = 0
}
func (n *Navigator) ChangeSelect(position string) {
if "down" == position && n.currentLine == len(n.files)-1 {
return
}
if "up" == position && n.currentLine == 0 {
return
}
n.display.Line(n.currentLine+1, n.files[n.currentLine], false)
if "up" == position {
n.currentLine -= 1
} else {
n.currentLine += 1
}
n.display.Line(n.currentLine+1, n.files[n.currentLine], true)
n.display.Stop()
}
func (n *Navigator) EnterDir() {
if !n.files[n.currentLine].isDir {
return
}
s := []string{n.currentPath, n.files[n.currentLine].name}
path := strings.Join(s, "/")
n.InitDir(path)
}
func (n *Navigator) LeaveDir() {
if n.currentPath == n.rootPath {
return
}
splitPath := strings.Split(n.currentPath, "/")
max := len(splitPath) - 1
path := strings.Join(splitPath[0:max], "/")
n.InitDir(path)
}
func (n *Navigator) fetchFiles(path string, fs chan File) {
n.files = make([]File, 0)
var file File
dirFiles, _ := ioutil.ReadDir(path)
for _, f := range dirFiles {
if f.IsDir() && strings.HasPrefix(f.Name(), ".") {
continue
}
file = File{f.IsDir(), f.Name()}
fs <- file
n.files = append(n.files, file)
// do something more complex
}
close(fs)
}