A pretty printer for zig
zig
1const std = @import("std");
2const print = std.debug.print;
3
4const pretty_mod = @import("pretty");
5const pretty = pretty_mod.pretty;
6const Pretty = pretty_mod.PrettyWithOptions;
7
8pub const pretty_options = pretty_mod.Options{ .always_show_type_names = true };
9
10const Hello = enum { world, developer };
11
12const Gender = enum(u8) { male, female, nonbinary, other, _ };
13
14const Person = struct {
15 age: u8,
16 gender: ?Gender,
17};
18
19const Nested = struct {
20 a: ChildA,
21 b: ChildB,
22};
23
24const ChildA = struct {
25 child: ChildC,
26};
27
28const ChildB = struct {
29 hello: Hello,
30};
31
32const ChildC = struct {
33 person: Person,
34};
35
36pub fn main(init: std.process.Init) !void {
37 _ = init;
38
39 print("Pretty type - {f}\n", .{pretty(Hello)});
40 print("Pretty null - {f}\n", .{pretty(null)});
41
42 print("\nBooleans\n", .{});
43 print("Boolean true - {f}\n", .{pretty(true)});
44 print("Boolean false - {f}\n", .{pretty(false)});
45 print(
46 "Boolean true always with type name - {f}\n",
47 .{Pretty(bool, .{
48 .always_show_type_names = true,
49 .theme = .{ .type_value_sep = ": " },
50 }).init(true)},
51 );
52
53 print("\nUnsigned Integers\n", .{});
54 print("Pretty u8 - {f}\n", .{pretty(@as(u8, 42))});
55 print("Pretty u16 - {f}\n", .{pretty(@as(u16, 42))});
56 print("Pretty u32 - {f}\n", .{pretty(@as(u32, 42))});
57 print("Pretty u64 - {f}\n", .{pretty(@as(u64, 42))});
58 print("Pretty usize - {f}\n", .{pretty(@as(usize, 42))});
59
60 print("\nSigned Integers\n", .{});
61 print("Pretty comptime_int - {f}\n", .{pretty(42)});
62 print("Pretty i8 - {f}\n", .{pretty(@as(i8, 42))});
63 print("Pretty i16 - {f}\n", .{pretty(@as(i16, 42))});
64 print("Pretty i32 - {f}\n", .{pretty(@as(i32, 42))});
65 print("Pretty i64 - {f}\n", .{pretty(@as(i64, 42))});
66 print("Pretty isize - {f}\n", .{pretty(@as(isize, 42))});
67
68 print("\nFloats\n", .{});
69 print("Pretty comptime_float - {f}\n", .{pretty(3.131)});
70 print("Pretty f16 - {f}\n", .{pretty(@as(f16, 3.141))});
71 print("Pretty f32 - {f}\n", .{pretty(@as(f32, 3.141))});
72 print("Pretty f64 - {f}\n", .{pretty(@as(f64, 3.141))});
73
74 print("\nEnums\n", .{});
75 print("Pretty enum - {f}\n", .{pretty(Hello.world)});
76 print("Pretty enum literal - {f}\n", .{pretty(.hello_world)});
77
78 const opt_null: ?Hello = null;
79 const opt_not_null: ?Hello = .developer;
80
81 print("\nOptionals\n", .{});
82 print("Pretty optional = null - {f}\n", .{pretty(opt_null)});
83 print("Pretty optional = not null - {f}\n", .{pretty(opt_not_null)});
84
85 const person = Person{ .age = 13, .gender = null };
86
87 const nested = Nested{
88 .a = .{ .child = .{ .person = person } },
89 .b = .{ .hello = .world },
90 };
91
92 print("\nStructs\n", .{});
93 print("Pretty simple struct - {f}\n", .{pretty(person)});
94 print(
95 "Pretty simple inlined struct - {f}\n",
96 .{Pretty(Person, .{ .inline_structs = true }).init(person)},
97 );
98 print("Pretty nested struct - {f}\n", .{pretty(nested)});
99}