Merge pull request #84 from rhues/fix-issue-52-windows-colors

Fixed issue 52 by using only native Windows colors.
This commit is contained in:
bobf 2024-06-04 18:11:29 +01:00 committed by GitHub
commit 1fb4509dbb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 54 deletions

View File

@ -31,50 +31,61 @@ pub const codes = .{
/// Map color codes generated by `std.io.tty.Config.setColor` back to `std.io.tty.Color`. Used by /// Map color codes generated by `std.io.tty.Config.setColor` back to `std.io.tty.Color`. Used by
/// `jetzig.loggers.LogQueue.writeWindows` to parse escape codes so they can be passed to /// `jetzig.loggers.LogQueue.writeWindows` to parse escape codes so they can be passed to
/// `std.io.tty.Config.setColor` (using Windows API to set console color mode). /// `std.io.tty.Config.setColor` (using Windows API to set console color mode).
const ansi_colors = .{
.{ "30", .black },
.{ "31", .red },
.{ "32", .green },
.{ "33", .yellow },
.{ "34", .blue },
.{ "35", .magenta },
.{ "36", .cyan },
.{ "37", .white },
.{ "90", .bright_black },
.{ "91", .bright_red },
.{ "92", .bright_green },
.{ "93", .bright_yellow },
.{ "94", .bright_blue },
.{ "95", .bright_magenta },
.{ "96", .bright_cyan },
.{ "97", .bright_white },
.{ "1", .bold },
.{ "2", .dim },
.{ "0", .reset },
};
pub const codes_map = if (@hasDecl(std, "ComptimeStringMap")) pub const codes_map = if (@hasDecl(std, "ComptimeStringMap"))
std.ComptimeStringMap(std.io.tty.Color, .{ std.ComptimeStringMap(std.io.tty.Color, ansi_colors)
.{ "30", .black },
.{ "31", .red },
.{ "32", .green },
.{ "33", .yellow },
.{ "34", .blue },
.{ "35", .magenta },
.{ "36", .cyan },
.{ "37", .white },
.{ "90", .bright_black },
.{ "91", .bright_red },
.{ "92", .bright_green },
.{ "93", .bright_yellow },
.{ "94", .bright_blue },
.{ "95", .bright_magenta },
.{ "96", .bright_cyan },
.{ "97", .bright_white },
.{ "1", .bold },
.{ "2", .dim },
.{ "0", .reset },
})
else if (@hasDecl(std, "StaticStringMap")) else if (@hasDecl(std, "StaticStringMap"))
std.StaticStringMap(std.io.tty.Color).initComptime(.{ std.StaticStringMap(std.io.tty.Color).initComptime(ansi_colors)
.{ "30", .black }, else
.{ "31", .red }, unreachable;
.{ "32", .green },
.{ "33", .yellow }, // Map basic ANSI color codes to Windows TextAttribute colors
.{ "34", .blue }, // used by std.os.windows.SetConsoleTextAttribute()
.{ "35", .magenta }, const windows_colors = .{
.{ "36", .cyan }, .{ "30", 0 },
.{ "37", .white }, .{ "31", 4 },
.{ "90", .bright_black }, .{ "32", 2 },
.{ "91", .bright_red }, .{ "33", 6 },
.{ "92", .bright_green }, .{ "34", 1 },
.{ "93", .bright_yellow }, .{ "35", 5 },
.{ "94", .bright_blue }, .{ "36", 3 },
.{ "95", .bright_magenta }, .{ "37", 7 },
.{ "96", .bright_cyan }, .{ "90", 8 },
.{ "97", .bright_white }, .{ "91", 12 },
.{ "1", .bold }, .{ "92", 10 },
.{ "2", .dim }, .{ "93", 14 },
.{ "0", .reset }, .{ "94", 9 },
}) .{ "95", 13 },
.{ "96", 11 },
.{ "97", 15 },
.{ "1", 7 },
.{ "2", 7 },
.{ "0", 7 },
};
pub const windows_map = if (@hasDecl(std, "ComptimeStringMap"))
std.ComptimeStringMap(u16, windows_colors)
else if (@hasDecl(std, "StaticStringMap"))
std.StaticStringMap(u16).initComptime(windows_colors)
else else
unreachable; unreachable;

View File

@ -273,23 +273,20 @@ fn initPool(allocator: std.mem.Allocator, T: type) std.heap.MemoryPool(T) {
fn writeWindows(file: std.fs.File, writer: anytype, event: Event) !void { fn writeWindows(file: std.fs.File, writer: anytype, event: Event) !void {
var info: std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined; var info: std.os.windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
const config: std.io.tty.Config = if (std.os.windows.kernel32.GetConsoleScreenBufferInfo( _ = std.os.windows.kernel32.GetConsoleScreenBufferInfo(
file.handle, file.handle,
&info, &info
) != std.os.windows.TRUE) );
.no_color
else
.{ .windows_api = .{
.handle = file.handle,
.reset_attributes = info.wAttributes,
} };
var it = std.mem.tokenizeSequence(u8, event.message[0..event.len], "\x1b["); var it = std.mem.tokenizeSequence(u8, event.message[0..event.len], "\x1b[");
while (it.next()) |token| { while (it.next()) |token| {
if (std.mem.indexOfScalar(u8, token, 'm')) |index| { if (std.mem.indexOfScalar(u8, token, 'm')) |index| {
if (index > 0 and index + 1 < token.len) { if (index > 0 and index + 1 < token.len) {
if (jetzig.colors.codes_map.get(token[0..index])) |color| { if (jetzig.colors.windows_map.get(token[0..index])) |color| {
try config.setColor(writer, color); try std.os.windows.SetConsoleTextAttribute(
file.handle,
color
);
try writer.writeAll(token[index + 1 ..]); try writer.writeAll(token[index + 1 ..]);
continue; continue;
} }