diff --git a/pretty.zig b/pretty.zig index 302d975..cbd6744 100644 --- a/pretty.zig +++ b/pretty.zig @@ -151,30 +151,32 @@ fn isComptimeType(comptime T: type) bool { } } +const global = struct { + var tty: ?Io.Terminal = null; + + pub inline fn setup(w: *std.Io.Writer) void { + if (tty == null) { + var buffer: [1]u8 = undefined; + const stderr = std.debug.lockStderr(&buffer).terminal(); + defer std.debug.unlockStderr(); + + tty = stderr; + tty.?.writer = w; + } + } +}; + pub fn PrettyComptimeValue(comptime T: type, comptime options: Options, comptime value: T) type { if (default_options.disable) return PrettyDisabled(T); - - const global = struct { - var tty: ?Io.Terminal = null; - }; const ctx: Context = Context{ .options = options }; return struct { - comptime value: T = value, - pub fn init(_: T) @This() { return .{}; } - pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { - if (global.tty == null) { - var buffer: [1]u8 = undefined; - const stderr = std.debug.lockStderr(&buffer).terminal(); - defer std.debug.unlockStderr(); - - global.tty = stderr; - global.tty.?.writer = w; - } + pub fn format(_: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { + global.setup(w); var run = Runtime{ .out = w, @@ -182,17 +184,14 @@ pub fn PrettyComptimeValue(comptime T: type, comptime options: Options, comptime .no_color = options.no_color, }; - try run.pretty(ctx, this.value, .{}); + try run.pretty(ctx, value, .{}); + try w.flush(); } }; } pub fn PrettyRuntimeValue(comptime T: type, comptime options: Options) type { if (default_options.disable) return PrettyDisabled(T); - - const global = struct { - var tty: ?Io.Terminal = null; - }; const ctx: Context = Context{ .options = options }; return struct { @@ -203,14 +202,7 @@ pub fn PrettyRuntimeValue(comptime T: type, comptime options: Options) type { } pub fn format(this: *const @This(), w: *std.Io.Writer) error{WriteFailed}!void { - if (global.tty == null) { - var buffer: [1]u8 = undefined; - const stderr = std.debug.lockStderr(&buffer).terminal(); - defer std.debug.unlockStderr(); - - global.tty = stderr; - global.tty.?.writer = w; - } + global.setup(w); var run = Runtime{ .out = w, @@ -219,6 +211,7 @@ pub fn PrettyRuntimeValue(comptime T: type, comptime options: Options) type { }; try run.pretty(ctx, this.value, .{}); + try w.flush(); } }; }