From eebc432646e95778836562fcf0c27a10e408d946 Mon Sep 17 00:00:00 2001 From: Altagos Date: Mon, 16 Feb 2026 16:41:31 +0100 Subject: [PATCH] allow custom pretty fn --- example/main.zig | 11 +++++++++++ pretty.zig | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/example/main.zig b/example/main.zig index cd9d3dc..8e7be5d 100644 --- a/example/main.zig +++ b/example/main.zig @@ -23,6 +23,17 @@ const Gender = enum(u8) { male, female, nonbinary, other, _ }; const Person = struct { age: u8, gender: ?Gender, + + pub fn pretty(self: *const @This(), comptime ctx: pretty_mod.Context, run: *const pretty_mod.Runtime) !void { + run.setColor(ctx, .dim); + try run.write("AGE = "); + run.resetColor(); + try run.print("{d}", .{self.age}); + run.setColor(ctx, .dim); + try run.write(", GENDER = "); + run.resetColor(); + try run.print("{?}", .{self.gender}); + } }; const Nested = struct { diff --git a/pretty.zig b/pretty.zig index b43e469..1afc475 100644 --- a/pretty.zig +++ b/pretty.zig @@ -147,7 +147,7 @@ pub fn PrettyWithOptions(comptime T: type, comptime options: Options) type { }; } -const Runtime = struct { +pub const Runtime = struct { out: *Io.Writer, tty: Io.Terminal, @@ -185,7 +185,7 @@ const Runtime = struct { } }; -const Context = struct { +pub const Context = struct { depth: comptime_int = 0, options: Options, @@ -213,6 +213,10 @@ fn innerFmt( if (!opts.skip_type_name) try printType(T, ctx, run, value); + if (std.meta.hasMethod(T, "pretty")) { + return value.pretty(ctx, run); + } + return switch (info) { .bool => formatBool(ctx, run, value), .null => formatNull(ctx, run), -- 2.51.2