-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (189 loc) · 3.62 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
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
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"syscall"
"github.com/eiannone/keyboard"
"golang.org/x/term"
)
type DirItem struct {
Name string
IsDir bool
}
type Flags struct {
ShowHiddenFiles bool
}
func showCursor() {
fmt.Fprint(os.Stderr, "\x1b[?25h")
}
func hideCursor() {
fmt.Fprint(os.Stderr, "\x1b[?25l")
}
func clearTerm() {
fmt.Fprint(os.Stderr, "\x1b[1;0H\x1b[0J\x1b[1;0H")
}
func initTerm() {
fmt.Fprint(os.Stderr, "\x1b[2J")
}
func renderItems(dir string, items []DirItem, height int, scr int) {
clearTerm()
fmt.Fprintf(os.Stderr, "\x1b[32m\x1b[1m%s\x1b[0m\n", dir)
i := 0 - scr
for _, item := range items {
if i < 0 {
i++
continue
} else if i > height-3 {
break
}
if item.IsDir {
fmt.Fprintf(os.Stderr, " \x1b[34m\x1b[1m%s\x1b[0m\n", item.Name)
} else {
fmt.Fprintf(os.Stderr, " \x1b[37m%s\x1b[0m\n", item.Name)
}
i++
}
}
func renderCursor(height int, cur int) {
if cur > 0 {
fmt.Fprintf(os.Stderr, "\x1b[%d;1H ", cur+1)
}
fmt.Fprintf(os.Stderr, "\x1b[%d;1H>", cur+2)
if cur+3 <= height {
fmt.Fprintf(os.Stderr, "\x1b[%d;1H ", cur+3)
}
}
func loadItems(path string, flags Flags) ([]DirItem, int, int) {
cur := 0
files, _ := os.ReadDir(path)
sort.Slice(files, func(i, j int) bool {
if files[i].IsDir() {
if files[j].IsDir() {
return files[i].Name() < files[j].Name()
} else {
return true
}
} else {
if files[j].IsDir() {
return false
} else {
return files[i].Name() < files[j].Name()
}
}
})
items := []DirItem{}
if path != "/" {
items = append(items, DirItem{Name: "..", IsDir: true})
cur = 1
}
for _, f := range files {
if !flags.ShowHiddenFiles && f.Name()[0] == '.' {
continue
}
items = append(items, DirItem{Name: f.Name(), IsDir: f.IsDir()})
}
if len(items) == 1 {
cur = 0
}
return items, cur, 0
}
func main() {
// Initialization
flags := Flags{
ShowHiddenFiles: false,
}
_, height, err := term.GetSize(syscall.Stdin)
if err != nil {
panic(err)
}
if err := keyboard.Open(); err != nil {
panic(err)
}
defer func() {
_ = keyboard.Close()
}()
// Set current directory
dir, err := os.Getwd()
if err != nil {
panic(err)
}
dirReturn := dir
// Load files in current directory
items, cur, scr := loadItems(dir, flags)
// Render
hideCursor()
initTerm()
renderItems(dir, items, height, scr)
renderCursor(height, cur)
for {
char, key, err := keyboard.GetKey()
if err != nil {
panic(err)
}
if char == 'q' {
dir = dirReturn
break
}
if key == keyboard.KeyEnter {
break
}
switch key {
case keyboard.KeyArrowDown:
{
cur++
if cur >= height-3 {
cur--
if (cur + scr + 1) < len(items) {
scr++
renderItems(dir, items, height, scr)
}
} else if cur >= len(items) {
cur--
break
}
renderCursor(height, cur)
}
case keyboard.KeyArrowUp:
{
cur--
if cur < 0 {
cur = 0
if scr > 0 {
scr--
renderItems(dir, items, height, scr)
} else {
break
}
}
renderCursor(height, cur)
}
case keyboard.KeyArrowLeft:
{
if dir == "/" {
fmt.Fprint(os.Stderr, "\a")
break
}
dir = filepath.Join(dir, "..")
items, cur, scr = loadItems(dir, flags)
renderItems(dir, items, height, scr)
renderCursor(height, cur)
}
case keyboard.KeyArrowRight:
{
if !items[cur+scr].IsDir {
fmt.Fprint(os.Stderr, "\a")
break
}
dir = filepath.Join(dir, items[cur+scr].Name)
items, cur, scr = loadItems(dir, flags)
renderItems(dir, items, height, scr)
renderCursor(height, cur)
}
}
}
clearTerm()
showCursor()
fmt.Print(dir)
}