aboutsummaryrefslogtreecommitdiff
path: root/examples/showcase.zig
diff options
context:
space:
mode:
authorDavid Czihak <git@dcz.at>2026-05-07 14:33:19 +0200
committerDavid Czihak <git@dcz.at>2026-05-07 14:33:19 +0200
commitddf2de739068b5ff0866ccb1d067f3cb53a4fc55 (patch)
tree1a77efe9d73a6172be3c37d29b321eadd4efe379 /examples/showcase.zig
Initial commitv0.1.7
Diffstat (limited to 'examples/showcase.zig')
-rw-r--r--examples/showcase.zig80
1 files changed, 80 insertions, 0 deletions
diff --git a/examples/showcase.zig b/examples/showcase.zig
new file mode 100644
index 0000000..54b195f
--- /dev/null
+++ b/examples/showcase.zig
@@ -0,0 +1,80 @@
+const std = @import("std");
+
+pub const AppError = error{
+ MissingName,
+ EmptyInput,
+};
+
+pub const Theme = enum {
+ light,
+ dark,
+ solarized,
+};
+
+pub const Settings = struct {
+ name: []const u8,
+ retries: u8 = 3,
+ theme: Theme = .dark,
+ tags: []const []const u8 = &.{},
+
+ pub fn validate(self: Settings) AppError!void {
+ if (self.name.len == 0) return error.MissingName;
+ if (self.tags.len == 0) return error.EmptyInput;
+ }
+};
+
+pub fn summarize(settings: Settings) []const u8 {
+ return switch (settings.theme) {
+ .light => "bright",
+ .dark => "focused",
+ .solarized => "balanced",
+ };
+}
+
+pub fn findLongestTag(tags: []const []const u8) ?[]const u8 {
+ var longest: ?[]const u8 = null;
+
+ for (tags) |tag| {
+ if (longest == null or tag.len > longest.?.len) {
+ longest = tag;
+ }
+ }
+
+ return longest;
+}
+
+pub fn renderExample(writer: anytype) !void {
+ const settings = Settings{
+ .name = "Nova Zig",
+ .tags = &.{ "tree-sitter", "zls", "tasks" },
+ };
+
+ try settings.validate();
+ try writer.print("theme summary: {s}\n", .{summarize(settings)});
+
+ if (findLongestTag(settings.tags)) |tag| {
+ try writer.print("longest tag: {s}\n", .{tag});
+ }
+
+ const block_value = label: {
+ var total: usize = 0;
+ for (settings.tags) |tag| total += tag.len;
+ break :label total;
+ };
+ try writer.print("combined tag length: {}\n", .{block_value});
+
+ const multiline =
+ \\sample text block
+ \\with enough lines to make folding obvious
+ \\inside Nova's editor
+ ;
+ _ = multiline;
+}
+
+test "showcase remains valid" {
+ var output: std.ArrayList(u8) = .empty;
+ defer output.deinit(std.testing.allocator);
+
+ try renderExample(output.writer(std.testing.allocator));
+ try std.testing.expect(std.mem.indexOf(u8, output.items, "theme summary") != null);
+}