To complement Jocelyn's answer, the same ANSI code sequences can be used on Windows with recent update by making sure the console is initialized to handle the sequences:
-- Make sure the console is allocated in a non-console application.
io.output.end_of_file.do_nothing
-- Set console to terminal mode.
initialize_terminal
-- Use ANSI codes to color text.
print ("%/27/[31mSome_red_txt")
where
initialize_terminal
external "C inline"
alias "[
#ifdef EIF_WINDOWS
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return;
DWORD dwMode = 0;
if (!GetConsoleMode(hOut, &dwMode)) return;
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
#endif
]"
end
After such initialization, print
statements work the same on Windows and Linux.
If output can go not only to the console, but also to a file, a pipe, etc., error status of setting the terminal mode on Windows console can be recorded in the external feature and used later to avoid outputting ANSI sequences in such cases.