Skip to content
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

Change console encoding to UTF-8 by default #4417

Closed
miere43 opened this issue Jun 27, 2016 · 4 comments
Closed

Change console encoding to UTF-8 by default #4417

miere43 opened this issue Jun 27, 2016 · 4 comments

Comments

@miere43
Copy link
Contributor

miere43 commented Jun 27, 2016

Reasons:

  • You can't print anything non-English to console because it's encoded to UTF-8 by Nim:
echo "привет"

=>

╨┐╤А╨╕╨▓╨╡╤В
  • Exception messages forwarded from Windows cannot be expressed too:
Error: unhandled exception: ╨Э╨╡ ╤Г╨┤╨░╨╡╤В╤Б╤П ╨╜╨░╨╣╤В╨╕ ╤Г╨║╨░╨╖╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗.
 [OSError]

To fix this stuff you need to manually change console encoding yourself:

when defined(windows):
  proc setConsoleOutputCP(codepage: cint): cint
    {.stdcall, dynlib: "kernel32", importc: "SetConsoleOutputCP".}
  proc getConsoleOutputCP(): cint
    {.stdcall, dynlib: "kernel32", importc: "GetConsoleOutputCP".}

  let g_originalOutputCP = getConsoleOutputCP()
  #65001 - utf-8 codepage
  discard setConsoleOutputCP(65001)

echo "привет"

when defined(windows):
  # Restore original codepage
  discard setConsoleOutputCP(g_originalOutputCP)

=>

привет
Error: unhandled exception: Не удается найти указанный файл.
 [OSError]
@shawnye
Copy link

shawnye commented Jul 8, 2016

It seems that XP is not supported.

@Araq
Copy link
Member

Araq commented Jul 8, 2016

Create a PR please. Even Windows 10 still has this fucked up behaviour. Ridiculous.

@nxshock
Copy link

nxshock commented Jul 29, 2016

I could try to create a pull request, but I'm not sure in which file we need to add the code. This should be before the start of the main procedure (set codepage to unicode), then after the completion (return previous codepage). Please suggest.

@miere43
Copy link
Contributor Author

miere43 commented Jul 29, 2016

@nxshock, I'll take care of it, don't worry.

@Araq Araq closed this as completed in f16140f Jul 30, 2016
Araq added a commit that referenced this issue Jul 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants