diff --git a/include/output.h b/include/output.h index 3dab2fd..2494291 100644 --- a/include/output.h +++ b/include/output.h @@ -19,6 +19,8 @@ typedef struct { } ant_output_stream_t; ant_output_stream_t *ant_output_stream(FILE *stream); + +void ant_output_init_console(void); void ant_output_stream_begin(ant_output_stream_t *out); bool ant_output_stream_reserve(ant_output_stream_t *out, size_t extra); diff --git a/src/main.c b/src/main.c index 50ffa23..2bc0b0b 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ #include "watch.h" #include "reactor.h" #include "runtime.h" +#include "output.h" #include "snapshot.h" #include "esm/loader.h" #include "esm/library.h" @@ -387,6 +388,7 @@ int main(int argc, char *argv[]) { if (internal_crash_report_mode) argc = 1; if (!internal_crash_report_mode && !getenv("ANT_NO_CRASH_HANDLER")) ant_crash_init(argc, argv); + ant_output_init_console(); setup_console_colors(); parse_ant_debug_flags(); diff --git a/src/output.c b/src/output.c index 7fc190a..3f8c84d 100644 --- a/src/output.c +++ b/src/output.c @@ -5,6 +5,11 @@ #include #include +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif + #include "output.h" #define ANT_OUTPUT_INITIAL_CAP 65536 @@ -12,6 +17,18 @@ static ant_output_stream_t g_stdout_writer = { .stream = NULL }; static ant_output_stream_t g_stderr_writer = { .stream = NULL }; +void ant_output_init_console(void) { +#ifdef _WIN32 + DWORD mode = 0; + bool has_console_output = + GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &mode) || + GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), &mode); + + if (has_console_output) SetConsoleOutputCP(CP_UTF8); + if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode)) SetConsoleCP(CP_UTF8); +#endif +} + ant_output_stream_t *ant_output_stream(FILE *stream) { ant_output_stream_t *out = (stream == stderr) ? &g_stderr_writer : &g_stdout_writer; out->stream = stream;