From e9c238f9efef2b12a163a4f901bfaf6dbf9b0f16 Mon Sep 17 00:00:00 2001 From: Altagos Date: Sun, 25 Jan 2026 13:47:41 +0100 Subject: [PATCH] add build options to the example --- build.zig | 9 +++++++++ example/main.zig | 9 ++++++--- pretty.zig | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index a06035e..45beeda 100644 --- a/build.zig +++ b/build.zig @@ -10,6 +10,14 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + const options = b.addOptions(); + + const indent_width = b.option(u32, "indent-width", "Set indent width") orelse 2; + options.addOption(u32, "indent_width", indent_width); + + const skip_root_type_name = b.option(bool, "skip-root-type-name", "Don't show the type name of the first element") orelse false; + options.addOption(bool, "skip_root_type_name", skip_root_type_name); + const example = b.addExecutable(.{ .root_module = b.createModule(.{ .root_source_file = b.path("example/main.zig"), @@ -17,6 +25,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .imports = &.{ .{ .name = "pretty", .module = pretty }, + .{ .name = "build-options", .module = options.createModule() }, }, }), .name = "pretty-example", diff --git a/example/main.zig b/example/main.zig index 72014a8..cd04c74 100644 --- a/example/main.zig +++ b/example/main.zig @@ -1,12 +1,15 @@ const std = @import("std"); const print = std.debug.print; +const build_options = @import("build-options"); + const pretty_mod = @import("pretty"); const pretty = pretty_mod.pretty; const prettyO = pretty_mod.prettyO; pub const pretty_options = pretty_mod.Options{ - .always_show_type_names = true, + .skip_root_type_name = build_options.skip_root_type_name, + .indent_width = build_options.indent_width, }; const employees = @embedFile("employees.json"); @@ -104,9 +107,9 @@ pub fn main(init: std.process.Init) !void { print("Pretty bool true - {f}\n", .{pretty(true)}); print("Pretty bool false - {f}\n", .{pretty(false)}); print( - "Pretty bool true always with type name - {f}\n", + "Pretty bool true without type name - {f}\n", .{prettyO(true, .{ - .always_show_type_names = true, + .skip_root_type_name = true, .theme = .{ .type_value_sep = ": " }, })}, ); diff --git a/pretty.zig b/pretty.zig index b61114c..dd3ccb7 100644 --- a/pretty.zig +++ b/pretty.zig @@ -33,7 +33,7 @@ pub const Options = struct { highlight_union_tag: bool = true, show_type_names: bool = true, - always_show_type_names: bool = false, + skip_root_type_name: bool = false, theme: Theme = .{}, }; @@ -250,7 +250,7 @@ inline fn printType( else => false, }; - if ((ctx.depth != 0 or ctx.options.always_show_type_names) and !excluded) { + if ((ctx.depth != 0 or !ctx.options.skip_root_type_name) and !excluded) { run.setColor(ctx, .dim); if (ctx.options.show_type_names) { -- 2.51.2