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
|
#if OS == .WINDOWS {
#load "windows.jai";
} else #if (OS == .LINUX) || (OS == .MACOS) {
#load "unix.jai";
} else {
#assert(false, "Unsupported OS.");
}
#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 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 rename to "setup()"
start :: () {
OS_prepare_terminal();
write_strings(Commands.HideCursor, Commands.SaveCursorPosition, Commands.EnterAlternateBuffer, Commands.SetUTF8);
isTUIActive = true;
}
// TODO Maybe rename to "reset()"
stop :: () {
isTUIActive = false;
write_strings(Commands.EnterMainBuffer, Commands.RestoreCursorPosition, Commands.ShowCursor);
OS_reset_terminal();
}
draw_box :: (x: int, y: int, width: int, height: int) {
// 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.EnterDrawingMode,
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.EnterNormalMode);
}
// TODO Maybe rename to "clear()"
clear_terminal :: inline () {
write_string(Commands.ClearScreen);
}
// TODO Maybe rename to "get_size()"
get_terminal_size :: () -> rows: int, columns: int {
rows, columns := OS_get_terminal_size();
return rows, columns;
}
// read_input: () -> string {
// return OS_read_input();
// }
set_cursor_position :: (row: int, column: int) {
auto_release_temp();
tmp_string := tprint(Commands.SetCursorPosition, row, column);
write_string(tmp_string);
}
get_cursor_position :: () -> row: int, column: int {
assert(isTUIActive, "TUI is not active."); // TODO
write_string(Commands.QueryCursorPosition); // Returned
input := read_input(false);
// Result: \e[21;1R
assert(
input.data[0] == #char "\e" &&
input.data[1] == #char "[" &&
input.data[input.count-1] == #char "R",
"Query cursor position returned invalid response.");
input.data += 2;
print(">%<\n", input);
parts := split(input, ";");
row := parse_int(*parts[0]);
column := parse_int(*parts[1]);
return row, column;
}
read_input :: (blocking: bool = true) -> string {
return OS_read_input(blocking);
}
// read_input: () -> string;
#if OS == .WINDOWS {
OS_read_input :: (blocking: bool) -> string {
result: string = ---;
MAX_BYTES_TO_READ :: 1024;
temp : [MAX_BYTES_TO_READ] u8;
bytes_read : s32;
if ReadConsoleA(stdin, temp.data, xx temp.count, *bytes_read) {
result.data = alloc(bytes_read);
result.count = bytes_read;
memcpy(result.data, temp.data, bytes_read);
}
return result;
};
}
else #if OS == .LINUX || OS == .MACOS {
OS_read_input :: (blocking: bool) -> string {
term : My_Termios;
tcgetattr(STDIN_FILENO, *term);
// Input modes.
Input_Modes :: enum_flags u32 {
IGNBRK; // Ignore break condition.
BRKINT; // Signal interrupt on break.
IGNPAR; // Ignore characters with parity errors.
PARMRK; // Mark parity and framing errors.
INPCK; // Enable input parity check.
ISTRIP; // Strip 8th bit off characters.
INLCR; // Map NL to CR on input.
IGNCR; // Ignore CR.
ICRNL; // Map CR to NL on input.
IXON; // Enable start/stop output control.
IXOFF; // Enable start/stop input control.
IXANY; // Any character will restart after stop.
__NOT_USED__;
IMAXBEL; // Ring bell when input queue is full.
IUCLC; // Translate upper case input to lower case.
}
// Local modes.
Local_Modes :: enum_flags u32 {
ECHOKE; // Visual erase for KILL.
ECHOE; // Visual erase for ERASE.
ECHOK; // Echo NL after KILL.
ECHO; // Enable echo.
ECHONL; // Echo NL even if ECHO is off.
ECHOPRT; // Hardcopy visual erase.
ECHOCTL; // Echo control characters as ^X.
ISIG; // Enable signals.
ICANON; // Do erase and kill processing.
ALTWERASE; // Alternate WERASE algorithm.
IEXTEN; // Enable DISCARD and LNEXT.
EXTPROC; // External processing.
TOSTOP; // Send SIGTTOU for background output.
FLUSHO; // Output being flushed (state).
XCASE; // Canonical upper/lower case.
NOKERNINFO; // Disable VSTATUS.
PENDIN; // Retype pending input (state).
NOFLSH; // Disable flush after interrupt.
}
backup: My_Termios;
tcgetattr(STDIN_FILENO, *backup);
if blocking {
// write_number(term.c_iflag, 2);
// write_string(":");
// write_number(term.c_lflag, 2);
// write_string(":");
// write_number(__term.c_iflag, 2);
// write_string(":");
// write_number(__term.c_lflag, 2);
// c_iflag
// 10
// 8
// 0101 0101 0000 0000 : __term
// 1111 1010 0001 0100 : &MASK
// 0101 0000 0000 0000 :
// 0101 0101 0000 0000 : NOW
// c_lflag
// 15
// 3
// 1
// 0
// 1000 1010 0011 1011 : __term
// 0111 1111 1011 0100 : &MASK
// 0000 1010 0011 0000 :
// 1000 1010 0011 1011 : NOW
// print("%\n", term.c_iflag, to_standard_error = true);
// print("%\n", term.c_lflag, to_standard_error = true);
// print("%\n", term, to_standard_error = true);
// term = __term;
term.c_iflag |= xx cast(Input_Modes)(.IXOFF | .ICRNL);
term.c_lflag |= xx cast(Local_Modes)(.NOKERNINFO | .ECHO | .ECHOE | .ECHOKE);
}
else {
iflags: Input_Modes = (.IGNBRK | .BRKINT | .PARMRK | .ISTRIP | .INLCR | .IGNCR | .ICRNL | .IXON);
term.c_iflag &= xx ~(iflags);
lflags: Local_Modes = (.ECHO | .ECHONL | .ICANON | .IEXTEN);
term.c_lflag &= xx ~lflags;
}
// term.c_iflag &= 0xFFFFFA14;// ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
// term.c_oflag &= 0xFFFFFFFE;// ~OPOST;
// term.c_lflag &= 0xFFFF7FB4;// ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
// term.c_cflag &= 0xFFFFFECF;// ~(CSIZE | PARENB);
// term.c_cflag |= 0x00000030;
tcsetattr(STDIN_FILENO, 0, *term);
result: string = ---;
buffer: [8192] u8;
write_string(Commands.ShowCursor);
bytes_read := read(STDIN_FILENO, buffer.data, buffer.count-1);
write_string(Commands.HideCursor);
// TODO WIP WIP WIP
result.data = alloc(bytes_read, temp);
memcpy(result.data, buffer.data, bytes_read);
result.count = bytes_read;
// result = "jat";
// result.count = bytes_read;
// result.data = buffer.data;
// result = copy_temporary_string(buffer);
// result = tprint("%", xx buffer.data);
// result = to_string(buffer.data, bytes_read); // TODO WIP WIP WIP This is still using the stack allocated buffer and WILL FAIL!
tcsetattr(STDIN_FILENO, 0, *backup);
return result;
};
}
|