feat(debug): render debug support

This commit is contained in:
2025-05-26 15:43:47 +02:00
parent 2572b57697
commit c6d8eec287
3 changed files with 77 additions and 145 deletions

177
build.zig
View File

@@ -22,9 +22,13 @@ pub fn build(b: *std.Build) void {
};
const example = b.option(Examples, "example", "Example to build and/or run. (default: all)") orelse .all;
const debug_rendering = b.option(bool, "debug", "Enable debug rendering. Highlight origin's, size's, padding's, gap's, etc. (default: false)") orelse false;
// NOTE do not support debug rendering in release builds
if (debug_rendering == true and optimize != .Debug) @panic("Cannot enable debug rendering in non-debug builds.");
const options = b.addOptions();
options.addOption(Examples, "example", example);
options.addOption(bool, "debug", debug_rendering);
const options_module = options.createModule();
// dependencies
const zg = b.dependency("zg", .{
@@ -39,146 +43,40 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
lib.addImport("code_point", zg.module("code_point"));
lib.addImport("build_options", options_module);
//--- Examples ---
// demo:
const demo = b.addExecutable(.{
.name = "demo",
.root_source_file = b.path("examples/demo.zig"),
.target = target,
.optimize = optimize,
});
demo.root_module.addImport("zterm", lib);
// elements:
const button = b.addExecutable(.{
.name = "button",
.root_source_file = b.path("examples/elements/button.zig"),
.target = target,
.optimize = optimize,
});
button.root_module.addImport("zterm", lib);
const input = b.addExecutable(.{
.name = "input",
.root_source_file = b.path("examples/elements/input.zig"),
.target = target,
.optimize = optimize,
});
input.root_module.addImport("zterm", lib);
const scrollable = b.addExecutable(.{
.name = "scrollable",
.root_source_file = b.path("examples/elements/scrollable.zig"),
.target = target,
.optimize = optimize,
});
scrollable.root_module.addImport("zterm", lib);
// layouts:
const vertical = b.addExecutable(.{
.name = "vertical",
.root_source_file = b.path("examples/layouts/vertical.zig"),
.target = target,
.optimize = optimize,
});
vertical.root_module.addImport("zterm", lib);
const horizontal = b.addExecutable(.{
.name = "horizontal",
.root_source_file = b.path("examples/layouts/horizontal.zig"),
.target = target,
.optimize = optimize,
});
horizontal.root_module.addImport("zterm", lib);
const grid = b.addExecutable(.{
.name = "grid",
.root_source_file = b.path("examples/layouts/grid.zig"),
.target = target,
.optimize = optimize,
});
grid.root_module.addImport("zterm", lib);
const mixed = b.addExecutable(.{
.name = "mixed",
.root_source_file = b.path("examples/layouts/mixed.zig"),
.target = target,
.optimize = optimize,
});
mixed.root_module.addImport("zterm", lib);
// styles:
const palette = b.addExecutable(.{
.name = "palette",
.root_source_file = b.path("examples/styles/palette.zig"),
.target = target,
.optimize = optimize,
});
palette.root_module.addImport("zterm", lib);
const text = b.addExecutable(.{
.name = "text",
.root_source_file = b.path("examples/styles/text.zig"),
.target = target,
.optimize = optimize,
});
text.root_module.addImport("zterm", lib);
// error handling:
const errors = b.addExecutable(.{
.name = "errors",
.root_source_file = b.path("examples/errors.zig"),
.target = target,
.optimize = optimize,
});
errors.root_module.addImport("zterm", lib);
// mapping of user selected example to compile step
const exe = switch (example) {
.demo => demo,
// elements:
.button => button,
.input => input,
.scrollable => scrollable,
// layouts:
.vertical => vertical,
.horizontal => horizontal,
.grid => grid,
.mixed => mixed,
// styles:
.text => text,
.palette => palette,
// error handling:
.errors => errors,
else => blk: {
b.installArtifact(button);
b.installArtifact(input);
b.installArtifact(scrollable);
b.installArtifact(vertical);
b.installArtifact(horizontal);
b.installArtifact(grid);
b.installArtifact(mixed);
b.installArtifact(text);
b.installArtifact(palette);
b.installArtifact(errors);
break :blk demo;
},
};
b.installArtifact(exe);
// zig build run
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
// Allow additional arguments, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| run_cmd.addArgs(args);
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const examples = std.meta.fields(Examples);
inline for (examples) |e| {
if (@as(Examples, @enumFromInt(e.value)) == .all) continue; // skip `.all` entry
const demo = b.addExecutable(.{
.name = e.name,
.root_source_file = b.path(switch (@as(Examples, @enumFromInt(e.value))) {
.demo => "examples/demo.zig",
// elements:
.button => "examples/elements/button.zig",
.input => "examples/elements/input.zig",
.scrollable => "examples/elements/scrollable.zig",
// layouts:
.vertical => "examples/layouts/vertical.zig",
.horizontal => "examples/layouts/horizontal.zig",
.grid => "examples/layouts/grid.zig",
.mixed => "examples/layouts/mixed.zig",
// styles:
.text => "examples/styles/text.zig",
.palette => "examples/styles/palette.zig",
// error handling
.errors => "examples/errors.zig",
.all => unreachable, // should never happen
}),
.target = target,
.optimize = optimize,
});
// import dependencies
demo.root_module.addImport("zterm", lib);
// mapping of user selected example to compile step
if (@intFromEnum(example) == e.value or example == .all) b.installArtifact(demo);
}
// zig build test
const lib_unit_tests = b.addTest(.{
@@ -188,6 +86,7 @@ pub fn build(b: *std.Build) void {
});
lib_unit_tests.root_module.addImport("code_point", zg.module("code_point"));
lib_unit_tests.root_module.addImport("DisplayWidth", zg.module("DisplayWidth"));
lib_unit_tests.root_module.addImport("build_options", options_module);
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);