Something went wrong. Try again.
A charm-like tui library
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970//! Mirrors bubbletea's `examples/textarea`. Header, multi-line editor,//! footer with quit hint. esc blurs, any other key re-focuses, ctrl-c quits.const std = @import("std");const matcha = @import("matcha");const leaves = @import("leaves");
const Writer = std.Io.Writer;
const Model = struct { model: matcha.Model = .{ .vtable = &.{ .init = init, .update = update, .view = view, } }, textarea: leaves.Textarea = .{ .placeholder = "Once upon a time...", .width = 50, .height = 6, }, quitting: bool = false,
fn init(m: *matcha.Model) ?matcha.Cmd { const self: *Model = @fieldParentPtr("model", m); return self.textarea.focus(m.gpa); }
fn update(m: *matcha.Model, msg: matcha.Msg) ?matcha.Cmd { const self: *Model = @fieldParentPtr("model", m); switch (msg) { .key_press => |k| { if (k.kind == .escape) { self.textarea.blur(); return null; } if (k.kind == .rune and k.rune == 'c' and k.mods.ctrl) { self.quitting = true; return .quit; } if (!self.textarea.focused) { return self.textarea.focus(m.gpa); } }, .interrupt => return .quit, else => {}, } return self.textarea.update(m.gpa, msg); }
fn view(m: *matcha.Model, w: *Writer) Writer.Error!matcha.View { const self: *Model = @fieldParentPtr("model", m); if (self.quitting) { try w.writeAll("Bye!\n"); return .{}; } try w.writeAll("Tell me a story.\n\n"); try self.textarea.view(w); try w.writeAll("\n\n(ctrl+c to quit)\n"); return .{}; }};
pub fn area(init: std.process.Init) !void { var m: Model = .{}; defer m.textarea.deinit(init.gpa);
var program: matcha.Program = try .init(init, &m.model, .{}); defer program.deinit(); try program.run();}