Support printing unicode characters on windows#449
Conversation
|
@ahaoboy Can you try this branch to check that it works for you? All works as expected for me locally, but good to double-check |
Cool~ It works! |
|
We might want to add an alternative function |
|
@chqrlie Do you have any objections? |
| } | ||
| MultiByteToWideChar(CP_UTF8, 0, str, len, wstr, wlen); | ||
| wstr[wlen] = L'\0'; | ||
| WriteConsoleW(hConsole, wstr, wlen, &written, NULL); |
There was a problem hiding this comment.
Wouldn't this work?
DWORD prev = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
WriteConsoleA(hConsole, str, len, &written, NULL);
SetConsoleOutputCP(prev);Maybe you don't even need to use WriteConsoleA:
DWORD prev = GetConsoleOutputCP();
SetConsoleOutputCP(CP_UTF8);
fwrite(str, 1, len, con);
SetConsoleOutputCP(prev);There was a problem hiding this comment.
@bnoordhuis Apologies for the delay, WriteConsoleA still appeared to be needed, but prints successfully without needing the wstr conversion - so changed
eb0c4e8 to
776c769
Compare
| const char *str; | ||
| size_t len; | ||
|
|
||
| #ifdef _WIN32 |
There was a problem hiding this comment.
There are barely a couple of lines of shared code here. Perhaps defining the whole function once for Unix one for Windows would be more readable here.
There was a problem hiding this comment.
Good call! Separated into two definitions, ready for another look
From #447, Unicode characters are not correctly printed to the console with
console.login some cases