-
Notifications
You must be signed in to change notification settings - Fork 3
/
ansiterminal.common.spin2h
572 lines (477 loc) · 14.6 KB
/
ansiterminal.common.spin2h
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
{
--------------------------------------------
Filename: ansiterminal.common.spin2h
Description: Add ANSI terminal functionality to a
terminal driver (P2 version)
Requires: Terminal driver that provides the following methods:
putchar(ch) - Output one character to terminal
getchar() - Read one character from terminal
dec(num) - Output a decimal number to terminal
Author: Jesse Burt
Copyright (c) 2023
Created: Jun 18, 2019
Updated: Jan 26, 2023
See end of file for terms of use.
--------------------------------------------
}
CON
{ Clear modes }
CLR_CUR_TO_END = 0 ' Clear from cursor position to end of line
CLR_CUR_TO_BEG = 1 ' Clear from cursor position to beginning of line
CLR_ALL_HOME = 2 ' Clear screen and return to home position
CLR_ALL_DEL_SCRLB = 3 ' Clear all and delete scrollback
{ Graphic Rendition modes }
SGR_RESET = 0 ' Reset text attributes
SGR_INTENSITY_BOLD = 1 ' Text intensity
SGR_INTENSITY_FAINT = 2
SGR_INTENSITY_NML = 22
SGR_ITALIC = 3 ' Can be either Italic or
' SGR_INVERSE = 3 ' Inverse, depending on the terminal
SGR_UNDERLINE = 4 ' Underlined text
SGR_UNDERLINE_DBL = 21
SGR_UNDERLINE_OFF = 24
SGR_BLINKSLOW = 5 ' Blinking text
SGR_BLINKFAST = 6 ' Not supported by all terminals
SGR_BLINK_OFF = 25
SGR_INVERSE = 7 ' Inverse text, or inverse terminal
SGR_INVERSE_OFF = 27
SGR_CONCEAL = 8 ' Concealed text
SGR_REVEAL = 28
SGR_STRIKETHRU = 9 ' strike-through text
SGR_STRIKETHRU_OFF = 29
SGR_PRI_FONT = 10 ' Select primary font
SGR_FGCOLOR_DEF = 39
SGR_BGCOLOR_DEF = 49
SGR_FRAMED = 51 '
SGR_ENCIRCLED = 52 ' Not supported by many terminals
SGR_FRAMED_ENC_OFF = 54 '
SGR_OVERLINED = 53 ' Overlined text
SGR_OVERLINED_OFF = 55
{ Text colors }
FG = 30
BG = 40
BRIGHT = 60
BLACK = 0
DKGREY = BRIGHT + BLACK
GREY = 7
WHITE = BRIGHT + GREY
RED = 1
LTRED = BRIGHT + RED
GREEN = 2
LTGREEN = BRIGHT + GREEN
YELLOW = 3
BROWN = 3
LTYELLOW = BRIGHT + YELLOW
LTBROWN = BRIGHT + BROWN
BLUE = 4
LTBLUE = BRIGHT + BLUE
MAGENTA = 5
VIOLET = 5
LTMAGENTA = BRIGHT + MAGENTA
LTVIOLET = BRIGHT + VIOLET
CYAN = 6
LTCYAN = BRIGHT + CYAN
{ Semi-graphics characters }
{ Corners: }
LOWRT = $6a ' Lower-right
UPPRT = $6b ' Upper-right
UPPLT = $6c ' Upper-left
LOWLT = $6d ' Lower-left
CROSS = $6e ' Cross/4-way
{ Horizontal and Vertical lines }
HLINE = $71 ' H-line
LVLHSTEM = $74 ' Left V-line w/H-stem
RVLHSTEM = $75 ' Right V-line w/H-stem
BHLVSTEM = $76 ' Bottom H-line w/V-stem
THLVSTEM = $77 ' Top H-line w/V-stem
VLINE = $78 ' V-line
PUB bgcolor_24bpp(bcolor)
' Set background color
' Valid values: $00_00_00_00..$ff_ff_ff_00 (RR_GG_BB 24bpp, left-justified)
csi()
puts(@"48;2;") ' BG color
dec(bcolor.byte[3]) ' R
putchar(";")
dec(bcolor.byte[2]) ' G
putchar(";")
dec(bcolor.byte[1]) ' B
putchar("m")
PUB bgcolor(bcolor)
' Set background color
' Valid values:
' BLACK (0)
' RED (1)
' GREEN (2)
' YELLOW (3)
' BLUE (4)
' MAGENTA (5)
' CYAN (6)
' (add BRIGHT (60) to color value for brighter/lighter shade)
csi()
putchar(";")
dec(BG + bcolor)
putchar("m")
PUB blink()
' Set blink attribute
sgr(SGR_BLINKSLOW)
PUB blink_fast()
' Set fast blink mode
sgr(SGR_BLINKFAST)
PUB bold()
' Set bold attribute
sgr(SGR_INTENSITY_BOLD)
PUB bold_off()
' Clear bold attribute
sgr(SGR_INTENSITY_NML)
PUB box(x0, y0, x1, y1, brd_fg, brd_bg, filled) | inwidth, inheight, inbot, intop, x, y
' Draw a box, using semigraphics characters
' x0, y0, x1, y1: upper left, lower right coords
' brd_fg, brd_bg: border foreground, background colors
' filled: fill box (bool)
inwidth := x1-x0-1 ' inside border w, h
inheight := y1-y0-1
intop := y0+1 ' inside border top, bot.
inbot := y1-1
case filled
false: ' not filled:
bgcolor(brd_bg)
fgcolor(brd_fg)
pos_xy(x0, y0) ' draw top:
semi_gfx_ena(true) ' enable semi_gfx_ena mode
putchar(UPPLT) ' upper-left corner
repeat inwidth
putchar(HLINE) ' fill horiz. lines
putchar(UPPRT) ' upper-right corner
pos_xy(x0, y1) ' draw bottom
putchar(LOWLT)
repeat inwidth
putchar(HLINE)
putchar(LOWRT)
repeat y from intop to inbot ' draw left side
pos_xy(x0, y)
putchar(VLINE) ' v-lines
repeat y from intop to inbot ' draw right side
pos_xy(x1, y)
putchar(VLINE)
semi_gfx_ena(false) ' done. turn off semi_gfx_ena mode
true: ' filled:
bgcolor(brd_bg)
fgcolor(brd_fg)
pos_xy(x0, y0) ' set window pos
semi_gfx_ena(true) ' turn on semi_gfx_ena mode
putchar(UPPLT) ' top row:
repeat inwidth ' fill with horiz. lines
putchar(HLINE)
putchar(UPPRT)
repeat y from intop to inbot ' middle rows:
pos_xy(x0, y) ' left border
putchar(VLINE) ' vert. lines
repeat inwidth
putchar(" ") ' fill line with spaces
putchar(VLINE) ' right border
pos_xy(x0, y)
putchar(LOWLT) ' bottom row
repeat inwidth
putchar(HLINE)
putchar(LOWRT)
semi_gfx_ena(false) ' done. turn off semi_gfx_ena mode
PUB clear()
' Clear the terminal
clear_mode(CLR_ALL_HOME)
PUB clearline = clear_ln
PUB clear_line = clear_ln
PUB clear_ln()
' Clear current line of terminal
clear_ln_mode(CLR_CUR_TO_END)
PUB clearlinex = clear_ln_mode
PUB clear_ln_mode(mode)
' Clear line using explicitly set mode
csi()
dec(mode)
putchar("K")
PUB clearmode = clear_mode
PUB clear_mode(mode)
' Clear screen using explicitly set mode
csi()
dec(mode)
putchar("J")
if (mode == CLR_ALL_HOME)
position (0, 0)
PUB color(fcolor, bcolor)
' Set foreground and background colors
' Valid values:
' BLACK (0)
' RED (1)
' GREEN (2)
' YELLOW (3)
' BLUE (4)
' MAGENTA (5)
' CYAN (6)
' (add BRIGHT (60) to color value for brighter/lighter shade)
csi()
dec(FG + fcolor)
putchar(";")
dec(BG + bcolor)
putchar("m")
PUB conceal()
' Set conceal attribute
sgr(SGR_CONCEAL)
PUB cursorpositionreporting = curs_pos_report_ena
PUB curs_pos_report_ena(enabled)
' Enable/disable mouse cursor position reporting
csi()
if (enabled)
str(@"?1000;1006;1015h")
else
str(@"?1000;1006;1015l")
PUB curs_down_rows = curs_down_rows
PUB curs_down_lines(lines)
' Move cursor number of lines down
csi()
dec(lines)
putchar("E")
PUB cursornextline = curs_next_line
PUB curs_next_row = curs_next_line
PUB curs_next_line()
' Move cursor to beginning of next lines
csi()
dec(1)
putchar("E")
PUB curs_up_rows = curs_up_lines
PUB curs_up_lines(lines)
' Move cursor number of lines up
csi()
dec(lines)
putchar("F")
PUB cursorprevline = curs_prev_line
PUB curs_prev_row = curs_prev_line
PUB curs_prev_line(rows)
' Move cursor to beginning of previous row
csi()
dec(1)
putchar("F")
PUB encircle()
' Set encircle attribute
sgr(SGR_ENCIRCLED)
PUB faint()
' Set faint attribute
sgr(SGR_INTENSITY_FAINT)
PUB faint_off()
' Clear faint attribute
sgr(SGR_INTENSITY_NML)
PUB fgcolor(fcolor)
' Set foreground color
' Valid values:
' BLACK (0)
' RED (1)
' GREEN (2)
' YELLOW (3)
' BLUE (4)
' MAGENTA (5)
' CYAN (6)
' (add BRIGHT (60) to color value for brighter/lighter shade)
csi()
dec(FG + fcolor)
putchar("m")
PUB fgcolor_24bpp(bcolor)
' Set foreground color (24-bpp)
' Valid values: $00_00_00_00..$ff_ff_ff_00 (RR_GG_BB 24bpp, left-justified)
csi()
puts(@"38;2;") ' FG color
dec(bcolor.byte[3]) ' R
putchar(";")
dec(bcolor.byte[2]) ' G
putchar(";")
dec(bcolor.byte[1]) ' B
putchar("m")
PUB framed()
' Set framed attribute
sgr(SGR_FRAMED)
PUB hidecursor = hide_cursor
PUB hide_cursor()
' Hide cursor
csi()
putchar("?")
dec(25)
putchar("l")
PUB home()
' Move cursor to home/upper-left position
pos_xy(0, 0)
PUB inverse()
' Set inverse attribute
sgr(SGR_INVERSE)
PUB inverse_off()
' Clear inverse attribute
sgr(SGR_INVERSE_OFF)
PUB italic()
' Set italicized attribute
sgr(SGR_ITALIC)
PUB movedown = move_down
PUB move_down()
' Move cursor down 1 row
csi()
dec(1)
putchar("B")
PUB move_down_lines = move_down_rows
PUB move_down_rows(rows)
' Move cursor down 1 or more rows
csi()
dec(rows)
putchar("B")
PUB moveleft = move_left
PUB move_left()
' Move cursor back/left 1 column
csi()
dec(1)
putchar("D")
PUB move_left_cols(cols)
' Move cursor back/left 1 or more columns
csi()
dec(cols)
PUB moveright = move_right
PUB move_right()
' Move cursor forward/right 1 column
csi()
dec(1)
putchar("C")
PUB move_right_cols(cols)
' Move cursor forward/right 1 or more columns
csi()
dec(columns)
putchar("C")
PUB moveup = move_up
PUB move_up()
' Move cursor up 1 row
csi()
dec(1)
putchar("A")
PUB move_up_lines = move_up_rows
PUB move_up_rows(rows)
' Move cursor up 1 or more rows
csi()
dec(rows)
putchar("A")
PUB normal()
' Clear all attributes
sgr(SGR_RESET)
PUB overline()
' Set overline attribute
sgr(SGR_OVERLINED)
PUB overline_off{}
' Clear overline attribute
sgr(SGR_OVERLINED_OFF)
PUB position = pos_xy
PUB pos_xy(x, y)
' Position cursor at column x, row y (from top-left)
csi()
dec(y+1) ' 1-base coordinates
putchar(";")
dec(x+1)
putchar("f")
PUB positionx = pos_x
PUB pos_x(column)
' Set horizontal position of cursor
csi()
dec(column+1) ' 1-base coordinate
putchar("G")
PUB positiony = pos_y
PUB pos_y(y)
' Set vertical position of cursor
csi()
dec(y+1) ' 1-base coordinate
putchar("d")
PUB reset()
' Reset terminal attributes
csi()
putchar("m")
PUB scrolldown = scroll_down
PUB scroll_down()
' Scroll display down 1 line
csi()
dec(1)
putchar("T")
PUB scroll_down_rows = scroll_down_lines
PUB scroll_down_lines(lines)
' Scroll display down 1 or more lines
csi()
dec(lines)
putchar("T")
PUB scrollup = scroll_up
PUB scroll_up()
' Scroll display up 1 line
csi()
dec(1)
putchar("S")
PUB scroll_up_rows = scroll_up_rows
PUB scroll_up_lines(lines=1)
' Scroll display up 1 or more lines
csi()
dec(lines)
putchar("S")
PUB semigfx = semi_gfx_ena
PUB semi_gfx_ena(enabled=1)
' Enable semi-graphics (e.g., line-drawing) mode
if (enabled)
str(string(ESC, "(", "0"))
else
str(string(ESC, "(", "B"))
PUB showcursor = show_cursor
PUB show_cursor()
' Show cursor
csi()
putchar("?")
dec(25)
putchar("h")
PUB strikethrough = strikethru
PUB strikethru()
' Set strike-through attribute
sgr(SGR_STRIKETHRU)
PUB strikethru_off()
' Clear strike-through attribute
sgr(SGR_STRIKETHRU_OFF)
PUB textwindow = text_win
PUB text_win(ptr_titlestr, x, y, w, h, brd_fg, brd_bg, ttl_fg)
' Draw a window with title string and border
' x, y: upper-left coords of window
' w, h: window dimensions (_outer_)
' brd_fg, brd_bg: window border foreground, background colors
' ttl_fg: title text fg color
box(x, y, x+w, y+h-1, brd_fg, brd_bg, true)
bgcolor(brd_bg)
fgcolor(ttl_fg)
pos_xy(x+2, y) ' display window title
str(ptr_titlestr)
PUB underline()
' Set underline attribute
sgr(SGR_UNDERLINE)
PUB underline_dbl()
' Set double-underline attribute
sgr(SGR_UNDERLINE_DBL)
PUB underline_off()
' Clear underline attribute
sgr(SGR_UNDERLINE_OFF)
PRI csi()
' Command Sequence Introducer
putchar(ESC)
putchar("[")
PRI sgr(mode)
' Select Graphic Rendition
csi()
dec(mode)
putchar("m")
DAT
{
Copyright 2022 Jesse Burt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}