Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768//! 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 tea = @import("blacktea");const buds = @import("buds");
const Writer = std.Io.Writer;const key = buds.key;const Init = std.process.Init;
const KeyMap = struct { exit: key.Binding = .new(&.{ .enter, .escape, .ctrl(.c) }, "quit", .{}),};
const State = struct { model: tea.Model = .{ .vtable = &.{ .init = init, .update = update, .view = view, } }, text_input: buds.TextInput = .{ .placeholder = "Pikachu", .char_limit = 156, .width = 20, }, quitting: bool = false, keymap: KeyMap = .{}, help: buds.Help = .{},
fn init(m: *tea.Model) ?tea.Cmd { const state: *State = @fieldParentPtr("model", m); return state.text_input.focus(m.gpa); }
fn update(m: *tea.Model, msg: tea.Msg) ?tea.Cmd { const state: *State = @fieldParentPtr("model", m); switch (msg) { .key_press => |k| if (state.keymap.exit.matches(k)) { state.quitting = true; return .quit; }, .interrupt => return .quit, else => {}, } return state.text_input.update(m.gpa, msg); }
fn view(m: *tea.Model, writer: *Writer) !tea.View { const state: *State = @fieldParentPtr("model", m); try writer.writeAll("What's your favorite Pokémon?\n"); try state.text_input.view(writer); const bindings = [_]key.Binding{ state.keymap.exit, }; try writer.writeAll("\n\n"); try state.help.shortView(writer, &bindings); return .{}; }};
pub fn textinput(init: Init) !void { var state: State = .{}; defer state.text_input.deinit(init.gpa); var program: tea.Program = try .init(init, &state.model, .{}); defer program.deinit(); try program.run();}