aboutsummaryrefslogtreecommitdiff
path: root/examples/showcase.zig
diff options
context:
space:
mode:
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 @@
1const std = @import("std");
2
3pub const AppError = error{
4 MissingName,
5 EmptyInput,
6};
7
8pub const Theme = enum {
9 light,
10 dark,
11 solarized,
12};
13
14pub const Settings = struct {
15 name: []const u8,
16 retries: u8 = 3,
17 theme: Theme = .dark,
18 tags: []const []const u8 = &.{},
19
20 pub fn validate(self: Settings) AppError!void {
21 if (self.name.len == 0) return error.MissingName;
22 if (self.tags.len == 0) return error.EmptyInput;
23 }
24};
25
26pub fn summarize(settings: Settings) []const u8 {
27 return switch (settings.theme) {
28 .light => "bright",
29 .dark => "focused",
30 .solarized => "balanced",
31 };
32}
33
34pub fn findLongestTag(tags: []const []const u8) ?[]const u8 {
35 var longest: ?[]const u8 = null;
36
37 for (tags) |tag| {
38 if (longest == null or tag.len > longest.?.len) {
39 longest = tag;
40 }
41 }
42
43 return longest;
44}
45
46pub fn renderExample(writer: anytype) !void {
47 const settings = Settings{
48 .name = "Nova Zig",
49 .tags = &.{ "tree-sitter", "zls", "tasks" },
50 };
51
52 try settings.validate();
53 try writer.print("theme summary: {s}\n", .{summarize(settings)});
54
55 if (findLongestTag(settings.tags)) |tag| {
56 try writer.print("longest tag: {s}\n", .{tag});
57 }
58
59 const block_value = label: {
60 var total: usize = 0;
61 for (settings.tags) |tag| total += tag.len;
62 break :label total;
63 };
64 try writer.print("combined tag length: {}\n", .{block_value});
65
66 const multiline =
67 \\sample text block
68 \\with enough lines to make folding obvious
69 \\inside Nova's editor
70 ;
71 _ = multiline;
72}
73
74test "showcase remains valid" {
75 var output: std.ArrayList(u8) = .empty;
76 defer output.deinit(std.testing.allocator);
77
78 try renderExample(output.writer(std.testing.allocator));
79 try std.testing.expect(std.mem.indexOf(u8, output.items, "theme summary") != null);
80}