I am a Chinese Windows user.
I use Msys2+Mingw64 (export LANG="EN.UTF-8") to compile nim app. But I also use windows7 DOS console(the default codepage is cp936, which is almost gb2312). The following code performs different in msys2 and windows's console.
So my question, how to let the EXE echo properly no matter in whatever consoles? Thanks
# this file is saved as a UTF8 file
import future, encodings
# this line outputs "你好" in msys which is corrected.
# but "浣犲ソ" in windows' console which is wrong
echo "你好"
# this outputs "▒▒▒" in msys which is wrong.
# but "你好" in DOS which is corrected
echo convert("你好", "gb2312", "utf8")
Don't use Windows. Seems to be bad software ;)
No seriously. I have no idea. I can only guess what to look for. Does msys2 try to make getCurrentEncoding behave correctly? Does msys2 try to make everything more unix like and make the shell utf8? Does msys2 have any information about encoding?
I have similar problem, but with Cyrillic output on several Windows (XP, 7, 10). The best I did was to use low level routine SetOutputConsole to control the output. You can use GetOutputConsole to save current setting and restore it on application end. I hope something like this will be useful for you:
module console.nim:
# Low-level & OS specific routines
proc getConsoleOutputCP(): cint
{. importc: "GetConsoleOutputCP", stdcall, dynlib: "kernel32" discardable .}
proc setConsoleOutputCP(codepage: cint): cint
{. stdcall, dynlib: "kernel32", importc: "SetConsoleOutputCP", discardable .}
let originalOutCP = getConsoleOutputCP()
proc setConsole*(codepage: cint = 65001) =
setConsoleOutputCP(codepage)
proc restoreConsole* =
setConsoleOutputCP(originalOutCP)
Application:
import console
setConsole(936)
echo "你好"
restoreConsole()
There's a simpler way to change the codepage :)
import os
discard execShellCmd("chcp 936")
thanks but set cp936 seems do not work in msys2
https://github.com/zacharycarter/blt-nim may be a solution for simple console-like interactive