Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New custom print function to handle windows unicode #131

Merged
merged 6 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/commands/init.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const args = @import("args");

const util = @import("../util.zig");
const cli = @import("../cli.zig");

const uPrint = @import("library/unicodeprint.zig");
const init_data = @import("init_data").init_data;

/// Command line options for the `init` command.
Expand Down Expand Up @@ -209,15 +209,15 @@ pub fn run(
// const git_setup = false;
// if (git_setup) try gitSetup(allocator, install_dir);

std.debug.print(
try uPrint.unicodePrint(
\\
\\Setup complete! ✈️ 🦎
\\
\\Launch your new application:
\\
\\ $ cd {s}
\\
\\ $ zig build run
\\ $ zig build run or jetzig server
\\
\\And then browse to http://localhost:8080/
\\
Expand Down
41 changes: 41 additions & 0 deletions cli/commands/library/unicodeprint.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const std = @import("std");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this belongs in cli/util.zig - we already import this in cli/commands/init.zig.

const builtin = @import("builtin");

pub fn unicodePrint(comptime fmt: []const u8, args: anytype) !void {
if (builtin.os.tag == .windows) {
// Windows-specific code
const cp_out = try UTF8ConsoleOutput.init();
defer cp_out.deinit();

std.debug.print(comptime fmt, args);
} else {
// Non-Windows platforms just print normally
std.debug.print(fmt, args);
}
}
const UTF8ConsoleOutput = struct {
original: if (builtin.os.tag == .windows) c_uint else void,

fn init() !UTF8ConsoleOutput {
if (builtin.os.tag == .windows) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you flip this around you can use a guard clause to remove some indentation:

if (builtin.os.tag != .windows) return .{ .original = {} };
// ... windows logic

const original = std.os.windows.kernel32.GetConsoleOutputCP();
if (original == 0) {
return error.FailedToGetConsoleOutputCP;
}
const result = std.os.windows.kernel32.SetConsoleOutputCP(65001); // UTF-8 code page
if (result == 0) {
return error.FailedToSetConsoleOutputCP;
}
return .{ .original = original };
}
// For non-Windows, return an empty struct
return .{ .original = {} };
}

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