A pretty printer for zig
zig

pretty unions

altagos.dev 57b360a5 2aff8792

verified
+44 -3
+14 -1
example/main.zig
··· 89 89 const ErrorSet = error{ OutOfMemory, WriteFailed }; 90 90 const Result = ErrorSet!Hello; 91 91 92 + const HelloResult = union(ResultType) { 93 + const ResultType = enum { ok, err }; 94 + 95 + ok: Hello, 96 + err: ErrorSet, 97 + }; 98 + 92 99 pub fn main(init: std.process.Init) !void { 93 100 print("Pretty type - {f}\n", .{pretty(Hello)}); 94 101 print("Pretty null - {f}\n", .{pretty(null)}); ··· 154 161 ); 155 162 print("Pretty nested struct - {f}\n", .{pretty(nested)}); 156 163 164 + const simple_union = HelloResult{ .ok = .world }; 165 + 166 + print("\nUnions\n", .{}); 167 + print("Pretty simple union - {f}\n", .{pretty(simple_union)}); 168 + 157 169 print("\nArrays\n", .{}); 158 170 print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); 159 171 print( ··· 213 225 defer parsed.deinit(); 214 226 215 227 print("\nJson\n", .{}); 216 - print("{}", .{parsed.value}); 228 + print("Data - {f}\n", pretty(pretty(employees))); 229 + print("{f}\n", .{pretty(parsed.value)}); 217 230 }
+30 -2
pretty.zig
··· 30 30 inline_slice: bool = false, 31 31 32 32 inline_structs: bool = false, 33 + highlight_union_tag: bool = true, 33 34 34 35 show_type_names: bool = true, 35 36 always_show_type_names: bool = false, ··· 139 140 } 140 141 141 142 pub inline fn write(this: *const Runtime, text: []const u8) error{WriteFailed}!void { 142 - return this.out.writeAll(text); 143 + _ = try this.out.write(text); 143 144 } 144 145 145 146 pub inline fn setColor( ··· 188 189 const info = @typeInfo(T); 189 190 190 191 if (!opts.skip_type_name) 191 - try printType(T, ctx, run); 192 + try printType(T, ctx, run, value); 192 193 193 194 return switch (info) { 194 195 .bool => formatBool(ctx, run, value), ··· 208 209 209 210 .optional => |opt| formatOptional(opt.child, ctx, run, value), 210 211 .@"struct" => |st| formatStruct(T, ctx, st, run, value), 212 + .@"union" => formatUnion(T, ctx, run, value), 211 213 212 214 .error_set => formatErrorSet(ctx, run, value), 213 215 .error_union => formatErrorUnion(ctx, run, value), ··· 240 242 comptime T: type, 241 243 comptime ctx: Context, 242 244 run: *Runtime, 245 + value: T, 243 246 ) error{WriteFailed}!void { 244 247 const active_type = comptime std.meta.activeTag(@typeInfo(T)); 245 248 const excluded = comptime switch (active_type) { ··· 252 255 253 256 if (ctx.options.show_type_names) { 254 257 try run.write(@typeName(T)); 258 + 259 + if (active_type == .@"union") { 260 + if (ctx.options.highlight_union_tag) { 261 + run.resetColor(); 262 + run.setColor(ctx, .field); 263 + } 264 + 265 + try run.print("{}", .{std.meta.activeTag(value)}); 266 + 267 + if (ctx.options.highlight_union_tag) { 268 + run.resetColor(); 269 + run.setColor(ctx, .dim); 270 + } 271 + } 255 272 } 256 273 try run.write(ctx.options.theme.type_value_sep); 257 274 ··· 354 371 run.setColor(ctx, .dim); 355 372 try run.write(" }"); 356 373 run.resetColor(); 374 + } 375 + } 376 + 377 + inline fn formatUnion( 378 + comptime T: type, 379 + comptime ctx: Context, 380 + run: *Runtime, 381 + value: T, 382 + ) !void { 383 + switch (value) { 384 + inline else => |val| try innerFmt(@TypeOf(val), ctx, run, val, .{ .skip_type_name = true }), 357 385 } 358 386 } 359 387