34 void _init_console_mode()
36 if (hStdOut == INVALID_HANDLE_VALUE)
38 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
39 if (hStdOut == INVALID_HANDLE_VALUE)
41 DWORD lastError = ::GetLastError();
42 std::cout <<
Helpers::formatstring(
"GetStdHandle(STD_OUTPUT_HANDLE) failed: code = 0x%x", lastError) << std::endl;
45 if (hStdIn == INVALID_HANDLE_VALUE)
47 hStdIn = GetStdHandle(STD_INPUT_HANDLE);
48 if (hStdIn != INVALID_HANDLE_VALUE)
50 ::GetConsoleMode(hStdIn, &inputMode);
54 DWORD lastError = ::GetLastError();
55 std::cout <<
Helpers::formatstring(
"GetStdHandle(STD_INPUT_HANDLE) failed: code = 0x%x", lastError) << std::endl;
71 if (hStdIn != INVALID_HANDLE_VALUE)
74 if (!::SetConsoleMode(hStdIn, newMode))
76 DWORD lastError = ::GetLastError();
77 std::cout <<
Helpers::formatstring(
"SetConsoleMode(hStdIn, newMode) failed: code = 0x%x", lastError) << std::endl;
81 tcgetattr(STDIN_FILENO, &oldt);
83 newt.c_lflag &= ~(ICANON | ECHO);
84 tcsetattr(STDIN_FILENO, TCSANOW, &
newt);
96 if (hStdIn != INVALID_HANDLE_VALUE)
98 if (!::SetConsoleMode(hStdIn, inputMode))
100 DWORD lastError = ::GetLastError();
101 std::cout <<
Helpers::formatstring(
"SetConsoleMode(hStdIn, inputMode) failed: code = 0x%x", lastError) << std::endl;
105 tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
142 std::cout <<
"\x1b[" << row <<
";" << column <<
"H";
151 if (row !=
nullptr && column !=
nullptr)
155 if (!wasInputEchoDisabled)
160 std::cout <<
"\x1b[6n";
162 char buffer[16]{ 0 };
164 std::cin.get(buffer,
_countof(buffer),
'R');
165 int lastChar = std::cin.get();
166 std::string input(buffer);
167 input += (char)lastChar;
169 if (input.size() > 2 && input[0] ==
'\x1b' && input[1] ==
'[')
171 std::vector<std::string> elements =
split(input,
"[;R");
172 if (elements.size() >= 3)
174 *row = std::stoi(elements[1],
nullptr);
175 *column = std::stoi(elements[2],
nullptr);
178 if (!wasInputEchoDisabled)
static bool inputEchoDisabled
static void _enableInputEcho(void)
Enable echoing of input.
static struct termios oldt newt
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.
int ENABLE_ECHO_INPUT
Flag to enable echoing everything in standard input.
int INVALID_HANDLE_VALUE
Windows indicator of an invalid handle.
int ENABLE_LINE_INPUT
Flag to enable buffering standard input until Enter is pressed.
The namespace containing all the "helper" functions in the C++ code.
void enableinputecho()
Enable echoing input, which should be the default mode. Call this only after calling disableinputecho...
std::string formatstring(const char *fmt,...)
Use the given string and arguments to return a buffer containing the formatted string....
void disableinputecho()
Disable echoing input until enableinputecho() is called.
std::vector< std::string > split(const char *pszString, const char *splitChars)
Split the given string into a list of strings given the character on which to split....
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.