Skip to content

Commit

Permalink
Try to terminal translation on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyley committed Jan 20, 2024
1 parent 317da6b commit ca7593b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const Api = @import("api");
const c = @import("c.zig").c;
const Config = @import("config.zig");

var terminal_translation = true;

pub const std_options = struct {
pub fn logFn(
comptime message_level: std.log.Level,
Expand All @@ -22,20 +24,29 @@ pub const std_options = struct {
.debug => "[1;35m",
.info => "[1;37m",
};

const level_txt = comptime message_level.asText();
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
const stderr = std.io.getStdErr().writer();

std.debug.getStderrMutex().lock();
defer std.debug.getStderrMutex().unlock();

nosuspend stderr.print("{c}" ++ color ++ level_txt ++ prefix2 ++ format ++ "{c}[0m\n", .{esc_code} ++ args ++ .{esc_code}) catch return;
if (terminal_translation) {
nosuspend stderr.print("{c}" ++ color ++ level_txt ++ prefix2 ++ format ++ "{c}[0m\n", .{esc_code} ++ args ++ .{esc_code}) catch return;
} else {
nosuspend stderr.print(level_txt ++ prefix2 ++ format, args) catch return;
}
}

pub const log_level = if (builtin.mode == .Debug) .debug else .info;
};

pub fn main() !void {
if (builtin.os.tag == .windows) {
terminal_translation = try @import("windows.zig").enableTerminalSequences();
}

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer if (gpa.deinit() == .leak) @panic("MEMORY LEAK");
const allocator = gpa.allocator();
Expand Down Expand Up @@ -384,6 +395,7 @@ pub fn runApp(allocator: std.mem.Allocator) !void {
}

if (config.close_upon_game_exit) {
std.log.info("User is not in a room, exiting...", .{});
return;
}

Expand Down
17 changes: 17 additions & 0 deletions src/windows.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const c = @cImport({
@cDefine("WIN32_LEAN_AND_MEAN", "1");
@cInclude("windows.h");
});

pub fn enableTerminalSequences() !bool {
const output_handle = c.GetStdHandle(c.STD_OUTPUT_HANDLE);
if (output_handle == c.INVALID_HANDLE_VALUE) {
return error.UnableToGetStdOutHandle;
}

if (c.SetConsoleMode(output_handle, c.ENABLE_VIRTUAL_TERMINAL_PROCESSING) == 0) {
return false;
}

return true;
}

0 comments on commit ca7593b

Please sign in to comment.