-
Notifications
You must be signed in to change notification settings - Fork 174
Support printing unicode characters on windows #449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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? |
quickjs-libc.c
Outdated
} | ||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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
quickjs-libc.c
Outdated
@@ -3882,17 +3882,41 @@ static JSValue js_print(JSContext *ctx, JSValue this_val, | |||
const char *str; | |||
size_t len; | |||
|
|||
#ifdef _WIN32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! Separated into two definitions, ready for another look
From #447, Unicode characters are not correctly printed to the console with
console.log
in some cases