/
terminal.go
68 lines (57 loc) · 1.34 KB
/
terminal.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
package view
import (
//"fmt"
termbox "github.com/nsf/termbox-go"
)
//Terminal is a simple wrapper for the Terminal display purpose
type Terminal interface {
Init()
Interrupt()
Close()
Flush()
SetCell(x, y int, c rune, fg, bg uint16)
WaitEvent() bool
SetInputMode()
Clear()
}
//Terminal implementation
type termboxImpl struct {
ver string
proxy func() termbox.Event
}
func (tb *termboxImpl) Init() {
termbox.Init()
//this should be the default , but you can change the proxy when you need it
tb.proxy = termbox.PollEvent
}
func (tb termboxImpl) Clear() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
}
func (tb termboxImpl) Close() {
termbox.Close()
}
func (tb termboxImpl) Flush() {
termbox.Flush()
}
func (tb termboxImpl) Interrupt() {
termbox.Interrupt()
}
func (tb termboxImpl) SetInputMode() {
termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
}
func (tb termboxImpl) SetCell(x, y int, c rune, fg, bg uint16) {
termbox.SetCell(x, y, c, termbox.Attribute(fg), termbox.Attribute(bg))
}
func (tb termboxImpl) WaitEvent() bool {
switch ev := tb.proxy(); ev.Type { // PollEvent will be blocked
case termbox.EventKey:
if ev.Key == termbox.KeyEsc {
return true
}
case termbox.EventResize:
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
case termbox.EventError:
panic(ev.Err)
}
return false
}