From 57b360a570bfa289aa6f8dd02357c717a0bfe2d2 Mon Sep 17 00:00:00 2001 From: Altagos Date: Sun, 25 Jan 2026 11:39:59 +0000 Subject: [PATCH] pretty unions --- pretty.zig | 32 ++++++++++++++++++++++++++++++-- example/main.zig | 15 ++++++++++++++- 2 file(s) changed, 44 insertion(s)(+), 3 deletion(s)(-) diff --git a/pretty.zig b/pretty.zig --- a/pretty.zig +++ b/pretty.zig @@ -30,6 +30,7 @@ inline_slice: bool = false, inline_structs: bool = false, + highlight_union_tag: bool = true, show_type_names: bool = true, always_show_type_names: bool = false, @@ -139,7 +140,7 @@ } pub inline fn write(this: *const Runtime, text: []const u8) error{WriteFailed}!void { - return this.out.writeAll(text); + _ = try this.out.write(text); } pub inline fn setColor( @@ -188,7 +189,7 @@ const info = @typeInfo(T); if (!opts.skip_type_name) - try printType(T, ctx, run); + try printType(T, ctx, run, value); return switch (info) { .bool => formatBool(ctx, run, value), @@ -208,6 +209,7 @@ .optional => |opt| formatOptional(opt.child, ctx, run, value), .@"struct" => |st| formatStruct(T, ctx, st, run, value), + .@"union" => formatUnion(T, ctx, run, value), .error_set => formatErrorSet(ctx, run, value), .error_union => formatErrorUnion(ctx, run, value), @@ -240,6 +242,7 @@ comptime T: type, comptime ctx: Context, run: *Runtime, + value: T, ) error{WriteFailed}!void { const active_type = comptime std.meta.activeTag(@typeInfo(T)); const excluded = comptime switch (active_type) { @@ -252,6 +255,20 @@ if (ctx.options.show_type_names) { try run.write(@typeName(T)); + + if (active_type == .@"union") { + if (ctx.options.highlight_union_tag) { + run.resetColor(); + run.setColor(ctx, .field); + } + + try run.print("{}", .{std.meta.activeTag(value)}); + + if (ctx.options.highlight_union_tag) { + run.resetColor(); + run.setColor(ctx, .dim); + } + } } try run.write(ctx.options.theme.type_value_sep); @@ -354,6 +371,17 @@ run.setColor(ctx, .dim); try run.write(" }"); run.resetColor(); + } +} + +inline fn formatUnion( + comptime T: type, + comptime ctx: Context, + run: *Runtime, + value: T, +) !void { + switch (value) { + inline else => |val| try innerFmt(@TypeOf(val), ctx, run, val, .{ .skip_type_name = true }), } } diff --git a/example/main.zig b/example/main.zig --- a/example/main.zig +++ b/example/main.zig @@ -89,6 +89,13 @@ const ErrorSet = error{ OutOfMemory, WriteFailed }; const Result = ErrorSet!Hello; +const HelloResult = union(ResultType) { + const ResultType = enum { ok, err }; + + ok: Hello, + err: ErrorSet, +}; + pub fn main(init: std.process.Init) !void { print("Pretty type - {f}\n", .{pretty(Hello)}); print("Pretty null - {f}\n", .{pretty(null)}); @@ -154,6 +161,11 @@ ); print("Pretty nested struct - {f}\n", .{pretty(nested)}); + const simple_union = HelloResult{ .ok = .world }; + + print("\nUnions\n", .{}); + print("Pretty simple union - {f}\n", .{pretty(simple_union)}); + print("\nArrays\n", .{}); print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); print( @@ -213,5 +225,6 @@ defer parsed.deinit(); print("\nJson\n", .{}); - print("{}", .{parsed.value}); + print("Data - {f}\n", pretty(pretty(employees))); + print("{f}\n", .{pretty(parsed.value)}); } -- tangled.sh