Skip to content

Change console encoding to UTF-8 by default #4417

Description

@miere43

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]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions