/
mock.go
92 lines (71 loc) 路 1.43 KB
/
mock.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
package terminal
import (
"io"
"sync"
)
type MockTerminal struct {
m sync.Mutex
width, height int
raw bool
info Info
keys chan string
dest io.Writer
}
func NewMockTerminal(dest io.Writer, width, height int, raw bool) *MockTerminal {
return &MockTerminal{
width: width,
height: height,
raw: raw,
info: info{noTermInfo(0)},
keys: make(chan string),
dest: dest,
}
}
func (t *MockTerminal) IsRaw() bool {
return t.raw
}
func (t *MockTerminal) Close() error {
close(t.keys)
return nil
}
func (t *MockTerminal) Size() (width, height int, err error) {
t.m.Lock()
defer t.m.Unlock()
return t.width, t.height, nil
}
func (t *MockTerminal) Write(b []byte) (int, error) {
return t.dest.Write(b)
}
func (t *MockTerminal) ClearLine() {
t.info.ClearLine(t)
}
func (t *MockTerminal) ClearEnd() {
t.info.ClearEnd(t)
}
func (t *MockTerminal) CursorUp(count int) {
t.info.CursorUp(t, count)
}
func (t *MockTerminal) CursorDown(count int) {
t.info.CursorDown(t, count)
}
func (t *MockTerminal) HideCursor() {
t.info.HideCursor(t)
}
func (t *MockTerminal) ShowCursor() {
t.info.ShowCursor(t)
}
func (t *MockTerminal) ReadKey() (string, error) {
k, ok := <-t.keys
if !ok {
return "", io.EOF
}
return k, nil
}
func (t *MockTerminal) SetSize(width, height int) {
t.m.Lock()
defer t.m.Unlock()
t.width, t.height = width, height
}
func (t *MockTerminal) SendKey(key string) {
t.keys <- key
}