A pretty printer for zig
zig

add build options to the example

altagos.dev e9c238f9 57b360a5

verified
+17 -5
+9
build.zig
··· 10 10 .optimize = optimize, 11 11 }); 12 12 13 + const options = b.addOptions(); 14 + 15 + const indent_width = b.option(u32, "indent-width", "Set indent width") orelse 2; 16 + options.addOption(u32, "indent_width", indent_width); 17 + 18 + const skip_root_type_name = b.option(bool, "skip-root-type-name", "Don't show the type name of the first element") orelse false; 19 + options.addOption(bool, "skip_root_type_name", skip_root_type_name); 20 + 13 21 const example = b.addExecutable(.{ 14 22 .root_module = b.createModule(.{ 15 23 .root_source_file = b.path("example/main.zig"), ··· 17 25 .optimize = optimize, 18 26 .imports = &.{ 19 27 .{ .name = "pretty", .module = pretty }, 28 + .{ .name = "build-options", .module = options.createModule() }, 20 29 }, 21 30 }), 22 31 .name = "pretty-example",
+6 -3
example/main.zig
··· 1 1 const std = @import("std"); 2 2 const print = std.debug.print; 3 3 4 + const build_options = @import("build-options"); 5 + 4 6 const pretty_mod = @import("pretty"); 5 7 const pretty = pretty_mod.pretty; 6 8 const prettyO = pretty_mod.prettyO; 7 9 8 10 pub const pretty_options = pretty_mod.Options{ 9 - .always_show_type_names = true, 11 + .skip_root_type_name = build_options.skip_root_type_name, 12 + .indent_width = build_options.indent_width, 10 13 }; 11 14 12 15 const employees = @embedFile("employees.json"); ··· 104 107 print("Pretty bool true - {f}\n", .{pretty(true)}); 105 108 print("Pretty bool false - {f}\n", .{pretty(false)}); 106 109 print( 107 - "Pretty bool true always with type name - {f}\n", 110 + "Pretty bool true without type name - {f}\n", 108 111 .{prettyO(true, .{ 109 - .always_show_type_names = true, 112 + .skip_root_type_name = true, 110 113 .theme = .{ .type_value_sep = ": " }, 111 114 })}, 112 115 );
+2 -2
pretty.zig
··· 33 33 highlight_union_tag: bool = true, 34 34 35 35 show_type_names: bool = true, 36 - always_show_type_names: bool = false, 36 + skip_root_type_name: bool = false, 37 37 38 38 theme: Theme = .{}, 39 39 }; ··· 250 250 else => false, 251 251 }; 252 252 253 - if ((ctx.depth != 0 or ctx.options.always_show_type_names) and !excluded) { 253 + if ((ctx.depth != 0 or !ctx.options.skip_root_type_name) and !excluded) { 254 254 run.setColor(ctx, .dim); 255 255 256 256 if (ctx.options.show_type_names) {