A pretty printer for zig
zig

pretty print the rest of the primitive types

altagos.dev e63c09e3 523ab221

verified
+40 -15
+6 -6
example/main.zig
··· 10 10 pub fn main(init: std.process.Init) !void { 11 11 _ = init; 12 12 13 - print("Booleans\n", .{}); 13 + print("Pretty type - {f}\n", .{pretty(Hello)}); 14 + print("Pretty null - {f}\n", .{pretty(null)}); 15 + 16 + print("\nBooleans\n", .{}); 14 17 print("Boolean true - {f}\n", .{pretty(true)}); 15 - print( 16 - "Boolean false - {f}\n", 17 - .{Pretty(bool, .{ .show_type_names = false, .type_value_seperator = "" }).init(false)}, 18 - ); 18 + print("Boolean false - {f}\n", .{pretty(false)}); 19 19 print( 20 - "Boolean true with type name - {f}\n", 20 + "Boolean true always with type name - {f}\n", 21 21 .{Pretty(bool, .{ .always_show_type_names = true, .type_value_seperator = ": " }).init(true)}, 22 22 ); 23 23
+34 -9
pretty.zig
··· 100 100 try printType(T, cctx, rctx); 101 101 102 102 return switch (info) { 103 + .type => formatType(rctx, value), 104 + .null => formatNull(rctx), 105 + 103 106 .bool => formatBool(rctx, value), 104 107 108 + // comptime types 105 109 .comptime_int, 106 110 .comptime_float, 111 + // number types 107 112 .int, 108 113 .float, 114 + // enum types 109 115 .@"enum", 110 116 .enum_literal, 111 117 => formatValue(rctx, value), ··· 123 129 comptime cctx: ComptimeContext, 124 130 rctx: RuntimeContext, 125 131 ) error{WriteFailed}!void { 126 - if (cctx.depth != 0 or cctx.options.always_show_type_names) { 132 + const active_type = comptime std.meta.activeTag(@typeInfo(T)); 133 + const excluded = comptime switch (active_type) { 134 + .void, .noreturn, .undefined, .null => true, 135 + else => false, 136 + }; 137 + 138 + if ((cctx.depth != 0 or cctx.options.always_show_type_names) and !excluded) { 127 139 rctx.setColor(.dim); 128 140 129 141 if (cctx.options.show_type_names) { ··· 135 147 } 136 148 } 137 149 138 - inline fn formatBool(rctx: RuntimeContext, value: bool) !void { 139 - rctx.setColor(if (value) .bright_green else .bright_red); 140 - try rctx.print("{}", .{value}); 141 - rctx.resetColor(); 150 + inline fn formatBool(ctx: RuntimeContext, value: bool) !void { 151 + ctx.setColor(if (value) .bright_green else .bright_red); 152 + try ctx.print("{}", .{value}); 153 + ctx.resetColor(); 154 + } 155 + 156 + inline fn formatNull(ctx: RuntimeContext) !void { 157 + ctx.setColor(.cyan); 158 + try ctx.write("null"); 159 + ctx.resetColor(); 160 + } 161 + 162 + inline fn formatType(ctx: RuntimeContext, value: type) !void { 163 + ctx.setColor(.bright_blue); 164 + ctx.setColor(.bold); 165 + try ctx.write(@typeName(value)); 166 + ctx.resetColor(); 142 167 } 143 168 144 - inline fn formatValue(rctx: RuntimeContext, value: anytype) !void { 145 - rctx.setColor(.blue); 146 - try rctx.print("{}", .{value}); 147 - rctx.resetColor(); 169 + inline fn formatValue(ctx: RuntimeContext, value: anytype) !void { 170 + ctx.setColor(.blue); 171 + try ctx.print("{}", .{value}); 172 + ctx.resetColor(); 148 173 }