From 9d24ba6ca33a413141016f4b40e6f7114057cf49 Mon Sep 17 00:00:00 2001 From: Altagos Date: Sat, 24 Jan 2026 23:20:40 +0100 Subject: [PATCH] no color and disable --- pretty.zig | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pretty.zig b/pretty.zig index 595a9c5..446d654 100644 --- a/pretty.zig +++ b/pretty.zig @@ -17,6 +17,9 @@ else Options{}; pub const Options = struct { + no_color: bool = false, + disable: bool = false, + indent_width: comptime_int = 2, inline_arrays: bool = false, @@ -60,9 +63,7 @@ pub const Theme = struct { color_false: Io.Terminal.Color = .bright_red, pub inline fn getColor(comptime this: Theme, comptime color: Color) Io.Terminal.Color { - return switch (color) { - inline else => |col| @field(this, "color_" ++ @tagName(col)), - }; + return @field(this, "color_" ++ @tagName(color)); } }; @@ -71,6 +72,18 @@ pub fn Pretty(comptime T: type) type { } pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { + if (default_options.disable) return struct { + value: T, + + pub fn init(val: T) @This() { + return .{ .value = val }; + } + + pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { + try w.print("{any}", .{this.value}); + } + }; + const global = struct { var tty: ?Io.Terminal = null; }; @@ -96,6 +109,7 @@ pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { var run = Runtime{ .out = w, .tty = global.tty.?, + .no_color = options.no_color, }; return innerFmt(T, ctx, &run, this.value, .{}); @@ -107,7 +121,7 @@ const Runtime = struct { out: *Io.Writer, tty: Io.Terminal, - depth: usize = 0, + no_color: bool = default_options.no_color, pub inline fn print( this: *const Runtime, @@ -126,14 +140,17 @@ const Runtime = struct { comptime ctx: Context, comptime color: Theme.Color, ) void { + if (this.no_color or default_options.no_color) return; this.tty.setColor(ctx.options.theme.getColor(color)) catch {}; } pub inline fn setColorRaw(this: *const Runtime, color: Io.Terminal.Color) void { + if (this.no_color or default_options.no_color) return; this.tty.setColor(color) catch {}; } pub inline fn resetColor(this: *const Runtime) void { + if (this.no_color or default_options.no_color) return; this.tty.setColor(.reset) catch {}; } }; -- 2.51.2