From e63c09e37f69af4b698ad7472356929144c72099 Mon Sep 17 00:00:00 2001 From: Altagos Date: Tue, 20 Jan 2026 14:39:51 +0100 Subject: [PATCH] pretty print the rest of the primitive types --- example/main.zig | 12 ++++++------ pretty.zig | 43 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/example/main.zig b/example/main.zig index 7c206d5..e84278c 100644 --- a/example/main.zig +++ b/example/main.zig @@ -10,14 +10,14 @@ const Hello = enum { world }; pub fn main(init: std.process.Init) !void { _ = init; - print("Booleans\n", .{}); + print("Pretty type - {f}\n", .{pretty(Hello)}); + print("Pretty null - {f}\n", .{pretty(null)}); + + print("\nBooleans\n", .{}); print("Boolean true - {f}\n", .{pretty(true)}); + print("Boolean false - {f}\n", .{pretty(false)}); print( - "Boolean false - {f}\n", - .{Pretty(bool, .{ .show_type_names = false, .type_value_seperator = "" }).init(false)}, - ); - print( - "Boolean true with type name - {f}\n", + "Boolean true always with type name - {f}\n", .{Pretty(bool, .{ .always_show_type_names = true, .type_value_seperator = ": " }).init(true)}, ); diff --git a/pretty.zig b/pretty.zig index 2da6a80..7558678 100644 --- a/pretty.zig +++ b/pretty.zig @@ -100,12 +100,18 @@ fn innerFmt( try printType(T, cctx, rctx); return switch (info) { + .type => formatType(rctx, value), + .null => formatNull(rctx), + .bool => formatBool(rctx, value), + // comptime types .comptime_int, .comptime_float, + // number types .int, .float, + // enum types .@"enum", .enum_literal, => formatValue(rctx, value), @@ -123,7 +129,13 @@ inline fn printType( comptime cctx: ComptimeContext, rctx: RuntimeContext, ) error{WriteFailed}!void { - if (cctx.depth != 0 or cctx.options.always_show_type_names) { + const active_type = comptime std.meta.activeTag(@typeInfo(T)); + const excluded = comptime switch (active_type) { + .void, .noreturn, .undefined, .null => true, + else => false, + }; + + if ((cctx.depth != 0 or cctx.options.always_show_type_names) and !excluded) { rctx.setColor(.dim); if (cctx.options.show_type_names) { @@ -135,14 +147,27 @@ inline fn printType( } } -inline fn formatBool(rctx: RuntimeContext, value: bool) !void { - rctx.setColor(if (value) .bright_green else .bright_red); - try rctx.print("{}", .{value}); - rctx.resetColor(); +inline fn formatBool(ctx: RuntimeContext, value: bool) !void { + ctx.setColor(if (value) .bright_green else .bright_red); + try ctx.print("{}", .{value}); + ctx.resetColor(); +} + +inline fn formatNull(ctx: RuntimeContext) !void { + ctx.setColor(.cyan); + try ctx.write("null"); + ctx.resetColor(); +} + +inline fn formatType(ctx: RuntimeContext, value: type) !void { + ctx.setColor(.bright_blue); + ctx.setColor(.bold); + try ctx.write(@typeName(value)); + ctx.resetColor(); } -inline fn formatValue(rctx: RuntimeContext, value: anytype) !void { - rctx.setColor(.blue); - try rctx.print("{}", .{value}); - rctx.resetColor(); +inline fn formatValue(ctx: RuntimeContext, value: anytype) !void { + ctx.setColor(.blue); + try ctx.print("{}", .{value}); + ctx.resetColor(); } -- 2.51.2