A pretty printer for zig
zig

improve consistency

altagos.dev 07bf4d26 99153376

verified
+32 -26
+18 -18
example/main.zig
··· 3 3 4 4 const pretty_mod = @import("pretty"); 5 5 const pretty = pretty_mod.pretty; 6 - const Pretty = pretty_mod.PrettyWithOptions; 6 + const prettyO = pretty_mod.prettyO; 7 7 8 8 pub const pretty_options = pretty_mod.Options{ 9 9 .always_show_type_names = true, ··· 45 45 print("Pretty null - {f}\n", .{pretty(null)}); 46 46 47 47 print("\nBooleans\n", .{}); 48 - print("Boolean true - {f}\n", .{pretty(true)}); 49 - print("Boolean false - {f}\n", .{pretty(false)}); 48 + print("Pretty bool true - {f}\n", .{pretty(true)}); 49 + print("Pretty bool false - {f}\n", .{pretty(false)}); 50 50 print( 51 - "Boolean true always with type name - {f}\n", 52 - .{Pretty(bool, .{ 51 + "Pretty bool true always with type name - {f}\n", 52 + .{prettyO(true, .{ 53 53 .always_show_type_names = true, 54 54 .theme = .{ .type_value_sep = ": " }, 55 - }).init(true)}, 55 + })}, 56 56 ); 57 57 58 58 print("\nUnsigned Integers\n", .{}); ··· 101 101 print("Pretty simple struct - {f}\n", .{pretty(person)}); 102 102 print( 103 103 "Pretty simple inline struct - {f}\n", 104 - .{Pretty(Person, .{ .inline_structs = true }).init(person)}, 104 + .{prettyO(person, .{ .inline_structs = true })}, 105 105 ); 106 106 print("Pretty nested struct - {f}\n", .{pretty(nested)}); 107 107 108 108 print("\nArrays\n", .{}); 109 - print("Array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); 109 + print("Pretty array of floats - {f}\n", .{pretty([_]f32{ 0.1, 0.2, 0.3 })}); 110 110 print( 111 - "Array of floats inline - {f}\n", 112 - .{Pretty([3]f32, .{ .inline_arrays = true }).init([_]f32{ 0.1, 0.2, 0.3 })}, 111 + "Pretty array of floats inline - {f}\n", 112 + .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ .inline_arrays = true })}, 113 113 ); 114 114 print( 115 - "Array of floats inline with indices - {f}\n", 116 - .{Pretty([3]f32, .{ 115 + "Pretty array of floats inline with indices - {f}\n", 116 + .{prettyO([3]f32{ 0.1, 0.2, 0.3 }, .{ 117 117 .inline_arrays = true, 118 118 .always_show_index = true, 119 - }).init([_]f32{ 0.1, 0.2, 0.3 })}, 119 + })}, 120 120 ); 121 121 122 122 const eu_error: Result = error.OutOfMemory; 123 123 const eu_ok: Result = .world; 124 124 125 125 print("\nError types\n", .{}); 126 - print("Error set - {f}\n", .{pretty(ErrorSet.OutOfMemory)}); 127 - print("Error union error - {f}\n", .{pretty(eu_error)}); 128 - print("Error union ok - {f}\n", .{pretty(eu_ok)}); 126 + print("Pretty error set - {f}\n", .{pretty(ErrorSet.OutOfMemory)}); 127 + print("Pretty error union error - {f}\n", .{pretty(eu_error)}); 128 + print("Pretty error union ok - {f}\n", .{pretty(eu_ok)}); 129 129 130 130 print("\nFunctions\n", .{}); 131 - print("Function Pretty - {f}\n", .{pretty(Pretty)}); 132 - print("Function main - {f}\n", .{pretty(main)}); 131 + print("Pretty function pretty - {f}\n", .{pretty(pretty)}); 132 + print("Pretty function main - {f}\n", .{pretty(main)}); 133 133 }
+14 -8
pretty.zig
··· 2 2 const Io = std.Io; 3 3 const Type = std.builtin.Type; 4 4 5 + pub fn pretty(value: anytype) Pretty(@TypeOf(value)) { 6 + return Pretty(@TypeOf(value)).init(value); 7 + } 8 + 9 + pub fn prettyO(value: anytype, comptime opts: Options) PrettyWithOptions(@TypeOf(value), opts) { 10 + return PrettyWithOptions(@TypeOf(value), opts).init(value); 11 + } 12 + 5 13 const root = @import("root"); 14 + const default_options: Options = if (@hasDecl(root, "pretty_options")) 15 + root.pretty_options 16 + else 17 + Options{}; 6 18 7 19 pub const Options = struct { 8 20 indent_width: comptime_int = 2, ··· 55 67 } 56 68 }; 57 69 58 - const default_options: Options = if (@hasDecl(root, "pretty_options")) root.pretty_options else .{}; 59 - 60 - pub fn pretty(value: anytype) Pretty(@TypeOf(value)) { 61 - return Pretty(@TypeOf(value)).init(value); 62 - } 63 - 64 70 pub fn Pretty(comptime T: type) type { 65 71 return PrettyWithOptions(T, default_options); 66 72 } ··· 178 184 => formatValue(ctx, run, value), 179 185 180 186 .optional => |opt| formatOptional(opt.child, ctx, run, value), 181 - .@"struct" => |st| formatStruct(T, st, ctx, run, value), 187 + .@"struct" => |st| formatStruct(T, ctx, st, run, value), 182 188 183 189 .error_set => formatErrorSet(ctx, run, value), 184 190 .error_union => formatErrorUnion(ctx, run, value), ··· 254 260 255 261 inline fn formatStruct( 256 262 comptime T: type, 257 - comptime st: Type.Struct, 258 263 comptime ctx: Context, 264 + comptime st: Type.Struct, 259 265 run: *Runtime, 260 266 value: T, 261 267 ) !void {