-
Notifications
You must be signed in to change notification settings - Fork 0
/
vterm.go
440 lines (383 loc) · 13.3 KB
/
vterm.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package fansiterm
import (
"bytes"
"fmt"
"image"
"image/draw"
"io"
"slices"
"sync"
"time"
"github.com/sparques/fansiterm/tiles"
"github.com/sparques/fansiterm/tiles/fansi"
"github.com/sparques/fansiterm/tiles/inconsolata"
"github.com/sparques/fansiterm/xform"
"github.com/sparques/gfx"
)
// Device implements a virtual terminal. It supports being io.Write()n to. It handles the cursor and processing of
// sequences.
//
//go:export
type Device struct {
// BellFunc is called if it is non-null and the terminal would
// display a bell character
// TODO: Implement affirmative beep (default) and negative acknowledge beep
// Negative acknowledge is produced when \a is sent while in SHIFT-OUT mode.
// Affirmative: C-G (quarter notes?)
// NAK: C♭ (whole note?)
BellFunc func()
// Config species the runtime configurable features of fansiterm.
Config Config
// cols and rows specify the size in characters of the terminal.
cols, rows int
// cursor collects together all the fields for handling the cursor.
cursor Cursor
// attr tracks the currently applied attributes
attr Attr
// attrDefault is used when attr is zero-value or nil
attrDefault Attr
// Render collects together all the graphical rendering fields.
Render Render
// inputBuf buffers chracters between call writes. This is exclusively used to
// buffer incomplete escape sequences.
inputBuf []rune
// Miscellaneous properties, like "Window Title"
Properties map[Property]string
// Output specifies the program attached to the terminal. This should be the
// same interface that the input mechanism (whatever that may be) uses to write
// to the program. On POSIX systems, this would be equivalent to Stdin.
// Default is io.Discard. Setting to nil will cause Escape Sequences that
// write a response to panic.
Output io.Writer
sync.Mutex
}
// Cursor is used to track the cursor.
type Cursor struct {
// col is the current column. This is zero indexed.
col int
// row is the current row. This is zero indexed.
row int
// show is whether we should be showing the the cursor.
show bool
// visible is whether or not the cursor is currently visible. When rendering text,
// we hide the cursor, then re-enable it when done.
visible bool
// prevPos is for saving cursor position; The indicies are col, row.
prevPos [2]int
}
type Render struct {
draw.Image
charSet tiles.Tiler
altCharSet tiles.Tiler
boldCharSet tiles.Tiler
italicCharSet tiles.Tiler
useAltCharSet bool
cell image.Rectangle
cursorFunc cursorRectFunc
// Some displays require a flush / blit / sync call
// this could be called at the end of (*Device).Write().
DisplayFunc func()
}
type Config struct {
TabSize int
CursorStyle int
CursorBlink bool
}
type Attr struct {
Bold bool
Underline bool
DoubleUnderline bool
Strike bool
Blink bool
Reversed bool
Italic bool
Fg Color
Bg Color
}
var AttrDefault = Attr{
Fg: ColorWhite,
Bg: ColorBlack,
}
var ConfigDefault = Config{
TabSize: 8,
}
// New returns an initialized *Device. If buf is nil, an internal buffer is used. Otherwise
// if you specify a hardware backed draw.Image, writes to Device will immediately be written
// to the backing hardware--whether this is instaneous or buffered is up to the device and the
// device driver.
func New(cols, rows int, buf draw.Image) *Device {
// Eventually I'd like to support different fonts and dynamic resizing
// I'm trying to get to an MVP first.
// thus, hardcoded font face
// 7x13 is smaller and non-antialiased. For small screens it might be a better choice
// than the 8x13 pre-render of inconsolata, however it doesn't have as many unicode-glyps
// as inconsolata.
//fontFace := basicfont.Face7x13
cell := image.Rect(0, 0, 8, 16)
if buf == nil {
buf = image.NewRGBA(image.Rect(0, 0, cols*cell.Max.X, rows*cell.Max.Y))
}
draw.Draw(buf, buf.Bounds(), image.Black, image.Point{}, draw.Src)
return &Device{
cols: cols,
rows: rows,
attr: AttrDefault,
Render: Render{
Image: buf,
altCharSet: fansi.AltCharSet,
charSet: inconsolata.Regular8x16,
boldCharSet: inconsolata.Bold8x16,
italicCharSet: &tiles.Italics{FontTileSet: inconsolata.Regular8x16},
cell: cell,
cursorFunc: blockRect,
},
cursor: Cursor{
show: true,
},
attrDefault: AttrDefault,
Config: ConfigDefault,
Output: io.Discard,
Properties: make(map[Property]string),
}
}
// NewAtResolution is like New, but rather than specifying the columns and rows,
// you specify the desired resolution. The maximum rows and cols will be determined
// automatically and the terminal rendered in the center.
// Fansiterm will only ever update / work on the rectangle it has claimed.
// If you want to use an existing backing buffer and position that, use NewWithBuf and
// use xform.SubImage() to locate the terminal.
func NewAtResolution(x, y int, buf draw.Image) *Device {
// TODO: This is a crappy way of figuring out what font we're using. Do something else.
d := New(1, 1, nil)
// use d.Render.cell to figure out rows and cols; integer division will round down
// which is what we want
cols := x / d.Render.cell.Max.X
rows := y / d.Render.cell.Max.Y
offset := image.Pt((x%d.Render.cell.Dx())/2, (y%d.Render.cell.Dy())/2)
//fmt.Println("Res:", x, "x", y, "Cols:", cols, "Rows:", rows, "Offset:", offset)
if buf == nil {
buf = image.NewRGBA(image.Rect(0, 0, x, y))
}
draw.Draw(buf, buf.Bounds(), image.Black, image.Point{}, draw.Src)
if offset.X == 0 && offset.Y == 0 {
// no offset needed, skip wrapping buf and save us some memory and cycles
return New(cols, rows, buf)
} else {
// return New(cols, rows, xform.Translate(buf, offset))
// return New(cols, rows, xform.NewImageTranslate(offset, buf))
return New(cols, rows,
xform.SubImage(buf, image.Rect(0, 0, cols*d.Render.cell.Dx(), rows*d.Render.cell.Dy()).Add(offset)))
}
}
// NewWithBuf uses buf as its target. NewWithBuf() will panic if called against a
// nil buf. If using fansiterm with backing hardware, NewWithBuf is likely the way
// you want to instantiate fansiterm.
// If you have buf providing an interface to a 240x135 screen, using the default
// 8x16 tiles, you can have an 40x8 cell terminal, with 7 rows of pixels leftover.
// If you want to have those extra 7 rows above the rendered terminal, you can do
// so like this:
//
// term := NewWithBuf(xform.SubImage(buf,image.Rect(0,0,240,128).Add(0,7)))
//
// Note: you can skip the Add() and just define your rectangle as
// image.Rect(0,7,240,135), but I find supplying the actual dimensions and then
// adding an offset to be clearer.
func NewWithBuf(buf draw.Image) *Device {
if buf == nil {
panic("NewWithBuf must be called with non-nil buf")
}
// TODO: How do I dynamically do this in a way that makes sense?
cols := buf.Bounds().Dx() / 8
rows := buf.Bounds().Dy() / 16
draw.Draw(buf, buf.Bounds(), image.Black, image.Point{}, draw.Src)
return New(cols, rows, buf)
}
// SetCursorStyle changes the shape of the cursor. Valid options are CursorBlock,
// CursorBeam, and CursorUnderscore. CursorBlock is the default.
func (d *Device) SetCursorStyle(style cursorRectFunc) {
d.hideCursor()
d.Render.cursorFunc = style
d.showCursor()
}
// VisualBell inverts the screen for a quarter second.
func (d *Device) VisualBell() {
draw.Draw(d.Render, d.Render.Bounds(), xform.InvertColors(d.Render), image.Point{}, draw.Src)
time.Sleep(time.Second / 4)
draw.Draw(d.Render, d.Render.Bounds(), xform.InvertColors(d.Render), image.Point{}, draw.Src)
}
// WriteAt works like calling the save cursor position escape sequence, then
// the absolute set cursor position escape sequence, writing to the terminal,
// and then finally restoring cursor position. The offset is just the i'th
// character on screen. Negative offset values are set to 0, values larger than
// d.rows * d.cols are set to d.rows*d.cols.
func (d *Device) WriteAt(p []byte, off int64) (n int, err error) {
col, row := d.cursor.col, d.cursor.row
defer func() {
d.hideCursor()
d.cursor.col = col
d.cursor.row = row
d.showCursor()
}()
if d.cursor.visible {
d.toggleCursor()
}
off = bound(off, 0, int64(d.rows*d.cols))
d.cursor.row = int(off) / d.cols
d.cursor.col = int(off) % d.cols
return d.Write(p)
}
func isControl(r rune) bool {
return r < 0x20
}
func isFinal(r rune) bool {
return r >= 0x40
}
// Write implements io.Write and is the main way to interract with with (*fansiterm).Device. This is
// essentially writing to the "terminal."
// Writes are more or less unbuffered with the exception of escape sequences. If a partial escape sequence
// is written to Device, the beginning will be bufferred and prepended to the next write.
func (d *Device) Write(data []byte) (n int, err error) {
d.Lock()
defer d.Unlock()
runes := bytes.Runes(data)
// first un-invert cursor (if we're showing it)
if d.cursor.visible {
d.toggleCursor()
}
if len(d.inputBuf) != 0 {
runes = append(d.inputBuf, runes...)
d.inputBuf = []rune{}
}
var endIdx int
for i := 0; i < len(runes); i++ {
switch runes[i] {
case '\a': // bell
if d.BellFunc != nil {
d.BellFunc()
}
case '\b': // backspace
// whatever is connected to the terminal needs to handle line/character editing
// however, when the terminal gets a backspace, that's the same as just moving cursor
// one space to the left. To perform a what looks like an actual backspace you must
// send "\b \b".
d.cursor.col = max(d.cursor.col-1, 0)
case '\t': // tab
// move cursor to nearest multiple of TabSize, but don't move to next row
d.cursor.col = min(d.cols-2, d.cursor.col+d.Config.TabSize-(d.cursor.col%d.Config.TabSize))
case '\r': // carriage return
d.cursor.col = 0
case '\n': // linefeed
d.cursor.row++
d.cursor.col = 0
d.ScrollToCursor()
case 0x0E: // shift out (use alt character set)
d.Render.useAltCharSet = true
case 0x0F: // shift in (use regular char set)
d.Render.useAltCharSet = false
case 0x1b: // ESC aka ^[
n, err = consumeEscSequence(runes[i:])
if err != nil {
// copy runes[i:] to d.inputBuf and wait for more input
d.inputBuf = runes[i:]
i += len(runes[i:])
continue
}
d.HandleEscSequence(runes[i : i+n])
i += n - 1
default:
// consume as many non-control characters as possible
// render these with RenderRunes
// increment cursor; increment i
// Originally I did this with strings.IndexFunc(string(runes[i:]), isControl)
// however this seems to return the byte offset rather than the rune offset
endIdx = slices.IndexFunc(runes[i:], isControl)
if endIdx == -1 {
endIdx = len(runes[i:])
}
// whichever comes first: end of runes, End of row, or a control char
endIdx = min(len(runes[i:]), d.cols-d.cursor.col, endIdx)
d.RenderRunes(runes[i : i+endIdx])
d.cursor.col += endIdx
if d.cursor.col >= d.cols {
d.cursor.col = 0
d.cursor.row++
}
i += endIdx - 1
d.ScrollToCursor()
}
}
// finally, update cursor, if needed
d.showCursor()
if d.Render.DisplayFunc != nil {
d.Render.DisplayFunc()
}
return len(data), nil
}
func (d *Device) Scroll(amount int) {
// if the underlying iimage support Scroll(), use that
if scrollable, ok := d.Render.Image.(gfx.Scroller); ok {
fmt.Println("yes, using scroller interface")
scrollable.Scroll(amount * d.Render.cell.Dy())
// fill in scrolls section with background
d.Clear(0, d.rows-amount, d.cols, d.rows)
return
}
if amount > 0 {
// shift the lower portion of the image up, row by row, starting with the row
// that will become thew new row zero
for y := (amount) * d.Render.cell.Dy(); y <= d.Render.cell.Dy()*(d.rows); y++ {
for x := d.Render.Bounds().Min.X; x <= d.Render.Bounds().Max.X; x++ {
// if y+-amount*d.Render.cell.Max.Y > d.Render.Bounds().Dy() {
// continue
// }
d.Render.Image.Set(x, y-amount*d.Render.cell.Dy()+d.Render.Bounds().Min.Y,
d.Render.Image.At(x, y+d.Render.Bounds().Min.Y),
)
}
}
// fill in the lower portion with Bg
d.Clear(0, d.rows-amount, d.cols, d.rows)
return
}
// negative scrolling
// shift the upper portion of the image down, pixel line-by-line, starting from bottom
// use d.Render.cell.Dy() * d.rows instead of d.Render.Bounds().Dy() because if we're using
// a draw.Image that's wrapped in an imageTranslate, we'll scroll pixes outside our render-area.
for y := d.Render.cell.Dy()*d.rows + (amount)*d.Render.cell.Dy(); y > 0; y-- {
for x := d.Render.Bounds().Min.X; x <= d.Render.Bounds().Max.X; x++ {
if y+-amount*d.Render.cell.Max.Y > d.Render.Bounds().Dy() {
continue
}
d.Render.Image.Set(x, y+-amount*d.Render.cell.Max.Y,
d.Render.Image.At(x, y),
)
}
}
// fill in scrolls section with background
d.Clear(0, 0, d.cols, -amount)
}
// ColsRemaining returns how many columns are remaining until EOL
func (d *Device) ColsRemaining() int {
return d.cols - d.cursor.col
}
func (d *Device) MoveCursorRel(x, y int) {
d.cursor.col = bound(x+d.cursor.col, 0, d.cols)
d.cursor.row = bound(y+d.cursor.row, 0, d.rows)
}
func (d *Device) MoveCursorAbs(x, y int) {
d.cursor.col = bound(x, 0, d.cols)
d.cursor.row = bound(y, 0, d.rows)
}
func (d *Device) ScrollToCursor() {
// this one shouldn't happen
if d.cursor.col > d.cols {
d.cursor.col = 0
d.cursor.row++
}
// this is the more common scenario
if d.cursor.row >= d.rows {
d.cursor.col = 0
d.cursor.row = d.rows - 1
d.Scroll(1)
}
}