Something went wrong. Try again.
A charm-like tui library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263//! Mirrors bubbletea's `examples/textinput`. Asks for a favourite Pokémon//! and echoes it; enter/esc/ctrl-c quits. Uses the virtual cursor.const std = @import("std");const matcha = @import("blacktea");const leaves = @import("buds");
const Writer = std.Io.Writer;
const State = struct { model: matcha.Model = .{ .vtable = &.{ .init = init, .update = update, .view = view, } }, text_input: leaves.TextInput = .{ .placeholder = "Pikachu", .char_limit = 156, .width = 20, }, quitting: bool = false,
fn init(m: *matcha.Model) ?matcha.Cmd { const self: *State = @fieldParentPtr("model", m); return self.text_input.focus(m.gpa); }
fn update(m: *matcha.Model, msg: matcha.Msg) ?matcha.Cmd { const self: *State = @fieldParentPtr("model", m); switch (msg) { .key_press => |k| switch (k.kind) { .enter, .escape => { self.quitting = true; return .quit; }, .rune => if (k.rune == 'c' and k.mods.ctrl) { self.quitting = true; return .quit; }, else => {}, }, .interrupt => return .quit, else => {}, } return self.text_input.update(m.gpa, msg); }
fn view(m: *matcha.Model, w: *Writer) Writer.Error!matcha.View { const self: *State = @fieldParentPtr("model", m); try w.writeAll("What's your favorite Pokémon?\n"); try self.text_input.view(w); try w.writeAll("\n\n(esc to quit)\n"); return .{}; }};
pub fn input(init: std.process.Init) !void { var m: State = .{}; defer m.text_input.deinit(init.gpa); var program: matcha.Program = try .init(init, &m.model, .{}); defer program.deinit(); try program.run();}