Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768//! Paginator demo: navigate 100 dummy items in pages of 10. h/l (and//! arrow keys / pgup/pgdn) move between pages; q quits.const std = @import("std");const matcha = @import("blacktea");const leaves = @import("buds");
const Writer = std.Io.Writer;
const item_count: u16 = 100;const per_page: u16 = 10;
const Model = struct { model: matcha.Model = .{ .vtable = &.{ .init = init, .update = update, .view = view, } }, paginator: leaves.Paginator = .{ .type = .dots, .per_page = per_page, .total_pages = (item_count + per_page - 1) / per_page, },
fn init(_: *matcha.Model) ?matcha.Cmd { return null; }
fn update(m: *matcha.Model, msg: matcha.Msg) ?matcha.Cmd { const self: *Model = @fieldParentPtr("model", m); switch (msg) { .key_press => |k| switch (k.kind) { .escape => return .quit, .rune => switch (k.codepoint) { 'q' => return .quit, 'c' => if (k.mods.ctrl) return .quit, else => {}, }, else => {}, }, else => {}, } return self.paginator.update(msg); }
fn view(m: *matcha.Model, w: *Writer) Writer.Error!matcha.View { const self: *Model = @fieldParentPtr("model", m); try w.writeAll("\n Paginator Example\n\n");
const bounds = self.paginator.sliceBounds(item_count); var i: u16 = bounds.start; while (i < bounds.end) : (i += 1) { try w.print(" • Item {d}\n\n", .{i + 1}); }
try w.writeAll(" "); try self.paginator.view(w); try w.writeAll("\n\n h/l ←/→ page • q: quit\n"); return .{}; }};
pub fn paginator(init: std.process.Init) !void { var m: Model = .{}; var program: matcha.Program = try .init(init, &m.model, .{}); defer program.deinit(); try program.run();}