A pretty printer for zig
zig

reordering and reformatting

altagos.dev 7011c7de f0c7fa74

verified
+11 -4
+2 -1
example/main.zig
··· 46 46 print("Pretty enum - {f}\n", .{pretty(Hello.world)}); 47 47 print("Pretty enum literal - {f}\n", .{pretty(.hello_world)}); 48 48 49 - print("\nOptionals\n", .{}); 50 49 const opt_null: ?Hello = null; 51 50 const opt_not_null: ?Hello = .developer; 51 + 52 + print("\nOptionals\n", .{}); 52 53 print("Pretty optional = null - {f}\n", .{pretty(opt_null)}); 53 54 print("Pretty optional = not null - {f}\n", .{pretty(opt_not_null)}); 54 55 }
+9 -3
pretty.zig
··· 7 7 show_type_names: bool = true, 8 8 always_show_type_names: bool = true, 9 9 10 - type_value_seperator: []const u8 = " = ", 10 + field_name_type_sep: []const u8 = ": ", 11 + type_value_sep: []const u8 = " = ", 11 12 }; 12 13 13 14 pub fn pretty(value: anytype) Pretty(@TypeOf(value)) { ··· 148 149 if (cctx.options.show_type_names) { 149 150 try rctx.write(@typeName(T)); 150 151 } 151 - try rctx.write(cctx.options.type_value_seperator); 152 + try rctx.write(cctx.options.type_value_sep); 152 153 153 154 rctx.resetColor(); 154 155 } ··· 166 167 ctx.resetColor(); 167 168 } 168 169 169 - inline fn formatOptional(comptime T: type, comptime cctx: ComptimeContext, rctx: RuntimeContext, value: ?T) !void { 170 + inline fn formatOptional( 171 + comptime T: type, 172 + comptime cctx: ComptimeContext, 173 + rctx: RuntimeContext, 174 + value: ?T, 175 + ) !void { 170 176 return if (value) |val| 171 177 innerFmt(T, cctx, rctx, val, .{ .dont_skip_type_name = false }) 172 178 else