Skip to content

Commit

Permalink
Merge pull request #433
Browse files Browse the repository at this point in the history
  • Loading branch information
LIJI32 committed Jun 21, 2024
2 parents 407e540 + c9dfc86 commit cee486a
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions SDL/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,31 @@ static bool is_term(void)
{
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) return false;
#ifdef _WIN32
if (AllocConsole()) {
FreeConsole();
return false;
}

unsigned long input_mode, output_mode;
bool has_con_output;

GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &input_mode);
GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &output_mode);
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_INPUT);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
HANDLE stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleMode(stdin_handle, &input_mode);
has_con_output = GetConsoleMode(stdout_handle, &output_mode);
if (!has_con_output) {
return false; // stdout has been redirected to a file or pipe
}

SetConsoleMode(stdin_handle, ENABLE_VIRTUAL_TERMINAL_INPUT);
SetConsoleMode(stdout_handle, ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

CONSOLE_SCREEN_BUFFER_INFO before = {0,};
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &before);
GetConsoleScreenBufferInfo(stdout_handle, &before);

printf(SGR("0"));

CONSOLE_SCREEN_BUFFER_INFO after = {0,};
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &after);
GetConsoleScreenBufferInfo(stdout_handle, &after);

SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), input_mode);
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), output_mode);
SetConsoleMode(stdin_handle, input_mode);
SetConsoleMode(stdout_handle, output_mode);


if (before.dwCursorPosition.X != after.dwCursorPosition.X ||
Expand All @@ -127,7 +130,7 @@ static char raw_getc(void)
#ifdef _WIN32
char c;
unsigned long ret;
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &ret, NULL);
ReadFile(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &ret, NULL);
#else
ssize_t ret;
char c;
Expand Down

0 comments on commit cee486a

Please sign in to comment.