-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
287 lines (253 loc) · 3.99 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"github.com/nsf/termbox-go"
"log"
"time"
)
func flushRetry() (err error) {
for i := 1; i <= 2; i++ {
if err = termbox.Flush(); err == nil {
return nil
} else {
time.Sleep(time.Duration(100_000_000 * i))
}
}
return
}
func setLine(x0, x1, y int, r rune) {
if x1 == 0 {
x1, _ = termbox.Size()
}
for i := x0; i < x1; i++ {
termbox.SetChar(i, y, r)
}
}
// y0 input abcd
// y1 num-kanji
// y2 code
// y3 <hr>
// y4+ buf
var gbi = newBufInfo(4)
func bufTo(r rune) bool {
if r == gbi.curBuf {
return false
}
termbox.SetChar(0, gbi.curY, ' ')
gbi.set(r)
termbox.SetChar(0, gbi.curY, '*')
return true
}
func initUI() error {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
X, Y := termbox.Size()
setLine(0, X, 3, '=')
bufCnt := 0
for j, r := 4, 'A'; j < Y && r < bufBound; j, r = j+1, r+1 {
termbox.SetChar(1, j, r)
bufCnt++
}
gbi.initX(bufCnt)
termbox.SetChar(0, gbi.curY, '*')
return flushRetry()
}
var codeOn = false
func toggleCode() bool {
if codeOn {
codeOn = false
setLine(0, 0, 2, ' ')
} else {
codeOn = true
pgShowCode()
}
return true
}
var gex = newExiter()
func showKj(kj string, x int) int {
for _, r := range kj {
termbox.SetChar(x, 1, r)
x++
}
return x + 2
//return -1
}
var gpg = newPager(eLen)
const num0 rune = '1'
func pgKj() {
xPos := 0
idx := num0
for _, kj := range gpg.getKj() {
termbox.SetChar(xPos, 1, idx)
idx++
xPos++
termbox.SetChar(xPos, 1, kj)
xPos += 2
termbox.SetChar(xPos, 1, ' ')
xPos++
termbox.SetChar(xPos, 1, ' ')
xPos++
}
setLine(xPos, 0, 1, ' ')
if codeOn {
pgShowCode()
}
}
func pgShowCode() {
xPos := 0
i := 0
for _, code := range gpg.getCode() {
termbox.SetChar(xPos, 2, rune(code))
xPos++
if i == 3 {
termbox.SetChar(xPos, 2, ' ')
xPos++
i = 0
} else {
i++
}
}
setLine(xPos, 0, 2, ' ')
}
var gim = newImData(eLen)
func imInput(r rune) bool {
if gim.full() {
return false
}
termbox.SetChar(gim.curP, 0, r)
gim.put(r)
gpg.put(gim.kj())
pgKj()
return true
}
func imBs() bool {
if gim.empty() {
return false
}
gim.bs()
termbox.SetChar(gim.curP, 0, ' ')
gpg.pop()
if gim.empty() {
setLine(0, 0, 1, ' ')
setLine(0, 0, 2, ' ')
} else {
pgKj()
}
return true
}
func imClear() bool {
if gim.empty() {
return false
}
for !gim.empty() {
gim.bs()
}
setLine(0, eLen, 0, ' ')
setLine(0, 0, 1, ' ')
setLine(0, 0, 2, ' ')
gpg.clear()
return true
}
func imSel(idx int) bool {
r := gpg.get1Kj(idx)
if r == 0 {
return false
}
termbox.SetChar(gbi.useX(), gbi.curY, r)
return true
}
func bufBs() bool {
p, b := gbi.bsX()
if b {
termbox.SetChar(p, gbi.curY, ' ')
termbox.SetChar(p+1, gbi.curY, ' ')
return true
} else {
return false
}
}
func bufClear() bool {
if gbi.empty() {
return false
}
setLine(bufX0, 0, gbi.curY, ' ')
gbi.clear()
return true
}
func pgBack() bool {
if !gpg.back() {
return false
}
pgKj()
return true
}
func pgForth() bool {
if !gpg.forth() {
return false
}
pgKj()
return true
}
func handleCh(r rune) bool {
if subjectEx(r) {
gex.handle(r)
return false
}
if subjectBuf(r) {
return bufTo(r)
}
if subjectIm(r) {
return imInput(r)
}
if subjectSel(r) {
return imSel(int(r - num0))
}
switch r {
case '?':
return toggleCode()
case '\'':
return imClear()
case '"':
return bufClear()
case '|':
return bufBs()
case ',':
return pgBack()
case '.':
return pgForth()
}
return false
}
func evlp() {
for {
flush := false
switch e := termbox.PollEvent(); e.Type {
case termbox.EventKey:
if e.Ch != 0 {
flush = handleCh(e.Ch)
} else {
switch e.Key {
case termbox.KeyBackspace, termbox.KeyBackspace2:
flush = imBs()
case termbox.KeySpace:
flush = imSel(0)
}
}
}
if gex.bye {
return
}
if flush {
flushRetry()
}
}
}
func main() {
if err := termbox.Init(); err != nil {
panic(err)
}
defer termbox.Close()
if err := initUI(); err != nil {
log.Printf("initUI err %v\n", err)
return
}
evlp()
}