Skip to content

Commit

Permalink
main: Configure console on Windows
Browse files Browse the repository at this point in the history
Fix console output on Windows by configuring the console to use UTF-8
and enable interpretation of ANSI escape sequences. This will only work
correctly on recent Windows versions.
  • Loading branch information
Zirias committed Jan 24, 2024
1 parent 77956d8 commit 5a85965
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/bin/dos2ansi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifdef _WIN32
# include <io.h>
# include <fcntl.h>
# include <windows.h>
#endif

int main(int argc, char **argv)
Expand Down Expand Up @@ -64,7 +65,17 @@ int main(int argc, char **argv)
else
{
#ifdef _WIN32
_setmode(_fileno(stdout), _O_BINARY);
int outfd = _fileno(stdout);
_setmode(outfd, _O_BINARY);
if (_isatty(outfd))
{
HANDLE outhdl = (HANDLE)_get_osfhandle(outfd);
DWORD mode;
GetConsoleMode(outhdl, &mode);
mode |= 4; /* ENABLE_VIRTUAL_TERMINAL_PROCESSING */
SetConsoleMode(outhdl, mode);
SetConsoleOutputCP(CP_UTF8);
}
#endif
ansifile = stdout;
}
Expand Down

0 comments on commit 5a85965

Please sign in to comment.