Skip to content

Commit

Permalink
Merge pull request #131 from z1fire/wip
Browse files Browse the repository at this point in the history
New custom print function to handle windows unicode
  • Loading branch information
z1fire authored Nov 30, 2024
2 parents 7b03850 + be8e517 commit 31f8a4a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
5 changes: 2 additions & 3 deletions cli/commands/init.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const args = @import("args");

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

const init_data = @import("init_data").init_data;

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

std.debug.print(
try util.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
34 changes: 34 additions & 0 deletions cli/util.zig
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,37 @@ pub fn environmentBuildOption(environment: cli.Environment) []const u8 {
inline else => |tag| "-Denvironment=" ++ @tagName(tag),
};
}



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: c_uint,

fn init() !UTF8ConsoleOutput {
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 };
}

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

0 comments on commit 31f8a4a

Please sign in to comment.