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
|
#if OS == .WINDOWS {
#load "windows.jai";
} else #if (OS == .LINUX) || (OS == .MACOS) {
#load "unix.jai";
} else {
#assert(false, "Unsupported OS.");
}
#import "Basic";
#import "String";
#import "Thread";
Drawings :: struct {
// TODO Maybe just use unicode instead of jumping back and forth between drawing mode?!
// test_drawing_without_mode :: () {
// top := "┌───┐";
// btm := "└───┘";
// print(">%<\n", top);
// print(">%<\n", btm);
// }
// #run test_drawing_without_mode();
CornerBR :: "\x6A";
CornerTR :: "\x6B";
CornerTL :: "\x6C";
CornerBL :: "\x6D";
Cross :: "\x6E";
LineH :: "\x71";
TeeL :: "\x74";
TeeR :: "\x75";
TeeB :: "\x76";
TeeT :: "\x77";
LineV :: "\x78";
Blank :: "\x5F";
Diamond :: "\x60";
Checkerboard :: "\x61";
PlusMinus :: "\x67";
LessThanOrEqual :: "\x79";
GreaterThanOrEqual :: "\x7A";
Pi :: "\x7B";
NotEqual :: "\x7C";
CenteredDot :: "\x7E";
}
Commands :: struct {
StartAlternateBuffer :: "\e[?1049h";
StartMainBuffer :: "\e[?1049l";
StartDrawingMode :: "\e(0";
StartNormalMode :: "\e(B";
ClearScreen :: "\e[2J";
ClearLine :: "\e[2K";
Bell :: "\x07";
SetWindowTitle :: "\e]0;%\x07";
RefreshWindow :: "\e[7t"; // TODO Not yet tested.
SetUTF8 :: "\e%G"; // TODO TEST ME PLEASE
// Cursor Position
SetCursorPosition :: "\e[%;%H";
// Cursor Visibility
ShowCursor :: "\e[?25h";
HideCursor :: "\e[?25l";
StartBlinking :: "\e[?25h]";
StopBlinking :: "\e[?25l]";
SaveCursorPosition :: "\e7";
RestoreCursorPosition :: "\e8";
// Cursor Shape
DefaultShape :: "\e[0 q";
BlinkingBlockShape :: "\e[1 q";
SteadyBlockShape :: "\e[2 q";
BlinkingUnderlineShape :: "\e[3 q";
SteadyUnderlineShape :: "\e[4 q";
BlinkingBarShape :: "\e[5 q";
SteadyBarShape :: "\e[6 q";
// Input Mode
KeypadAppMode :: "\e=";
KeypadNumMode :: "\e>";
CursorAppMode :: "\e[?1h";
CursorNormalMode :: "\e[?1l";
// Query State
QueryCursorPosition :: "\e[6n"; // Emits the cursor position as: "ESC [ <r> ; <c> R" Where <r> = row and <c> = column.
QueryDeviceAttributes :: "\e[0c";
QueryWindowSizeInChars :: "\e[18t"; // Emits the window size as: "ESC [ 8 <r> ; <c> t" Where <r> = row and <c> = column. TODO Does not work on windows.
}
// TODO Maybe make the OS_* procedures as inline?!
// We wanted the Key type to represent either UTF-8 encoded characters and also keyboard keys.
// The UTF-8 only requires up to 4 bytes, but some keyboard keys return up to 6 bytes.
// Therefore, we rounded it up to 8 bytes to support all this and more (if needed).
/*
In this block, we're testing having the Key as a union of both [8]u8 and u64.
It kinda looks OK, but it may drive the users to extract info from the code while
it should be used only for comparison.
*/
Key :: u64;
KEY_SIZE :: #run type_info(Key).runtime_size;
Keys :: struct {
// Terminal key-codes have 1 to 6 bytes, so we can signal special cases setting the edge-bytes.
None : Key : 0xF0000000_0000000F;
Resize : Key : 0xF0000000_0000001F;
Up : Key : #run to_key("\e[A");
Down : Key : #run to_key("\e[B");
Right : Key : #run to_key("\e[C");
Left : Key : #run to_key("\e[D");
PgUp : Key : #run to_key("\e[5~");
PgDown : Key : #run to_key("\e[6~");
}
/* TODO
This has to be compatible with: (#char "a" == key) ... so "a" must be stored in the LSB of key
|-|-|-|-|-|
string |a|b|c|0|0|
key/u64 |0|0|c|b|a| -> that in memory lays as (BE:|0|0|c|b|a|) and (LE:|a|b|c|0|0|)
*/
to_key :: inline (str: $T) -> Key #modify { return T == ([]u8) || T == string; } {
k: Key;
// #if DEBUG {
// assert(str.count <= 8); // TODO Add DEBUG to module parameters.
// }
for 0..str.count-1 #no_abc {
k |= ((cast(u64)str[it]) << (it*8));
}
return k;
}
to_string :: inline (key: Key) -> string {
str := talloc_string(KEY_SIZE);
str.count = 0;
while key != 0 #no_abc {
str[str.count] = xx key & 0xFF;
key >>= 8;
str.count += 1;
}
return str;
}
test_union :: ()
{
// ti := type_info(K);
print("\n---\n%\n---\n", type_info(Key).*);
print("\n---\n%\n---\n", type_info(Key).runtime_size);
a: Key;
b: u8;
print(">%\n", a == b);
print(">%\n", a != Keys.None);
c: string = "";
print(">%\n", a == to_key(c));
d: []u8 = xx "";
print(">%\n", a == to_key(d));
top := "┌───┐";
btm := "└───┘";
print(">%<\n", top);
print(">%<\n", btm);
str := "abcd";
tok := to_key(str);
tos := to_string(tok);
print("1:%:\n", str);
print("2:");
for 0..7 {
val := tok & 0xFF;
tok >>=8 ;
print("% ", FormatInt.{value=val, base=16, minimum_digits=2});
}
print(":\n");
print("3:%:\n", tos);
}
#run test_union();
// Terminal action codes are encoded with values incompatible with UTF-8 to avoid collisions.
initialized := false;
// input_buffer : [64] u8; // TODO FIXME Input buffer is too small!!!
input_buffer : [8] u8; // TODO FIXME Input buffer is too small!!!
input_string : string;
input_override : Key;
#run {
// Some tests.
assert(input_buffer.count >= 6, "The input buffer size must be capable to hold an entire terminal (6 bytes) or UTF8 (4 bytes) code.");
}
assert_is_initialized :: inline () {
assert(initialized, "TUI is not ready.");
}
set_next_key :: (key: Key) {
assert_is_initialized();
input_override = key;
}
get_key :: (timeout_milliseconds: s32 = -1) -> Key {
assert_is_initialized();
// BBBB BBBB & 1100 0000 == 10XX XXXX -> is continuation byte
is_utf8_continuation_byte :: inline (byte: u8) -> bool {
return (byte & 0xC0) == 0x80;
}
// BBBB BBBB & 1110 0000 == 110X XXXX -> 1 initial + 1 continuation byte
// BBBB BBBB & 1111 0000 == 1110 XXXX -> 1 initial + 2 continuation byte
// BBBB BBBB & 1111 1000 == 1111 0XXX -> 1 initial + 3 continuation byte
count_utf8_bytes :: inline (byte: u8) -> int {
if (byte & 0xE0) == 0xC0 return 1+1;
if (byte & 0xF0) == 0xE0 return 1+2;
if (byte & 0xF8) == 0xF0 return 1+3;
return 1;
}
if input_override != xx Keys.None {
defer input_override = xx Keys.None;
return input_override;
}
if OS_was_terminal_resized() return xx Keys.Resize;
// FIXME Old version...
// if input_string.count > 0 {
// defer advance(*input_string, 1);
// return input_string[0];
// }
// FIXME New version...
if input_string.count > 0 #no_abc { // TODO Some trickery requires no_abc... which is not that nice in this case...
utf8_bytes := count_utf8_bytes(input_string[0]);
// TODO We're assuming the terminal buffer contains the entirety of what we need to read for the UTF8 symbols.
if utf8_bytes > input_string.count {
diff := utf8_bytes - input_string.count;
// TODO Test this and make sure it's working...drop the following lines using Ctrl+V to fill the terminal buffer at once:
// d€€€a
// 1234567890123456789012345678901234567890
print("<WoW>");
// Copy buffered bytes to the start, and read the remaining ones from input.
for 0..input_string.count-1 {
input_buffer[it] = input_string[it];
}
aaa := OS_read_input(input_buffer.data + input_string.count, utf8_bytes-input_string.count); // TODO Does not check for read errors.
assert(aaa == diff, "READ MORE THAN EXPECTED");
input_string.data = input_buffer.data;
input_string.count = utf8_bytes;
}
to_parse := input_string;
to_parse.count = utf8_bytes;
key := to_key(to_parse);
advance(*input_string, utf8_bytes);
return key;
}
is_input_available := OS_wait_for_input(timeout_milliseconds);
if OS_was_terminal_resized() return xx Keys.Resize;
// FIXME Old version...
// if is_input_available {
// bytes_read := OS_read_input(input_buffer.data, input_buffer.count); // TODO Does not check for read errors.
// if bytes_read > 0 {
// input_string.data = input_buffer.data;
// input_string.count = bytes_read;
// defer advance(*input_string, 1);
// return input_string[0];
// }
// }
// FIXME New version...
if is_input_available {
bytes_read := OS_read_input(input_buffer.data, input_buffer.count); // TODO Does not check for read errors.
if bytes_read > 0 {
input_string.data = input_buffer.data;
input_string.count = bytes_read;
utf8_bytes := count_utf8_bytes(input_string[0]);
assert(utf8_bytes <= input_string.count, "The input buffer is too small."); // TODO Improve error message.
// TODO This is only being done after the OS_wait_for_input... for now!
to_parse := input_string;
to_parse.count = utf8_bytes;
// Must be a terminal escape sequence.
if utf8_bytes == 1 && input_string[0] == #char "\e" {
to_parse.count = ifx input_string.count > 6 then 6 else input_string.count; // TODO We should look into the input_string and search for the following escape sequence or somehting!?
}
key := to_key(to_parse);
advance(*input_string, to_parse.count);
return key;
/// /// /// /// /// /// /// /// ///
// DEBUG
// br, bc := get_cursor_position();
// column := 3;
// row += 1;
// nr, rc := get_terminal_size();
//
// if row >= (rc - 5) then row = 5;
//
// set_cursor_position(row, column);
// for 0..input_string.count-1 {
// print("%:% | ",
// FormatInt.{base= 2, minimum_digits = 8, value = input_string[it]},
// FormatInt.{base= 16, minimum_digits = 2, value = input_string[it]},
// ); // TODO DEBUG
// }
// set_cursor_position(br, bc);
/// /// /// /// /// /// /// /// ///
}
}
return xx Keys.None;
}
get_str :: (count_limit: int = -1, allocator: Allocator = temp) -> string {
assert_is_initialized();
assert(allocator.proc != null, "Argument 'allocator.proc' has invalid null value.");
if count_limit < 0 {
builder: String_Builder();
builder.allocator = allocator;
init_string_builder(*builder);
while(1) {
buffer := get_current_buffer(*builder);
buffer_data := get_buffer_data(buffer);
buffer.count = OS_read_input(buffer_data, buffer.allocated); // TODO Does not check for read errors.
if buffer.count < buffer.allocated break;
expand(*builder);
}
return builder_to_string(*builder, allocator);
}
else {
buffer := alloc_string(count_limit, allocator);
buffer.count = OS_read_input(buffer.data, count_limit);
return buffer;
}
}
start :: () {
if initialized == true return;
input_string.data = input_buffer.data;
input_string.count = 0;
input_override = xx Keys.None;
write_strings(Commands.HideCursor, Commands.SaveCursorPosition, Commands.StartAlternateBuffer, Commands.SetUTF8);
OS_prepare_terminal();
initialized = true;
}
stop :: () {
if initialized == false return;
initialized = false;
OS_reset_terminal();
write_strings(Commands.StartMainBuffer, Commands.RestoreCursorPosition, Commands.ShowCursor);
}
flush_input :: () {
OS_flush_input();
}
draw_box :: (x: int, y: int, width: int, height: int) {
assert_is_initialized();
// TODO Check if using a String_Builder improves performance (measure it)!
// TODO Validate input parameters against the terminal size.
assert(x > 0 && y > 0 && width > 1 && height > 1, "Invalid arguments.");
auto_release_temp();
tmp_string: string;
tmp_string = tprint(Commands.SetCursorPosition, y, x);
write_strings(
Commands.StartDrawingMode,
tmp_string,
Drawings.CornerTL
);
for 1..width-2 {
write_string(Drawings.LineH);
}
write_string(Drawings.CornerTR);
for idx: y+1..y+height-2 {
tmpL := tprint(Commands.SetCursorPosition, idx, x);
tmpR := tprint(Commands.SetCursorPosition, idx, x+width-1);
write_strings(
tmpL,
Drawings.LineV,
tmpR,
Drawings.LineV);
}
tmpBL := tprint(Commands.SetCursorPosition, y+height-1, x);
write_strings(
tmpBL,
Drawings.CornerBL);
for 1..width-2 {
write_string(Drawings.LineH);
}
write_string(Drawings.CornerBR);
write_string(Commands.StartNormalMode);
}
// TODO Maybe rename to "clear()"
clear_terminal :: inline () {
assert_is_initialized();
write_string(Commands.ClearScreen);
}
// TODO Maybe rename to "get_size()"
get_terminal_size :: () -> rows: int, columns: int {
assert_is_initialized();
rows, columns: int = ---;
#if OS == .WINDOWS {
rows, columns = OS_get_terminal_size();
}
else {
auto_release_temp();
flush_input();
write_string(Commands.QueryWindowSizeInChars);
input := get_str(64);
// Expected response format: \e[8;<r>;<c>t
// where <r> is the number of rows and <c> of columns.
FORMAT :: "\e[8;<r>;<c>t";
// Discard head noise.
while input.count >= 3 && (input[0] != FORMAT[0] || input[1] != FORMAT[1] || input[2] != FORMAT[2]) {
advance(*input);
}
// Discard tail noise.
while input.count >= 3 && input[input.count-1] != FORMAT[FORMAT.count-1] {
input.count -= 1;
}
assert(input.count >= 3 &&
input[0] == FORMAT[0] && input[1] == FORMAT[1] && input[2] == FORMAT[2] && input[input.count-1] == FORMAT[FORMAT.count-1],
"Query window size in chars returned invalid response.");
parts := split(input, ";");
rows = parse_int(*parts[1]);
columns = parse_int(*parts[2]);
}
return rows, columns;
}
set_cursor_position :: (row: int, column: int) {
assert_is_initialized();
auto_release_temp();
tmp_string := tprint(Commands.SetCursorPosition, row, column);
write_string(tmp_string);
}
get_cursor_position :: () -> row: int, column: int {
assert_is_initialized();
auto_release_temp();
flush_input();
write_string(Commands.QueryCursorPosition);
input := get_str(64);
// Expected response format: \e[<r>;<c>R
// where <r> is the number of rows and <c> of columns.
FORMAT :: "\e[<r>;<c>R";
// Discard head noise.
while input.count >= 2 && (input[0] != FORMAT[0] || input[1] != FORMAT[1]) {
advance(*input);
}
// Discard tail noise.
while input.count >= 2 && input[input.count-1] != FORMAT[FORMAT.count-1] {
input.count -= 1;
}
assert(input.count >= 2 &&
input[0] == FORMAT[0] && input[1] == FORMAT[1] && input[input.count-1] == FORMAT[FORMAT.count-1],
"Query cursor position returned invalid response.");
advance(*input, 2);
parts := split(input, ";");
row := parse_int(*parts[0]);
column := parse_int(*parts[1]);
return row, column;
}
set_terminal_title :: (title: string) {
assert_is_initialized();
print(Commands.SetWindowTitle, title);
}
#if OS == .WINDOWS {
// Prototyping zone... keep clear!
}
else #if OS == .LINUX || OS == .MACOS {
// Prototyping zone... keep clear!
}
|