Change console encoding to UTF-8 by default #4417

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

Projects

None yet

4 participants

@miere43
Contributor
miere43 commented Jun 27, 2016 edited

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
shawnye commented Jul 8, 2016

It seems that XP is not supported.

@Araq
Member
Araq commented Jul 8, 2016

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

@nxshock
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
Contributor
miere43 commented Jul 29, 2016 edited

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

@Araq Araq pushed a commit that closed this issue Jul 30, 2016
@miere43 miere43 fix #4417, update news f16140f
@Araq Araq closed this in f16140f Jul 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment