...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package termbox
18
19 import (
20 "errors"
21
22 "github.com/gdamore/tcell/v2"
23 )
24
25 var screen tcell.Screen
26 var outMode OutputMode
27
28
29 func Init() error {
30 outMode = OutputNormal
31 if s, e := tcell.NewScreen(); e != nil {
32 return e
33 } else if e = s.Init(); e != nil {
34 return e
35 } else {
36 screen = s
37 return nil
38 }
39 }
40
41
42 func Close() {
43 screen.Fini()
44 }
45
46
47 func Flush() error {
48 screen.Show()
49 return nil
50 }
51
52
53 func SetCursor(x, y int) {
54 screen.ShowCursor(x, y)
55 }
56
57
58 func HideCursor() {
59 SetCursor(-1, -1)
60 }
61
62
63 func Size() (int, int) {
64 return screen.Size()
65 }
66
67
68
69 type Attribute uint16
70
71
72 const (
73 ColorDefault Attribute = iota
74 ColorBlack
75 ColorRed
76 ColorGreen
77 ColorYellow
78 ColorBlue
79 ColorMagenta
80 ColorCyan
81 ColorWhite
82 )
83
84
85 const (
86 AttrBold Attribute = 1 << (9 + iota)
87 AttrUnderline
88 AttrReverse
89 )
90
91 func fixColor(c tcell.Color) tcell.Color {
92 if c == tcell.ColorDefault {
93 return c
94 }
95 switch outMode {
96 case OutputNormal:
97 c = tcell.PaletteColor(int(c) & 0xf)
98 case Output256:
99 c = tcell.PaletteColor(int(c) & 0xff)
100 case Output216:
101 c = tcell.PaletteColor(int(c)%216 + 16)
102 case OutputGrayscale:
103 c %= tcell.PaletteColor(int(c)%24 + 232)
104 default:
105 c = tcell.ColorDefault
106 }
107 return c
108 }
109
110 func mkStyle(fg, bg Attribute) tcell.Style {
111 st := tcell.StyleDefault
112
113 f := tcell.PaletteColor(int(fg)&0x1ff - 1)
114 b := tcell.PaletteColor(int(bg)&0x1ff - 1)
115
116 f = fixColor(f)
117 b = fixColor(b)
118 st = st.Foreground(f).Background(b)
119 if (fg|bg)&AttrBold != 0 {
120 st = st.Bold(true)
121 }
122 if (fg|bg)&AttrUnderline != 0 {
123 st = st.Underline(true)
124 }
125 if (fg|bg)&AttrReverse != 0 {
126 st = st.Reverse(true)
127 }
128 return st
129 }
130
131
132 func Clear(fg, bg Attribute) {
133 st := mkStyle(fg, bg)
134 w, h := screen.Size()
135 for row := 0; row < h; row++ {
136 for col := 0; col < w; col++ {
137 screen.SetContent(col, row, ' ', nil, st)
138 }
139 }
140 }
141
142
143 type InputMode int
144
145
146 const (
147 InputCurrent InputMode = iota
148 InputEsc
149 InputAlt
150 InputMouse
151 )
152
153
154 func SetInputMode(mode InputMode) InputMode {
155
156 return InputEsc
157 }
158
159
160
161 type OutputMode int
162
163
164 const (
165 OutputCurrent OutputMode = iota
166 OutputNormal
167 Output256
168 Output216
169 OutputGrayscale
170 )
171
172
173 func SetOutputMode(mode OutputMode) OutputMode {
174 if screen.Colors() < 256 {
175 mode = OutputNormal
176 }
177 switch mode {
178 case OutputCurrent:
179 return outMode
180 case OutputNormal, Output256, Output216, OutputGrayscale:
181 outMode = mode
182 return mode
183 default:
184 return outMode
185 }
186 }
187
188
189 func Sync() error {
190 screen.Sync()
191 return nil
192 }
193
194
195
196 func SetCell(x, y int, ch rune, fg, bg Attribute) {
197 st := mkStyle(fg, bg)
198 screen.SetContent(x, y, ch, nil, st)
199 }
200
201
202 type EventType uint8
203
204
205 type Modifier tcell.ModMask
206
207
208 type Key tcell.Key
209
210
211 type Event struct {
212 Type EventType
213 Mod Modifier
214 Key Key
215 Ch rune
216 Width int
217 Height int
218 Err error
219 MouseX int
220 MouseY int
221 N int
222 }
223
224
225 const (
226 EventNone EventType = iota
227 EventKey
228 EventResize
229 EventMouse
230 EventInterrupt
231 EventError
232 EventRaw
233 )
234
235
236 const (
237 KeyF1 = Key(tcell.KeyF1)
238 KeyF2 = Key(tcell.KeyF2)
239 KeyF3 = Key(tcell.KeyF3)
240 KeyF4 = Key(tcell.KeyF4)
241 KeyF5 = Key(tcell.KeyF5)
242 KeyF6 = Key(tcell.KeyF6)
243 KeyF7 = Key(tcell.KeyF7)
244 KeyF8 = Key(tcell.KeyF8)
245 KeyF9 = Key(tcell.KeyF9)
246 KeyF10 = Key(tcell.KeyF10)
247 KeyF11 = Key(tcell.KeyF11)
248 KeyF12 = Key(tcell.KeyF12)
249 KeyInsert = Key(tcell.KeyInsert)
250 KeyDelete = Key(tcell.KeyDelete)
251 KeyHome = Key(tcell.KeyHome)
252 KeyEnd = Key(tcell.KeyEnd)
253 KeyArrowUp = Key(tcell.KeyUp)
254 KeyArrowDown = Key(tcell.KeyDown)
255 KeyArrowRight = Key(tcell.KeyRight)
256 KeyArrowLeft = Key(tcell.KeyLeft)
257 KeyCtrlA = Key(tcell.KeyCtrlA)
258 KeyCtrlB = Key(tcell.KeyCtrlB)
259 KeyCtrlC = Key(tcell.KeyCtrlC)
260 KeyCtrlD = Key(tcell.KeyCtrlD)
261 KeyCtrlE = Key(tcell.KeyCtrlE)
262 KeyCtrlF = Key(tcell.KeyCtrlF)
263 KeyCtrlG = Key(tcell.KeyCtrlG)
264 KeyCtrlH = Key(tcell.KeyCtrlH)
265 KeyCtrlI = Key(tcell.KeyCtrlI)
266 KeyCtrlJ = Key(tcell.KeyCtrlJ)
267 KeyCtrlK = Key(tcell.KeyCtrlK)
268 KeyCtrlL = Key(tcell.KeyCtrlL)
269 KeyCtrlM = Key(tcell.KeyCtrlM)
270 KeyCtrlN = Key(tcell.KeyCtrlN)
271 KeyCtrlO = Key(tcell.KeyCtrlO)
272 KeyCtrlP = Key(tcell.KeyCtrlP)
273 KeyCtrlQ = Key(tcell.KeyCtrlQ)
274 KeyCtrlR = Key(tcell.KeyCtrlR)
275 KeyCtrlS = Key(tcell.KeyCtrlS)
276 KeyCtrlT = Key(tcell.KeyCtrlT)
277 KeyCtrlU = Key(tcell.KeyCtrlU)
278 KeyCtrlV = Key(tcell.KeyCtrlV)
279 KeyCtrlW = Key(tcell.KeyCtrlW)
280 KeyCtrlX = Key(tcell.KeyCtrlX)
281 KeyCtrlY = Key(tcell.KeyCtrlY)
282 KeyCtrlZ = Key(tcell.KeyCtrlZ)
283 KeyCtrlUnderscore = Key(tcell.KeyCtrlUnderscore)
284 KeyBackspace = Key(tcell.KeyBackspace)
285 KeyBackspace2 = Key(tcell.KeyBackspace2)
286 KeyTab = Key(tcell.KeyTab)
287 KeyEnter = Key(tcell.KeyEnter)
288 KeyEsc = Key(tcell.KeyEscape)
289 KeyPgdn = Key(tcell.KeyPgDn)
290 KeyPgup = Key(tcell.KeyPgUp)
291 KeySpace = Key(tcell.Key(' '))
292 KeyTilde = Key(tcell.Key('~'))
293 KeyCtrlSpace = Key(tcell.KeyCtrlSpace)
294
295
296
297
298
299 MouseLeft = Key(tcell.KeyF63)
300 MouseRight = Key(tcell.KeyF62)
301 MouseMiddle = Key(tcell.KeyF61)
302 MouseRelease = Key(tcell.KeyF60)
303 MouseWheelUp = Key(tcell.KeyF59)
304 MouseWheelDown = Key(tcell.KeyF58)
305 KeyCtrlTilde = Key(tcell.KeyCtrlSpace)
306 KeyCtrl2 = Key(tcell.KeyNUL)
307 KeyCtrl3 = Key(tcell.KeyEscape)
308 KeyCtrl4 = Key(tcell.KeyCtrlBackslash)
309 KeyCtrl5 = Key(tcell.KeyCtrlRightSq)
310 KeyCtrl6 = Key(tcell.KeyCtrlCarat)
311 KeyCtrl7 = Key(tcell.KeyCtrlUnderscore)
312 KeyCtrl8 = Key(tcell.KeyDEL)
313 KeyCtrlSlash = Key(tcell.KeyCtrlUnderscore)
314 KeyCtrlRsqBracket = Key(tcell.KeyCtrlRightSq)
315 KeyCtrlBackslash = Key(tcell.KeyCtrlBackslash)
316 KeyCtrlLsqBracket = Key(tcell.KeyCtrlLeftSq)
317 )
318
319
320 const (
321 ModAlt = Modifier(tcell.ModAlt)
322 )
323
324 func makeEvent(tev tcell.Event) Event {
325 switch tev := tev.(type) {
326 case *tcell.EventInterrupt:
327 return Event{Type: EventInterrupt}
328 case *tcell.EventResize:
329 w, h := tev.Size()
330 return Event{Type: EventResize, Width: w, Height: h}
331 case *tcell.EventKey:
332 k := tev.Key()
333 ch := rune(0)
334 if k == tcell.KeyRune {
335 ch = tev.Rune()
336 if ch == ' ' {
337 k = tcell.Key(' ')
338 } else {
339 k = tcell.Key(0)
340 }
341 }
342 mod := tev.Modifiers()
343 return Event{
344 Type: EventKey,
345 Key: Key(k),
346 Ch: ch,
347 Mod: Modifier(mod),
348 }
349 default:
350 return Event{Type: EventNone}
351 }
352 }
353
354
355 func ParseEvent(data []byte) Event {
356
357 return Event{Type: EventError, Err: errors.New("no raw events")}
358 }
359
360
361 func PollRawEvent(data []byte) Event {
362
363 return Event{Type: EventError, Err: errors.New("no raw events")}
364 }
365
366
367 func PollEvent() Event {
368 ev := screen.PollEvent()
369 return makeEvent(ev)
370 }
371
372
373 func Interrupt() {
374 screen.PostEvent(tcell.NewEventInterrupt(nil))
375 }
376
377
378 type Cell struct {
379 Ch rune
380 Fg Attribute
381 Bg Attribute
382 }
383
View as plain text