enableUtf8Console function
void
enableUtf8Console()
Switch the process to UTF-8 console output so non-ASCII tool output renders correctly.
On Windows this sets the console input/output code pages to UTF-8 (the
chcp 65001 equivalent, done in-process via the Win32 API) and switches the
stdout/stderr sinks to UTF-8 encoding. On every other platform — where
the console is already UTF-8 — it is a no-op.
Safe to call more than once: only the first call takes effect. Every step is individually guarded, so a redirected/detached stdout (no attached console, or a sink that rejects an encoding change) degrades silently instead of crashing the tool.
Implementation
void enableUtf8Console() {
if (_consoleConfigured) return;
_consoleConfigured = true;
if (Platform.isWindows) {
_setWindowsConsoleCodePages();
}
// Encode our own output as UTF-8 so the (now UTF-8) console renders it
// correctly. On non-Windows hosts the sinks are already UTF-8; reasserting it
// is harmless.
try {
stdout.encoding = utf8;
} catch (_) {
// Sink does not allow changing the encoding (e.g. already written to, or
// detached). Nothing actionable — leave the default in place.
}
try {
stderr.encoding = utf8;
} catch (_) {
// As above.
}
}