The Incredible Unicode Mess

Made a comment on the issue with how I would fix it. Reproduced here:

const std = @import("std");
const builtin = @import("builtin");

const UTF8ConsoleOutput = struct {
    original: if (builtin.os.tag == .windows) c_uint else void,

    fn init() UTF8ConsoleOutput {
        if (builtin.os.tag == .windows) {
            const original = std.os.windows.kernel32.GetConsoleOutputCP();
            _ = std.os.windows.kernel32.SetConsoleOutputCP(65001);
            return .{ .original = original };
        }
        return .{ .original = {} };
    }

    fn deinit(self: UTF8ConsoleOutput) void {
        if (builtin.os.tag == .windows) {
            _ = std.os.windows.kernel32.SetConsoleOutputCP(self.original);
        }
    }
};

pub fn main() !void {
    const cp_out = UTF8ConsoleOutput.init();
    defer cp_out.deinit();

    std.debug.print("\u{00a9}", .{});
}
5 Likes