-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
173 lines (141 loc) · 3.07 KB
/
input.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
package minigo
import "unicode/utf8"
type Input struct {
Value []byte
m *Minitel
refCol int
refRow int
width int
height int
dots bool
}
func NewInput(m *Minitel, refRow, refCol int, width, height int, dots bool) *Input {
return &Input{
m: m,
refRow: refRow,
width: width,
refCol: refCol,
height: height,
dots: dots,
}
}
func NewInputWithValue(m *Minitel, value string, refRow, refCol int, width, height int, dots bool) *Input {
return &Input{
m: m,
Value: []byte(value),
refRow: refRow,
width: width,
refCol: refCol,
height: height,
dots: dots,
}
}
// getCursorPos returns the absolute position of the cursor
func (i *Input) getCursorPos() (row, col int) {
len := i.Len()
if len == i.height*i.width {
len -= 1 // do not move the cursor to the next pos
}
row = len/i.width + i.refRow
col = len%i.width + i.refCol
return
}
func (i *Input) Len() int {
return utf8.RuneCount(i.Value)
}
func (i *Input) isReturn() bool {
return i.Len() > 0 && i.Len()%i.width == 0
}
func (i *Input) isFull() bool {
return i.Len() == i.width*i.height
}
// Init displays the input empty
func (i *Input) Init() {
i.UnHide()
}
// AppendKey appends a new Rune to the Value array
func (i *Input) AppendKey(r rune) {
if i.isFull() {
i.m.Bell()
return
}
command := EncodeRune(r)
i.m.Send(command)
i.Value = utf8.AppendRune(i.Value, r)
if i.isFull() {
i.m.Left(1)
} else if i.isReturn() {
i.m.Return(1)
i.m.Left(i.refCol)
}
}
// Correction removes the last key, on screen and within Value
func (i *Input) Correction() {
if utf8.RuneCount(i.Value) == 0 {
return
}
r, shift := utf8.DecodeLastRune(i.Value)
if r == utf8.RuneError {
return
}
if !i.isFull() {
i.m.Left(1)
}
i.Value = i.Value[:len(i.Value)-shift]
if i.dots {
i.m.Print(".")
} else {
i.m.Print(" ")
}
i.m.Left(1)
if i.isReturn() {
i.m.Up(1)
i.m.Right(i.width)
}
}
// UnHide reveals the input on screen
func (i *Input) UnHide() {
command := []byte{}
for row := i.refRow; row < i.refRow+i.height; row += 1 {
command = append(command, MoveAt(row, i.refCol, i.m.supportCSI)...)
if i.dots {
command = append(command, RepeatRune('.', i.width)...)
} else {
command = append(command, RepeatRune(' ', i.width)...)
}
}
command = append(command, MoveAt(i.refRow, i.refCol, i.m.supportCSI)...)
if len(i.Value) > 0 {
command = append(command, EncodeBytes(i.Value)...)
}
i.m.Send(command)
}
// Hide clears the input on the Minitel screen,
// but it keeps the Value member complete
func (i *Input) Hide() {
i.m.CursorOff()
i.m.MoveAt(i.refRow, i.refCol)
for row := 0; row < i.height; row += 1 {
i.m.Repeat(Sp, i.width)
if i.refCol+i.width < 39 {
i.m.Return(1)
i.m.Right(i.refCol)
} else {
i.m.LineStart()
i.m.Right(i.refCol)
}
}
}
// Reset clears both the input on screen and Value
func (i *Input) Reset() {
i.Value = []byte{}
i.UnHide()
}
// Activate moves the cursor to its actual position and let it on
func (i *Input) Activate() {
i.m.MoveAt(i.getCursorPos())
i.m.CursorOn()
}
func (i *Input) Deactivate() {
i.m.CursorOff()
}