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
|
// TODO Change this into a module with subfiles windows.jai and unix.jai
// #if OS == .WINDOWS {
// #load "windows.jai";
// } else #if (OS == .LINUX) || (OS == .MACOS) {
// #load "unix.jai";
// } else {
// #assert(false, "Unsupported OS.");
// }
// TODO On OS validations, use .LINUX or .MACOS to allow MACOS users to enj... errmmm test this.
#import "Basic";
#import "String";
// https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences
// https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#designate-character-set
// https://github.com/MicrosoftDocs/Console-Docs/blob/main/docs/console-virtual-terminal-sequences.md
isTUIActive := false; // TODO Rename this variable.
Drawings :: struct {
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 {
EnterAlternateBuffer :: "\e[?1049h";
EnterMainBuffer :: "\e[?1049l";
EnterDrawingMode :: "\e(0";
EnterNormalMode :: "\e(B";
ClearScreen :: "\e[2J";
ClearLine :: "\e[2K";
RefreshWindow :: "\e[7t"; // TODO Not yet tested.
SetUTF8 :: "\e%G"; // TODO TEST ME PLEASE
// 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.
}
start :: () {
#if OS == .LINUX {
// TODO Required to do unlocking input.
tcgetattr(STDIN_FILENO, *__term);
term_new := __term;
term_new.c_iflag &= 0xFFFFFA14;// ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
term_new.c_oflag &= 0xFFFFFFFE;// ~OPOST;
term_new.c_lflag &= 0xFFFF7FB4;// ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
term_new.c_cflag &= 0xFFFFFECF;// ~(CSIZE | PARENB);
term_new.c_cflag |= 0x00000030;
tcsetattr(STDIN_FILENO, 0, *term_new);
}
else {
print("TODO TUI\n", to_standard_error = true);
// stdin
stdin = GetStdHandle(STD_INPUT_HANDLE );
if stdin == INVALID_HANDLE_VALUE {
print("Invalid input handler.", to_standard_error = true);
return;
}
if GetConsoleMode(stdin, *initial_stdin_mode) == false {
print("Failed to get input mode.", to_standard_error = true);
return;
}
if SetConsoleMode(stdin, initial_stdin_mode | ENABLE_VIRTUAL_TERMINAL_INPUT) == false {
print("Failed to set input mode: %.", GetLastError(), to_standard_error = true);
return;
}
// stdout
stdout = GetStdHandle(STD_OUTPUT_HANDLE);
outMode: u32 = 0;
if stdout == INVALID_HANDLE_VALUE {
print("Invalid output handler.", to_standard_error = true);
return;
}
if GetConsoleMode(stdout, *initial_stdout_mode) == false {
print("Failed to get output mode.", to_standard_error = true);
return;
}
if SetConsoleMode(stdout, initial_stdout_mode | ENABLE_PROCESSED_OUTPUT| ENABLE_VIRTUAL_TERMINAL_PROCESSING) == false {
print("Failed to set output mode: %.", GetLastError(), to_standard_error = true);
return;
}
}
write_strings(Commands.HideCursor, Commands.SaveCursorPosition, Commands.EnterAlternateBuffer, Commands.SetUTF8);
isTUIActive = true;
}
stop :: () {
isTUIActive = false;
write_strings(Commands.EnterMainBuffer, Commands.RestoreCursorPosition, Commands.ShowCursor);
#if OS == .LINUX {
tcsetattr(STDIN_FILENO, 0, *__term); // return echo
}
else {
print("TODO TUI\n", to_standard_error = true);
if SetConsoleMode(stdin, initial_stdin_mode) == false {
print("Failed to reset input mode: %.", GetLastError(), to_standard_error = true);
return;
}
if SetConsoleMode(stdout, initial_stdout_mode) == false {
print("Failed to reset output mode: %.", GetLastError(), to_standard_error = true);
return;
}
}
}
draw_box :: (x: int, y: int, width: int, height: int, to_standard_error := false) {
// TODO Hardcoded box starting at 1,1... fix this!
write_strings(
// Commands.EnterNormalMode,
Commands.EnterDrawingMode,
"\e[1;1H", // Move to position 1,1
// TODO // Move pointer to top-left corner.
Drawings.CornerTL,
to_standard_error = to_standard_error);
for 1..width-2 {
write_string(Drawings.LineH, to_standard_error = to_standard_error);
}
write_string(Drawings.CornerTR, to_standard_error = to_standard_error);
// TODO Take care of the temporary allocations.
for idx: 2..height-1 {
tmpL := tprint("\e[%;%H", idx, 1);
tmpR := tprint("\e[%;%H", idx, width);
write_strings(
tmpL,
Drawings.LineV,
tmpR,
Drawings.LineV,
to_standard_error = to_standard_error);
}
tmpBL := tprint("\e[%;%H", height, 1);
write_strings(
tmpBL,
Drawings.CornerBL,
to_standard_error = to_standard_error);
for 1..width-2 {
write_string(Drawings.LineH, to_standard_error = to_standard_error);
}
write_string(Drawings.CornerBR, to_standard_error = to_standard_error);
write_strings(
// TODO // print
Commands.EnterNormalMode,
to_standard_error = to_standard_error);
}
clear_screen :: inline () {
write_string(Commands.ClearScreen);
}
// get_cursor_position :: () -> row: int, column: int {
// assert(isTUIActive, "TUI is not active."); // TODO
// write_string(TUI.Commands.QueryCursorPosition); // Returned "\e[21;1R"
// read_input()
// }
// read_input: () -> string;
#if OS == .LINUX {
#import "Basic";
#import "POSIX";
__term : My_Termios;
My_Termios :: struct {
c_iflag : u32; // Input mode flags.
c_oflag : u32; // Output mode flags.
c_cflag : u32; // Control modes flags.
c_lflag : u32; // Local modes flags.
c_line : u8; // Line discipline.
c_cc : [32]u8; // Control characters.
c_ispeed : u32; // Input speed (baud rates).
c_ospeed : u32; // Output speed (baud rates).
}
// Required to do unlocking input.
libc :: #system_library "libc";
// https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcsetattr.c.html
tcsetattr :: (fd : s32, optional_actions : s32, termios_p : *My_Termios) -> s32 #foreign libc;
// https://codebrowser.dev/glibc/glibc/sysdeps/unix/sysv/linux/tcgetattr.c.html
tcgetattr :: (fd : s32, termios_p : *My_Termios) -> s32 #foreign libc;
read_input :: () -> string {
buffer: [8192] u8;
bytes_read := read(STDIN_FILENO, buffer.data, buffer.count-1);
str := to_string(buffer.data, bytes_read);
return str;
};
get_buffer_size :: () -> rows: int, columns: int {
buffer: [512] u8;
write_string(Commands.QueryWindowSizeInChars);
bytes_read := read(STDIN_FILENO, buffer.data, buffer.count-1);
str := to_string(buffer.data, bytes_read);
// Result: [8;79;156t
assert(
buffer.data[0] == #char "\e" &&
buffer.data[1] == #char "[" &&
buffer.data[2] == #char "8",
"Query windows size in chars returned invalid response.");
parts := split(str, ";");
rows := parse_int(*parts[1]);
columns := parse_int(*parts[2]);
return rows, columns;
}
}
else #if OS == .WINDOWS {
#import "Windows";
ENABLE_VIRTUAL_TERMINAL_INPUT :: 0x0200;
ENABLE_PROCESSED_OUTPUT :: 0x0001;
ENABLE_WRAP_AT_EOL_OUTPUT :: 0x0002;
ENABLE_VIRTUAL_TERMINAL_PROCESSING :: 0x0004;
DISABLE_NEWLINE_AUTO_RETURN :: 0x0008;
ENABLE_LVB_GRID_WORLDWIDE :: 0x0010;
SHORT :: s16;
WORD :: u16;
DWORD :: s32;
COORD :: struct {
X : SHORT;
Y : SHORT;
}
SMALL_RECT :: struct {
Left : SHORT;
Top : SHORT;
Right : SHORT;
Bottom : SHORT;
}
CONSOLE_SCREEN_BUFFER_INFO :: struct {
dwSize : COORD;
dwCursorPosition : COORD;
wAttributes : WORD;
srWindow : SMALL_RECT;
dwMaximumWindowSize : COORD;
}
kernel32 :: #system_library "kernel32";
GetConsoleScreenBufferInfo :: (hConsoleOutput: HANDLE, lpConsoleScreenBufferInfo: *CONSOLE_SCREEN_BUFFER_INFO) -> bool #foreign kernel32;
ReadConsoleA :: (hConsoleHandle: HANDLE, buff : *u8, chars_to_read : s32, chars_read : *s32, lpInputControl := *void ) -> bool #foreign kernel32;
// ReadConsole :: (hConsoleInput: HANDLE, lpBuffer: *u8, nNumberOfCharsToRead: s32, lpNumberOfCharsRead: *s32, pInputControl := *void) -> bool #foreign kernel32;
GetConsoleMode :: (hConsoleHandle: HANDLE, lpMode: *u32) -> bool #foreign kernel32;
SetConsoleMode :: (hConsoleHandle: HANDLE, dwMode: u32) -> bool #foreign kernel32;
GetLastError :: () -> s32 #foreign kernel32;
stdin: HANDLE;
initial_stdin_mode: u32;
stdout: HANDLE;
initial_stdout_mode: u32;
// #run read_input = () -> string {
read_input :: () -> string {
MAX_BYTES_TO_READ :: 1024;
temp : [MAX_BYTES_TO_READ] u8;
result: string = ---;
bytes_read : s32;
if !ReadConsoleA( stdin, temp.data, xx temp.count, *bytes_read )
return "";
result.data = alloc(bytes_read);
result.count = bytes_read;
memcpy(result.data, temp.data, bytes_read);
return result;
};
get_buffer_size :: () -> rows: int, columns: int {
// GET WINDOW CHAR SIZE
ScreenBufferInfo: CONSOLE_SCREEN_BUFFER_INFO;
GetConsoleScreenBufferInfo(stdout, *ScreenBufferInfo);
Size: COORD;
Size.X = ScreenBufferInfo.srWindow.Right - ScreenBufferInfo.srWindow.Left + 1;
Size.Y = ScreenBufferInfo.srWindow.Bottom - ScreenBufferInfo.srWindow.Top + 1;
return Size.Y, Size.X;
}
}
|