-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
169 lines (146 loc) · 3.14 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
package main
import (
"flag"
"log"
"sync"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
"github.com/shota3506/gtree/commands"
"github.com/shota3506/gtree/entry"
"github.com/shota3506/gtree/state"
)
var showHidden bool
func init() {
const (
showHiddenUsage = "show hidden files or directories"
)
flag.BoolVar(&showHidden, "hidden", false, showHiddenUsage)
flag.BoolVar(&showHidden, "h", false, showHiddenUsage+" (shorthand)")
}
func main() {
flag.Parse()
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
path := "."
if args := flag.Args(); len(args) == 1 {
path = args[0]
}
root, err := entry.NewRoot(path, showHidden)
if err != nil {
return err
}
s, err := initScreen()
if err != nil {
return err
}
defer s.Fini()
commandChan := make(chan commands.Command)
stateChan := make(chan state.State)
var wg sync.WaitGroup
wg.Add(3)
go func() {
handleCommand(s, commandChan)
wg.Done()
}()
go func() {
handleChange(s, stateChan)
wg.Done()
}()
go func() {
start(s, root, commandChan, stateChan)
wg.Done()
}()
wg.Wait()
return nil
}
func initScreen() (tcell.Screen, error) {
s, err := tcell.NewScreen()
if err != nil {
return nil, err
}
if err = s.Init(); err != nil {
return nil, err
}
defaultStyle := tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
s.SetStyle(defaultStyle)
s.DisableMouse()
s.DisablePaste()
s.Clear()
return s, nil
}
func handleCommand(s tcell.Screen, commandChan chan commands.Command) {
for {
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape, tcell.KeyCtrlC:
close(commandChan)
return
case tcell.KeyUp:
commandChan <- commands.CommandUp{}
case tcell.KeyDown:
commandChan <- commands.CommandDown{}
case tcell.KeyEnter:
commandChan <- commands.CommandSelect{}
case tcell.KeyRune:
switch ev.Rune() {
case 'q':
close(commandChan)
return
case 'k':
commandChan <- commands.CommandUp{}
case 'j':
commandChan <- commands.CommandDown{}
case ' ':
commandChan <- commands.CommandSelect{}
}
}
case *tcell.EventResize:
width, height := s.Size()
commandChan <- commands.CommandResize{Width: width, Height: height - 1}
}
}
}
func handleChange(s tcell.Screen, stateChan chan state.State) {
for {
st, ok := <-stateChan
if !ok {
return
}
render(s, st)
}
}
func start(s tcell.Screen, root *entry.Dir, commandChan chan commands.Command, stateChan chan state.State) {
width, height := s.Size()
var st state.State = state.NewState(root, width, height-1)
stateChan <- st
for {
command, ok := <-commandChan
if !ok {
close(stateChan)
return
}
if next, err := command.Do(st); err == nil {
st = next
stateChan <- st
}
}
}
func render(s tcell.Screen, st state.State) {
s.Clear()
layout := views.NewBoxLayout(views.Vertical)
v := views.NewCellView()
v.SetModel(st.View())
statusBar := views.NewSimpleStyledTextBar()
statusBar.SetLeft(st.Root().Path())
layout.SetView(s)
layout.AddWidget(v, 1.0)
layout.AddWidget(statusBar, 0.0)
layout.Draw()
s.Show()
}