my little cross-platform Tcl/Tk pet-project uses utf-8 as the text encoding everywhere。
encoding system utf-8
fconfigure stderr -encoding utf-8
fconfigure stdout -encoding utf-8
#puts "汉语"
puts "foo"
This works great on Linux and macOS where the standard encoding is UTf-8:
$ tclsh utf8test.tcl
foo
Unfortunately it doesn't work at all on Windows where the terminal typically uses cp1252 and it consequently outputs garbage:
$ wine tclsh85.exe utf8test.tcl
潦?
As far as I understand this is because we cannot really change the encoding of the Windows terminal and - according to this sf issue - I need to detect myself whether the output is going to a terminal or somewhere else。
I have no idea how to make this decision。
So how can I output printable characters to the console in a project that otherwise uses UTF-8 everywhere?
edit1
My original example would output some high-Unicode points (Chinese characters Chinese)。
however the problem also persists if I try to output a single ASCII string (foo
).)。
since most of the text i'm outputting is ASCII anyhow I would like at least to fix the output for that 。
i thought that cp1252 would be backwards compatible with ASCII。。。
###Output to the real Console on Windows is special -- it has it's own channel implementation -- since the low-level API takes Unicode (I think it is actually UTF-16 but that isn't important here) and Tcl uses the low-level IO API on Windows where it can。 I do not know whether Tcl has automatically detected your console correctly。 I know absolutely nothing about what interaction there is between the API and Wine's implementation of it and whatever is going on in the host environment。
Console output is supposed to Just Work? if you don't modify the encoding on its channel。 Indeed the usual rule is don't change encodings except on files and sockets (where you know the encoding might be not the system one)。