-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
239 lines (211 loc) · 5.36 KB
/
handler.go
File metadata and controls
239 lines (211 loc) · 5.36 KB
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
package termfun
import (
"fmt"
"strings"
"github.com/exyzzy/termfun/format"
)
// TileHandler handles the rendering (output) and keypresses (input) for a specific type of tile
// TileType_ScrollDown renders from top to bottom and breaks lines between words if possible, it scolls up, down.
// TileType_ScrollDownClip renders from top to bottom and only breaks lines on newline in the text, it scrolls up, down, left, right.
// TileType_ScrollDownClipRaw is like TileType_ScrollDownClip but without any scroll key handling.
// TileType_ScrollUp behaves like a typical terminal, rendering bottom to top by line, it scrolls up, down.
type TileType int
const (
TileType_ScrollDown TileType = iota
TileType_ScrollDownClip
TileType_ScrollDownClipRaw
TileType_ScrollUp
)
type TileHandler struct {
TileType TileType
Render func(*Tile) string
KeyPress func(*Tile, rune) bool
}
var tileHandler = []*TileHandler{
{TileType: TileType_ScrollDown, Render: sd_RenderText, KeyPress: sd_KeyPress},
{TileType: TileType_ScrollDownClip, Render: sdc_RenderText, KeyPress: sdc_KeyPress},
{TileType: TileType_ScrollDownClipRaw, Render: sdc_RenderText, KeyPress: sdcr_KeyPress},
{TileType: TileType_ScrollUp, Render: su_RenderText, KeyPress: su_KeyPress}}
// Note that all render handlers must render full text boundary area, clearing as necessary
// renderLines renders top to bottom any lines that line within the tile boundary
// using blank lines as needed
func renderLinesDown(t *Tile, lines []string, origin func(*Tile)) string {
var str, newLine string
blankLine := strings.Repeat(" ", t.Width())
for y := t.bounds.Min.Y; y <= t.bounds.Max.Y; y++ {
str += CUP(t.bounds.Min.X, y)
index := y - t.bounds.Min.Y + t.start.Y
if index >= 0 && index < len(lines) {
newLine = lines[index]
} else {
newLine = blankLine
}
str += newLine
}
origin(t)
return str
}
// == TileType_ScrollDown Handler Functions
func sd_RenderText(t *Tile) string {
tabs := 3
lines := format.FormatTextBreak(t.buffer.String(), t.Width(), tabs)
return (renderLinesDown(t, lines, sd_SetCurPosOrigin))
}
func sd_SetCurPosOrigin(t *Tile) {
t.curPos.X = t.bounds.Min.X
t.curPos.Y = t.bounds.Min.Y
}
func sd_KeyPress(t *Tile, r rune) bool {
switch r {
case KeyUp:
if t.start.Y > 0 {
t.start.Y--
t.setDirty()
}
case KeyDown:
t.start.Y++
t.setDirty()
}
return false // return true from any keypress handler to exit TileTerm
}
// == TileType_ScrollDownClip Handler Functions
func sdc_RenderText(t *Tile) string {
tabs := 3
lines := format.FormatTextClipCol(t.buffer.String(), t.Width(), tabs, t.start.X)
return (renderLinesDown(t, lines, sd_SetCurPosOrigin))
}
func sdc_KeyPress(t *Tile, r rune) bool {
switch r {
case KeyUp:
if t.start.Y > 0 {
t.start.Y--
t.setDirty()
}
case KeyDown:
t.start.Y++
t.setDirty()
case KeyLeft:
if t.start.X > 0 {
t.start.X--
t.setDirty()
}
case KeyRight:
t.start.X++
t.setDirty()
}
return false
}
// == TileType_ScrollDownClipRaw Handler Functions
// returning true from any KeyCallback will exit TileTerm
type KeyCallback func(r rune) bool
func sdcr_KeyPress(t *Tile, r rune) bool {
if t.keyCallback != nil {
return t.keyCallback(r)
}
return false
}
// == TileType_ScrollUp Handler Functions
func su_RenderText(t *Tile) string {
var str string
blankLine := strings.Repeat(" ", t.Width())
ss := strings.Split(t.buffer.String(), "\n")
ss[len(ss)-1] = t.cursor + t.line // add cursor
curSS := len(ss) - 1
var sub []string
curSub := -1
var newLine string
for y := t.bounds.Max.Y; y >= t.bounds.Min.Y; y-- {
str += CUP(t.bounds.Min.X, y)
if curSub >= 0 {
newLine = sub[curSub]
curSub--
} else {
if curSS >= 0 {
sub = wrapStr(ss[curSS], t.Width())
curSub = len(sub) - 1
curSS--
newLine = sub[curSub]
curSub--
} else {
newLine = blankLine
}
}
str += newLine
}
su_SetCurPosOrigin(t)
t.curPos.X += len(t.cursor) + len(t.line)
return str
}
// wrapStr returns single string s as []strings that are clipped or space padded
func wrapStr(s string, ln int) []string {
var str string
var out []string
if ln == 0 {
return out
}
str = s
if len(str) == 0 {
str = " " //always one line min
}
for len(str) > 0 {
if len(str) > ln {
out = append(out, str[:ln])
str = str[ln:]
} else {
out = append(out, str+strings.Repeat(" ", ln-len(str)))
str = ""
}
}
return out
}
func su_SetCurPosOrigin(t *Tile) {
t.curPos.X = t.bounds.Min.X
t.curPos.Y = t.bounds.Max.Y
}
// returning true from any LineCallback will exit TileTerm
type LineCallback func(string) bool
func su_KeyPress(t *Tile, r rune) bool {
switch r {
case KeyEnter:
if t.lineCallback != nil {
if t.lineCallback(t.line) {
return true
}
t.historyIndex = -1
t.history.Add(t.line)
t.line = ""
}
// case KeyLeft, KeyRight:
case KeyUp:
entry, ok := t.history.NthPreviousEntry(t.historyIndex + 1)
if !ok {
return false
}
if t.historyIndex == -1 {
t.historyPending = t.line
}
t.historyIndex++
t.setLine(entry)
case KeyDown:
switch t.historyIndex {
case -1:
return false
case 0:
t.setLine(t.historyPending)
t.historyIndex--
default:
entry, ok := t.history.NthPreviousEntry(t.historyIndex - 1)
if ok {
t.historyIndex--
t.setLine(entry)
}
}
case KeyBackspace:
if len(t.line) > 0 {
t.line = t.line[:len(t.line)-1]
}
default:
t.line += fmt.Sprintf("%c", r)
}
return false
}