Charm-like tui library
hex README.md
4.9 kB · 160 lines
Markdown
at main

HEX #

A collection of terminal libraries.

Components are styled after charm's bubbles as I actually like those. I just didn't like go.

zig fetch --save git+https://git.zig.build/hex
// build.zig
const std = @import("std");

pub fn build(b: *std.Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const hex = b.dependency("hex", .{ .target = target });
    const tea = hex.module("blacktea");
    const matte = hex.module("matte");
    const buds = hex.module("buds");
    const zli = hex.module("zli");
}

ZLI #

This is designed as a quick and dirty cli module.

// main.zig
const std = @import("std");
const zli = @import("zli");

pub fn main(init: std.process.Init) !void {
    try zli.run(
        init,
        // Config, all values optional
        .{
            .description = "This text describes the application",
            .styles = .{
                .bin = .new(.{.foreground = .blue}),
                .description = .new(.{.foreground = .light_black}),
            },
        },
        &.{
            .group("items", "description for this group", &.{
                .group("more-items", "another description", &.{
                    ...
                }),
                .func(pager, "page a file"), // func name will be used as key.
            }),
        },
    );
}

const Enum = enum { alpha, beta };

fn pager(
    // Handler expects an Init to keep it simple.
    init: std.process.Init,
    file: zli.param.String("file", "file to open", .{}),
    // enum type name will be used as param name
    enumerator: zli.param.Enum(Enum, "description", .{ .optional = .default(.alpha)}),
    flag: zli.param.Flag("some-flag", "idk"),
) !void {
    _ = init;
    const string = file.value; // resolved value from input.
    const raw = file.raw; // raw string input
    const flagged = flag.is_present;
    switch (enumerator.value) {
        .alpha => {},
        .beta => {},
    }
}

zli only requires the minimum amount of characters to be unambiguous for each group/func/enum. Absent any -/-- indicators, parsed input will be treated as positional, so bin i p my.txt is valid for bin items pager --file my.txt. Note any non optional params must come before optional ones, placing an optional param before a mandatory one will result in a compile error.

I suck at keeping docs up to date, so use the functional test in exe/main.zig as a reference.

Basic usage #

// src/root.zig
const std = @import("std");
const tea = @import("blacktea");
const matte = @import("matte");

const Writer = std.Io.Writer;

pub const Demo = struct {
    model: tea.Model = .{
        .vtable = &.{
            .init = init,
            .update = update,
            .view = view,
        },
    },
    cursor: usize = 0,
    items: [3][]const u8 = .{ "buy carrots", "buy celery", "buy kohlrabi" },
    selected: [3]bool = .{ false, false, false },

    fn init(_: *tea.Model) ?tea.Cmd {
        return null;
    }

    fn update(m: *tea.Model, msg: tea.Msg) ?tea.Cmd {
        const self: *Demo = @fieldParentPtr("model", m);
        switch (msg) {
            .key_press => |k| {
                switch (k.kind) {
                    .rune => switch (k.rune) {
                        'q' => return .quit,
                        'j' => self.cursor = @min(self.cursor + 1, self.items.len - 1),
                        'k' => self.cursor -|= 1,
                        else => {},
                    },
                    .down => self.cursor = @min(self.cursor + 1, self.items.len - 1),
                    .up => self.cursor -|= 1,
                    .space, .enter => self.selected[self.cursor] = !self.selected[self.cursor],
                    else => {},
                }
            },
            else => {},
        }
        return null;
    }

    fn view(m: *tea.Model, w: *Writer) Writer.Error!tea.View {
        const self: *Demo = @fieldParentPtr("model", m);
        try w.writeAll("what do we need from the market?\n\n");
        for (self.items, 0..) |item, i| {
            const cursor: u8 = if (i == self.cursor) '>' else ' ';
            const check: u8 = if (self.selected[i]) 'x' else ' ';
            try w.print("{c} [{c}] {s}\n", .{ cursor, check, item });
        }
        try w.writeAll("\nq quit  k/j or up/down move  space toggle\n");
        return .{ .window_title = "matcha demo" };
    }
};
test {
    std.testing.refAllDecls(@This());
}

// src/main.zig
const std = @import("std");
const matte = @import("matte");
const tea = @import("blacktea");

const Writer = std.Io.Writer;
const Demo = @import("lib").Demo;

pub fn main(init: std.process.Init) !void {
    const gpa = init.gpa;
    var demo: Demo = .{};
    var program: tea.Program = try .init(gpa, init.io, &demo.model, .{});
    defer program.deinit();
    try program.run();
}