-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (170 loc) · 3.63 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
package main
import (
"log"
"os"
"strings"
"github.com/homedm/collect-editor/pkg/buffer"
termbox "github.com/nsf/termbox-go"
)
var (
mode Mode
editBufs *buffer.Buffer
cmdLineBuf *buffer.Buffer
cmdLineWin *CmdLineWin
editWins *EditWin
)
func main() {
if err := startUp(); err != nil {
log.Fatal(err)
}
defer termbox.Close()
screenPaint()
// poll for keyboard events in another goroutine
events := make(chan termbox.Event, 1000)
go func() {
for {
events <- termbox.PollEvent()
}
}()
mainloop:
for {
select {
case ev := <-events:
switch mode {
case Move:
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyEsc:
mode = Move
case termbox.KeyArrowUp:
editBufs.MovePos(buffer.Up)
case termbox.KeyArrowDown:
editBufs.MovePos(buffer.Down)
case termbox.KeyArrowLeft:
editBufs.MovePos(buffer.Left)
case termbox.KeyArrowRight:
editBufs.MovePos(buffer.Right)
case termbox.KeyCtrlS:
editBufs.WriteBufToFile()
case termbox.KeyCtrlC:
break mainloop // 実行終了
default:
}
switch ev.Ch {
case ':':
mode = Cmd
cmdLineWin.Focus()
case 'k':
editBufs.MovePos(buffer.Up)
case 'j':
editBufs.MovePos(buffer.Down)
case 'h':
editBufs.MovePos(buffer.Left)
case 'l':
editBufs.MovePos(buffer.Right)
case 'i':
mode = Edit
case 'u':
editBufs.Undo()
case 'r':
editBufs.Redo()
default:
}
}
case Edit:
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyEsc:
mode = Move
case termbox.KeyEnter:
editBufs.LineFeed()
// mac delete-key is this
case termbox.KeyCtrlH:
fallthrough
case termbox.KeyBackspace2:
editBufs.BackSpace()
case termbox.KeyCtrlZ:
editBufs.Undo()
case termbox.KeyCtrlR:
editBufs.Redo()
default:
editBufs.InsertChr(ev.Ch)
}
}
case Visual:
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyEsc:
mode = Move
}
}
case Cmd:
if ev.Type == termbox.EventKey {
switch ev.Key {
case termbox.KeyEsc:
mode = Move
case termbox.KeyEnter:
// 入力されたコマンドの解析と実行を開始する
// quit
usrCmd, err := cmdLineBuf.GetLine(0)
if err != nil {
log.Fatal(err)
}
if strings.Compare(string(usrCmd[1:]), "q") == 0 || strings.Compare(string(usrCmd), "quit") == 0 {
break mainloop
}
default:
cmdLineBuf.InsertChr(ev.Ch)
}
}
default:
}
// when entered any key, redraw buffer
screenPaint()
default:
// Nothing
}
}
}
func startUp() error {
// Initialize terminal window
err := termbox.Init()
if err != nil {
return err
}
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
termbox.SetCursor(0, 0)
if len(os.Args) > 1 {
fname := os.Args[1]
editBufs, err = buffer.NewBuffer(fname)
if err != nil {
log.Fatal(err)
}
} else {
editBufs = buffer.NewEmptyBuffer()
}
cmdLineBuf = buffer.NewEmptyBuffer()
// get window size
w, h := termbox.Size()
// Set command line window default value
cmdLineWin = NewCmdLineWin(w, 2, cmdLineBuf)
// Set editWins default value
editWins = NewEditWin(0, 0, w, h-cmdLineWin.Size.Height, editBufs)
mode = Move
return nil
}
func drawCursor() {
if mode.equal(Cmd) {
cmdLineWin.UpdateCursor()
} else {
editWins.UpdateCursor()
}
}
func screenPaint() {
cmdLineWin.Redraw()
editWins.Draw()
// 現在のモードに合わせて、カーソルを描く
drawCursor()
// update all window
termbox.Flush()
}