22static HANDLE hStdIn = INVALID_HANDLE_VALUE;
23static HANDLE hStdOut = INVALID_HANDLE_VALUE;
24static DWORD inputMode = 0;
26static struct termios oldt,
newt;
33static void _init_console_mode(
void)
35 if (hStdOut == INVALID_HANDLE_VALUE)
37 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
38 if (hStdOut == INVALID_HANDLE_VALUE)
40 DWORD lastError = GetLastError();
41 printf(
"GetStdHandle(STD_OUTPUT_HANDLE) failed: code = 0x%x\n", lastError);
44 if (hStdIn == INVALID_HANDLE_VALUE)
46 hStdIn = GetStdHandle(STD_INPUT_HANDLE);
47 if (hStdIn != INVALID_HANDLE_VALUE)
49 GetConsoleMode(hStdIn, &inputMode);
53 DWORD lastError = GetLastError();
54 printf(
"GetStdHandle(STD_INPUT_HANDLE) failed: code = 0x%x\n", lastError);
68 if (hStdIn != INVALID_HANDLE_VALUE)
70 DWORD newMode = inputMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
71 if (!SetConsoleMode(hStdIn, newMode))
73 DWORD lastError = GetLastError();
74 printf(
"SetConsoleMode(hStdIn, newMode) failed: code = 0x%x\n", lastError);
78 tcgetattr(STDIN_FILENO, &oldt);
80 newt.c_lflag &= ~(ICANON | ECHO);
81 tcsetattr(STDIN_FILENO, TCSANOW, &
newt);
93 if (hStdIn != INVALID_HANDLE_VALUE)
95 if (!SetConsoleMode(hStdIn, inputMode))
97 DWORD lastError = GetLastError();
98 printf(
"SetConsoleMode(hStdIn, inputMode) failed: code = 0x%x\n", lastError);
102 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
136 printf(
"\x1b[%d;%dH", row, column);
145 if (row != NULL && column != NULL)
149 if (!wasInputEchoDisabled)
156 char buffer[16] = { 0 };
159 size_t bufferIndex = 0;
160 while (c !=
'R' && c != EOF && bufferIndex <
sizeof(buffer))
163 buffer[bufferIndex] = (char)c;
168 if (bufferIndex <
sizeof(buffer))
171 if (bufferIndex > 2 && buffer[0] ==
'\x1b' && buffer[1] ==
'[')
174 split(buffer,
"[;R", &elements);
177 *row = (int)strtol(elements.
strings[1], NULL, 0);
178 *column = (int)strtol(elements.
strings[2], NULL, 0);
183 if (!wasInputEchoDisabled)
void disableinputecho(void)
Disable echoing input until enableinputecho() is called.
static bool inputEchoDisabled
void enableinputecho(void)
Enable echoing input, which should be the default mode. Call this only after calling disableinputecho...
static void _enableInputEcho(void)
Enable echoing of input.
static struct termios oldt newt
void setcursorposition(int row, int column)
Move the text cursor to the specified screen coordinates.
void getcursorposition(int *row, int *column)
Retrieve the current cursor position in the console window.
static void _disableInputEcho(void)
Disable echoing of input and disable line input mode (where the Enter key must be entered to complete...
Declaration of the setcursorposition() and getcursorposition() functions for manipulating the cursor ...
Declaration of the split functions, for splitting a string on delimiters.
void SplitList_Clear(SplitList *list)
Clear the given SplitList object so it can be reused again. Releases the list of sub-strings (but doe...
void split(char *s, const char *splitChars, SplitList *components)
Split the given path into multiple strings based on the given delimiter. The pointers to each string ...
Represents a collection of sub-strings split from a single string using the split() function.
size_t strings_count
Number of sub-strings.
const char ** strings
Pointers to each sub-string.