aboutsummaryrefslogtreecommitdiff
path: root/examples/showcase.zig
blob: 54b195fa0c9b34f9da15701a72cad86e75a0e8da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
}