From ddf2de739068b5ff0866ccb1d067f3cb53a4fc55 Mon Sep 17 00:00:00 2001 From: David Czihak Date: Thu, 7 May 2026 14:33:19 +0200 Subject: Initial commit --- examples/showcase.zig | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 examples/showcase.zig (limited to 'examples/showcase.zig') 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); +} -- cgit v1.3