//! The root widget: transcript viewport, prompt, the rows around the prompt and //! the footer. Also owns the scroll and spinner state that outlives a frame. const std = @import("std"); const vaxis = @import("vaxis"); const vxfw = vaxis.vxfw; const chrome = @import("chrome.zig"); const editors = @import("../editor.zig"); const footer_view = @import("footer.zig"); const prompt_view = @import("prompt.zig"); const protocol = @import("../protocol.zig"); const transcript_view = @import("transcript.zig"); const transcripts = @import("../transcript.zig"); const testing = std.testing; pub const View = struct { io: std.Io, arena: std.heap.ArenaAllocator, list: vxfw.ListView = .{ .children = .{ .slice = &.{} }, .draw_cursor = false }, builder: transcript_view.Builder, cache: transcript_view.Cache, prompt: prompt_view.Prompt, spinner: vxfw.Spinner, top_border: chrome.Border = .{}, working: bool = false, /// True while the view follows new output, i.e. while it sits at the bottom. follow: bool = true, /// Transcript rows drawn last frame; a page is one viewport. viewport: u16 = 1, /// The surface drawn last frame, for hit testing. Valid until the next /// frame resets the arena. last: ?vxfw.Surface = null, /// Prompts that wait to run, one dim row each below the transcript. queued: []const []const u8 = &.{}, model: []const u8 = "", cwd: []const u8 = "", /// How much the model reasons. Null for a model without reasoning. level: ?protocol.core.Level = null, /// The last usage report. Null until a request reported usage. usage: ?protocol.core.UsageReport = null, /// A scroll large enough to run past the last item, so that a last item /// taller than the viewport shows its bottom instead of its top. const follow_rows = std.math.maxInt(i17); const frame_retain_bytes = 1 << 20; pub fn init(gpa: std.mem.Allocator, io: std.Io) View { return .{ .io = io, .arena = .init(gpa), .builder = .{ .transcript = undefined, .arena = gpa }, .cache = .init(gpa), .prompt = .{ .editor = undefined, .rows = 1 }, .spinner = .{ .io = io }, }; } pub fn deinit(self: *View) void { self.cache.deinit(); self.arena.deinit(); } pub fn widget(self: *View) vxfw.Widget { return .{ .userdata = self, .drawFn = typeErasedDrawFn, }; } fn typeErasedDrawFn(ptr: *anyopaque, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const self: *View = @ptrCast(@alignCast(ptr)); return self.draw(ctx); } pub fn draw(self: *View, ctx: vxfw.DrawContext) std.mem.Allocator.Error!vxfw.Surface { const size = ctx.max.size(); // The screen is cleared before each frame, so the root needs no buffer; vxfw sizes buffers with u16. var surface: vxfw.Surface = .{ .size = size, .widget = self.widget(), .buffer = &.{}, .children = &.{} }; if (size.height < 4 or size.width < 2) { return surface; } const prompt_rows: u16 = @intCast(@min(try self.prompt.editor.height(ctx.arena, size.width), size.height - 3)); self.prompt.rows = prompt_rows; const bottom_row = size.height - 2; const prompt_row = bottom_row - prompt_rows; const top_row = prompt_row - 1; const queued_rows: u16 = @intCast(@min(self.queued.len, top_row)); const list_rows = top_row - queued_rows; self.viewport = list_rows; const list = try self.list.draw(ctx.withConstraints( .{ .width = size.width, .height = list_rows }, .{ .width = size.width, .height = list_rows }, )); self.follow = self.atBottom(list, self.builder.transcript.items.items.len, self.list.scroll.top, list_rows); self.top_border.spinner = if (self.working) &self.spinner else null; var children: std.ArrayList(vxfw.SubSurface) = .empty; try children.append(ctx.arena, .{ .origin = .{ .row = 0, .col = 0 }, .surface = list }); for (self.queued[self.queued.len - queued_rows ..], list_rows..) |prompt, row| { const text: *vxfw.Text = try ctx.arena.create(vxfw.Text); text.* = .{ .text = prompt[0 .. std.mem.indexOfScalar(u8, prompt, '\n') orelse prompt.len], .style = .{ .dim = true }, .softwrap = false, .overflow = .clip, .width_basis = .parent, }; try children.append(ctx.arena, .{ .origin = .{ .row = @intCast(row), .col = 0 }, .surface = try text.draw(rowContext(ctx, size.width, 1)), }); } try children.append(ctx.arena, .{ .origin = .{ .row = top_row, .col = 0 }, .surface = try self.top_border.draw(rowContext(ctx, size.width, 1)), }); try children.append(ctx.arena, .{ .origin = .{ .row = prompt_row, .col = 0 }, .surface = try self.prompt.draw(rowContext(ctx, size.width, prompt_rows)), }); try children.append(ctx.arena, .{ .origin = .{ .row = bottom_row, .col = 0 }, .surface = try bottom_border.draw(rowContext(ctx, size.width, 1)), }); try children.append(ctx.arena, .{ .origin = .{ .row = size.height - 1, .col = 0 }, .surface = try self.footer(ctx, size.width), }); surface.children = children.items; return surface; } /// `MODEL • LEVEL · CWD` on the left. On the right the session usage, how full /// the context is, and the interrupt hint. fn footer(self: *View, ctx: vxfw.DrawContext, width: u16) std.mem.Allocator.Error!vxfw.Surface { const gap = " "; var totals: []const u8 = ""; var context: footer_view.Context = .{ .text = "", .style = .{} }; if (self.usage) |report| { totals = try footer_view.totals(ctx.arena, report); if (try footer_view.context(ctx.arena, report)) |shown| { context = shown; } if (totals.len > 0 and context.text.len > 0) { totals = try std.mem.concat(ctx.arena, u8, &.{ totals, " " }); } if (totals.len > 0 or context.text.len > 0) { totals = try std.mem.concat(ctx.arena, u8, &.{ gap, totals }); } } var hint: []const u8 = if (self.working) "esc to interrupt" else ""; // What does not fit is left out, the usage first. A hint that would // take the whole row would hide the text left of it. if (ctx.stringWidth(totals) + ctx.stringWidth(context.text) + gap.len + ctx.stringWidth(hint) >= width) { totals = ""; context.text = ""; } if (ctx.stringWidth(hint) >= width) { hint = ""; } if (hint.len > 0 and (totals.len > 0 or context.text.len > 0)) { hint = try std.mem.concat(ctx.arena, u8, &.{ gap, hint }); } const left = try footerText(ctx, if (self.level) |level| try std.fmt.allocPrint(ctx.arena, "{s} • {t} · {s}", .{ self.model, level, self.cwd }) else try std.fmt.allocPrint(ctx.arena, "{s} · {s}", .{ self.model, self.cwd }), .{}); left.overflow = .clip; left.width_basis = .parent; const items = try ctx.arena.alloc(vxfw.FlexItem, 4); items[0] = .{ .widget = left.widget(), .flex = 1 }; items[1] = .{ .widget = (try footerText(ctx, totals, .{})).widget(), .flex = 0 }; items[2] = .{ .widget = (try footerText(ctx, context.text, context.style)).widget(), .flex = 0 }; items[3] = .{ .widget = (try footerText(ctx, hint, .{})).widget(), .flex = 0 }; const row: *vxfw.FlexRow = try ctx.arena.create(vxfw.FlexRow); row.* = .{ .children = items }; return row.draw(rowContext(ctx, width, 1)); } fn footerText(ctx: vxfw.DrawContext, text: []const u8, style: vaxis.Style) std.mem.Allocator.Error!*vxfw.Text { const label: *vxfw.Text = try ctx.arena.create(vxfw.Text); label.* = .{ .text = text, .style = style, .softwrap = false, .width_basis = .longest_line }; return label; } /// True when the last transcript row is on screen, so new output stays visible. fn atBottom(self: *View, list: vxfw.Surface, count: usize, top: u32, viewport: u16) bool { // A window this short draws no transcript; keep the position it had. if (viewport == 0) { return self.follow; } if (list.children.len == 0) { return true; } const last = list.children[list.children.len - 1]; const index = @as(usize, top) + list.children.len - 1; return index + 1 == count and last.origin.row + @as(i17, @intCast(last.surface.size.height)) <= viewport; } /// Draws one frame of `transcript` and `editor` into the window. A click /// on the frame can change the transcript. pub fn render(self: *View, win: vaxis.Window, transcript: *transcripts.Transcript, editor: *editors.Editor) !void { self.last = null; self.builder.transcript = transcript; self.prompt.editor = editor; _ = self.arena.reset(.{ .retain_with_limit = frame_retain_bytes }); const arena = self.arena.allocator(); self.builder.arena = arena; self.builder.cache = &self.cache; self.list.item_count = @intCast(self.builder.transcript.items.items.len); self.list.children = self.builder.source(); if (self.follow) { self.list.scrollToBottom(); self.list.scroll.pending_lines = follow_rows; } const screen = win.screen; vxfw.DrawContext.init(screen.width_method); self.prompt.editor.width_method = screen.width_method; self.cache.width_method = screen.width_method; const surface = try self.widget().draw(.{ .arena = arena, .min = .{}, .max = .{ .width = win.width, .height = win.height }, .cell_size = .{ .width = if (screen.width == 0) 0 else screen.width_pix / screen.width, .height = if (screen.height == 0) 0 else screen.height_pix / screen.height, }, }); self.cache.sweep(); win.clear(); win.hideCursor(); surface.render(win, self.prompt.widget()); self.last = surface; } /// Appends the widgets under a point from the last frame, innermost last. pub fn hitTest(self: *View, gpa: std.mem.Allocator, point: vxfw.Point, hits: *std.ArrayList(vxfw.HitResult)) !void { const surface = self.last orelse return; if (point.row >= surface.size.height or point.col >= surface.size.width) { return; } try surface.hitTest(gpa, hits, point); } /// Starts or stops the spinner and returns the tick it wants next, so the /// caller can arm its timer with the widget that armed it. pub fn setWorking(self: *View, working: bool) ?vxfw.Tick { if (self.working == working) { return null; } self.working = working; if (working) { const command = self.spinner.start() orelse return null; return command.tick; } self.spinner.stop(); return null; } pub fn scrollUp(self: *View, rows: u16) void { self.list.scroll.pending_lines -|= @intCast(rows); self.follow = false; } pub fn scrollDown(self: *View, rows: u16) void { self.list.scroll.pending_lines +|= @intCast(rows); } pub fn pageUp(self: *View) void { self.scrollUp(self.viewport); } pub fn pageDown(self: *View) void { self.scrollDown(self.viewport); } /// Jumps to the newest output and keeps following it. pub fn toBottom(self: *View) void { self.follow = true; } }; fn rowContext(ctx: vxfw.DrawContext, width: u16, height: u16) vxfw.DrawContext { return ctx.withConstraints( .{ .width = width, .height = height }, .{ .width = width, .height = height }, ); } const bottom_border: chrome.Border = .{}; const Fixture = @import("testing.zig").Fixture; const content = @import("../content.zig"); const Draw = struct { view: View, fixture: Fixture, transcript: transcripts.Transcript, editor: editors.Editor, fn init(self: *Draw, rows: u16, cols: u16) !void { self.transcript = transcripts.Transcript.init(testing.allocator); self.editor = editors.Editor.init(testing.allocator); self.view = .init(testing.allocator, testing.io); self.view.model = "fake"; self.view.cwd = "/tmp"; try self.fixture.init(rows, cols); } fn deinit(self: *Draw) void { self.view.deinit(); self.transcript.deinit(); self.editor.deinit(); self.fixture.deinit(); } fn redraw(self: *Draw) !void { try self.view.render(self.fixture.win, &self.transcript, &self.editor); } fn user(self: *Draw, text: []const u8) void { const copy = testing.allocator.dupe(u8, text) catch unreachable; self.transcript.items.append(testing.allocator, .{ .user = copy }) catch unreachable; } fn assistant(self: *Draw, text: []const u8) void { const copy = testing.allocator.dupe(u8, text) catch unreachable; self.transcript.items.append(testing.allocator, .{ .assistant = .fromOwnedSlice(copy) }) catch unreachable; } fn thinking(self: *Draw, text: []const u8) void { const copy = testing.allocator.dupe(u8, text) catch unreachable; self.transcript.items.append(testing.allocator, .{ .thinking = .fromOwnedSlice(copy) }) catch unreachable; } fn tool(self: *Draw, name: []const u8, params: []const u8, state: transcripts.State, collapsed: []const u8, expanded: []const u8, open: bool) void { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const call = content.callLine(arena_state.allocator(), name, params) catch unreachable; const item: transcripts.ToolResult = .{ .id = testing.allocator.dupe(u8, "call") catch unreachable, .state = state, .call = content.Block.init(testing.allocator, call) catch unreachable, .collapsed = content.Block.init(testing.allocator, &.{.{ .text = collapsed }}) catch unreachable, .expanded = content.Block.init(testing.allocator, &.{.{ .text = expanded }}) catch unreachable, .open = open, }; self.transcript.items.append(testing.allocator, .{ .tool = item }) catch unreachable; } fn notice(self: *Draw, kind: @import("../protocol.zig").core.Notice.Kind, text: []const u8) void { const copy = testing.allocator.dupe(u8, text) catch unreachable; self.transcript.items.append(testing.allocator, .{ .notice = .{ .kind = kind, .text = copy } }) catch unreachable; } }; test "a user message fills the row with palette color 8" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 20); defer draw_state.deinit(); draw_state.user("hi"); try draw_state.redraw(); // rows: 0 blank padding, 1 text, 2 blank padding, then footer rows try testing.expectEqual(@as(u8, 8), draw_state.fixture.style(0, 0).bg.index); try testing.expectEqual(@as(u8, 8), draw_state.fixture.style(19, 2).bg.index); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(1, arena), " hi")); } test "assistant text is plain and thinking is italic" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 20); defer draw_state.deinit(); draw_state.assistant("pong"); draw_state.thinking("hm"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(0, arena), "pong")); try testing.expect(!draw_state.fixture.style(0, 0).bold); try testing.expectEqual(vaxis.Color.default, draw_state.fixture.style(0, 0).bg); // the thinking item starts after a separator row try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(2, arena), "hm")); try testing.expect(draw_state.fixture.style(0, 2).italic); } test "tool blocks use the state color and a bold name" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(24, 30); defer draw_state.deinit(); draw_state.tool("read", "a.txt", .running, "", "", false); draw_state.tool("read", "b.txt", .ok, "1 file(s), 2 lines", "", false); draw_state.tool("bash", "false (.)", .failed, "boom", "boom", false); try draw_state.redraw(); try testing.expectEqual(@as(u8, 8), draw_state.fixture.style(0, 1).bg.index); try testing.expectEqual(@as(u8, 2), draw_state.fixture.style(0, 5).bg.index); try testing.expectEqual(@as(u8, 1), draw_state.fixture.style(0, 9).bg.index); try testing.expect(draw_state.fixture.style(1, 1).bold); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(1, arena), " read a.txt")); } test "a click on a tool block expands only that block" { var draw_state: Draw = undefined; try draw_state.init(20, 40); defer draw_state.deinit(); const args = "echo hi (.)"; const output = "hi\n[exit code 0, elapsed 1.20s]"; draw_state.tool("bash", args, .ok, "exit 0, 1.20s", output, false); draw_state.tool("bash", args, .ok, "exit 0, 1.20s", output, false); // Nothing was drawn yet, so there is nothing to hit. var hits: std.ArrayList(vxfw.HitResult) = .empty; defer hits.deinit(testing.allocator); try draw_state.view.hitTest(testing.allocator, .{ .row = 1, .col = 2 }, &hits); try testing.expectEqual(@as(usize, 0), hits.items.len); try draw_state.redraw(); try draw_state.view.hitTest(testing.allocator, .{ .row = 1, .col = 2 }, &hits); try testing.expect(hits.items.len > 0); const target = hits.items[hits.items.len - 1]; var ctx: vxfw.EventContext = .{ .io = testing.io, .alloc = testing.allocator, .phase = .at_target, .cmds = .empty, }; defer ctx.cmds.deinit(testing.allocator); try target.widget.handleEvent(&ctx, .{ .mouse = .{ .row = 1, .col = 2, .button = .left, .mods = .{}, .type = .press, } }); try testing.expect(ctx.redraw); try testing.expect(draw_state.transcript.items.items[0].tool.open); try testing.expect(!draw_state.transcript.items.items[1].tool.open); // A right press or a release must not toggle. try target.widget.handleEvent(&ctx, .{ .mouse = .{ .row = 1, .col = 2, .button = .right, .mods = .{}, .type = .press } }); try target.widget.handleEvent(&ctx, .{ .mouse = .{ .row = 1, .col = 2, .button = .left, .mods = .{}, .type = .release } }); try testing.expect(draw_state.transcript.items.items[0].tool.open); } test "a drawn item shows its changes in the next frame" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 40); defer draw_state.deinit(); draw_state.tool("bash", "echo hi (.)", .ok, "exit 0, 1.23s", "hi", false); draw_state.assistant("po"); try draw_state.redraw(); draw_state.transcript.toggleItem(0); try draw_state.transcript.items.items[1].assistant.appendSlice(testing.allocator, "ng"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(2, arena), " hi ")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(5, arena), "pong")); // A second result for the same call, with text of the same length. draw_state.transcript.apply(.{ .tool_result = .{ .call_id = try testing.allocator.dupe(u8, "call"), .is_error = false, .collapsed = try .init(testing.allocator, &.{.{ .text = "exit 0, 4.56s" }}), .expanded = try .init(testing.allocator, &.{.{ .text = "ho" }}), } }); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(2, arena), " ho ")); } test "a collapsed call shows its collapsed result and hides the output" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 40); defer draw_state.deinit(); draw_state.tool("bash", "echo hi (.)", .ok, "exit 0, 1.23s", "hi\n[exit code 0, elapsed 1.23s]", false); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(1, arena), " bash echo hi (.)")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(2, arena), " exit 0, 1.23s")); try testing.expect(std.mem.indexOf(u8, try draw_state.fixture.row(3, arena), "hi") == null); } test "notices are red for errors and yellow when interrupted" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 20); defer draw_state.deinit(); draw_state.notice(.err, "bad"); draw_state.notice(.interrupted, "stop"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(0, arena), "bad")); try testing.expectEqual(@as(u8, 1), draw_state.fixture.style(0, 0).fg.index); try testing.expectEqual(@as(u8, 3), draw_state.fixture.style(0, 2).fg.index); } test "text wraps by display width and wide characters take two cells" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 10); defer draw_state.deinit(); draw_state.assistant("\u{4f60}\u{597d}\u{4e16}\u{754c}\u{4f60}\u{597d}"); try draw_state.redraw(); const first = try draw_state.fixture.row(0, arena); try testing.expect(std.mem.startsWith(u8, first, "\u{4f60}")); try testing.expectEqual(@as(u8, 2), draw_state.fixture.win.readCell(0, 0).?.char.width); // five wide glyphs per ten column row const second = try draw_state.fixture.row(1, arena); try testing.expect(std.mem.startsWith(u8, second, "\u{597d}")); } test "the status rows show borders, the spinner and the footer" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(10, 40); defer draw_state.deinit(); draw_state.editor.insert("hi") catch unreachable; _ = draw_state.view.setWorking(true); try draw_state.redraw(); // footer, prompt bottom border, prompt row, prompt top border try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(9, arena), "fake · /tmp")); try testing.expect(std.mem.endsWith(u8, try draw_state.fixture.row(9, arena), "esc to interrupt")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(8, arena), "────")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(7, arena), "hi")); const top = try draw_state.fixture.row(6, arena); try testing.expect(std.mem.startsWith(u8, top, "─ ⣶ Working")); try testing.expect(std.mem.endsWith(u8, top, "────")); } const usage_report: protocol.core.UsageReport = .{ .request = .{}, .request_cost = null, .session = .{ .input = 12_000, .output = 1_200, .cache_read = 8_300 }, .session_cost = 0.0123, .context_tokens = 45_000, .context_window = 1_048_576, }; test "the footer shows the session usage, left of the interrupt hint" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(10, 80); defer draw_state.deinit(); try draw_state.redraw(); try testing.expectEqualStrings("fake · /tmp", std.mem.trimEnd(u8, try draw_state.fixture.row(9, arena), " ")); draw_state.view.usage = usage_report; try draw_state.redraw(); const idle = try draw_state.fixture.row(9, arena); try testing.expect(std.mem.startsWith(u8, idle, "fake · /tmp ")); try testing.expect(std.mem.endsWith(u8, idle, " ↑12k ↓1.2k R8.3k $0.012 4.3%/1M")); _ = draw_state.view.setWorking(true); try draw_state.redraw(); try testing.expect(std.mem.endsWith(u8, try draw_state.fixture.row(9, arena), " ↑12k ↓1.2k R8.3k $0.012 4.3%/1M esc to interrupt")); } test "the footer shows the reasoning level after the model, and none for a model without reasoning" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(10, 60); defer draw_state.deinit(); draw_state.view.level = .high; try draw_state.redraw(); try testing.expectEqualStrings("fake • high · /tmp", std.mem.trimEnd(u8, try draw_state.fixture.row(9, arena), " ")); draw_state.view.level = null; try draw_state.redraw(); try testing.expectEqualStrings("fake · /tmp", std.mem.trimEnd(u8, try draw_state.fixture.row(9, arena), " ")); } test "the footer colors a context that is nearly full" { var draw_state: Draw = undefined; try draw_state.init(10, 80); defer draw_state.deinit(); draw_state.view.usage = usage_report; draw_state.view.usage.?.context_tokens = 1_000_000; try draw_state.redraw(); // The percent ends the row. The cost before it keeps the default color. try testing.expectEqual(vaxis.Color{ .index = 1 }, draw_state.fixture.style(79, 9).fg); try testing.expectEqual(vaxis.Color.default, draw_state.fixture.style(79 - "95.3%/1M ".len, 9).fg); } test "a footer too narrow for the usage leaves it out and keeps the hint" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(10, 30); defer draw_state.deinit(); draw_state.view.usage = usage_report; _ = draw_state.view.setWorking(true); try draw_state.redraw(); const row = try draw_state.fixture.row(9, arena); try testing.expect(std.mem.indexOf(u8, row, "↑") == null); try testing.expect(std.mem.endsWith(u8, row, "esc to interrupt")); } test "an expanded tool shows every line of its output" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(20, 20); defer draw_state.deinit(); var output: std.ArrayList(u8) = .empty; for (0..5) |line| { try output.print(arena, "line {d}\n", .{line}); } draw_state.tool("bash", "seq 5 (.)", .ok, "exit 0, 0.01s", output.items, true); try draw_state.redraw(); var seen: usize = 0; var row: u16 = 0; while (row < 20) : (row += 1) { const text = try draw_state.fixture.row(row, arena); if (std.mem.indexOf(u8, text, "line ") != null) { seen += 1; } } try testing.expectEqual(@as(usize, 5), seen); } test "a notice stays on one line and ends in an ellipsis" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(12, 20); defer draw_state.deinit(); draw_state.notice(.err, "a very long error message that will not fit\nand a second line"); try draw_state.redraw(); const row = try draw_state.fixture.row(0, arena); try testing.expect(std.mem.endsWith(u8, row, "…")); try testing.expect(std.mem.indexOf(u8, try draw_state.fixture.row(1, arena), "second") == null); } test "the wheel and the page keys move the view and it follows the bottom again" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(8, 20); defer draw_state.deinit(); draw_state.assistant("one"); draw_state.assistant("two"); draw_state.assistant("three"); try draw_state.redraw(); try testing.expect(draw_state.view.follow); draw_state.view.scrollUp(3); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(0, arena), "one")); draw_state.view.pageDown(); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "three")); try testing.expect(draw_state.view.follow); draw_state.assistant("four"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "four")); } test "submitting jumps to the newest output" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(8, 20); defer draw_state.deinit(); draw_state.assistant("one"); draw_state.assistant("two"); draw_state.assistant("three"); try draw_state.redraw(); draw_state.view.scrollUp(3); try draw_state.redraw(); draw_state.view.toBottom(); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "three")); } test "the view follows a reply taller than the viewport" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(8, 20); defer draw_state.deinit(); draw_state.assistant("one"); draw_state.assistant("a\nb\nc\nd\ne\nf"); try draw_state.redraw(); // The bottom rows of the tall item are visible and the view keeps following. try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "f")); try testing.expect(draw_state.view.follow); draw_state.assistant("last"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "last")); var tall: std.ArrayList(u8) = .empty; for (0..20_000) |_| try tall.appendSlice(arena, "a\n"); try tall.appendSlice(arena, "end"); draw_state.assistant(tall.items); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "end")); } test "a scrolled up view keeps its rows and follows again at the bottom" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(8, 20); defer draw_state.deinit(); draw_state.assistant("one"); draw_state.assistant("two"); draw_state.assistant("three"); try draw_state.redraw(); draw_state.view.scrollUp(1); try draw_state.redraw(); try testing.expect(!draw_state.view.follow); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(0, arena), "one")); draw_state.assistant("four"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(0, arena), "one")); draw_state.view.scrollDown(3); try draw_state.redraw(); try testing.expect(draw_state.view.follow); draw_state.assistant("five"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(3, arena), "five")); } test "a twenty line prompt draws eight rows and keeps the transcript height" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(20, 20); defer draw_state.deinit(); for (0..20) |line| { try draw_state.editor.insert("l"); try draw_state.editor.insert(try std.fmt.allocPrint(arena, "{d}", .{line})); if (line < 19) { try draw_state.editor.newline(); } } try draw_state.redraw(); // footer 19, bottom border 18, eight prompt rows 10-17, top border 9, transcript 0-8 try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(19, arena), "fake · /tmp")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(18, arena), "────")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(17, arena), "l19")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(10, arena), "l12")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(9, arena), "────")); try testing.expect(std.mem.indexOf(u8, try draw_state.fixture.row(8, arena), "─") == null); } test "the prompt wraps by the width method of the screen" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(10, 8); defer draw_state.deinit(); draw_state.fixture.screen.width_method = .unicode; try draw_state.editor.insert("❤️👩‍🚀👩‍🚀a"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(6, arena), "────")); try testing.expectEqualStrings("a", draw_state.fixture.screen.readCell(6, 7).?.char.grapheme); } test "a screen with more than 65535 cells draws" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(220, 300); defer draw_state.deinit(); draw_state.assistant("hello"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(0, arena), "hello")); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(219, arena), "fake · /tmp")); } test "rows that the prompt gives back to the transcript are blank" { var arena_state = std.heap.ArenaAllocator.init(testing.allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); var draw_state: Draw = undefined; try draw_state.init(10, 20); defer draw_state.deinit(); try draw_state.editor.insert("a"); try draw_state.editor.newline(); try draw_state.editor.insert("b"); try draw_state.editor.newline(); try draw_state.editor.insert("c"); try draw_state.redraw(); try testing.expect(std.mem.startsWith(u8, try draw_state.fixture.row(5, arena), "a")); draw_state.editor.clear(); try draw_state.redraw(); // prompt rows 5-7 shrink to row 7, so rows 4 and 5 are now empty transcript rows const blank: [20]u8 = @splat(' '); for ([_]u16{ 4, 5 }) |line| { try testing.expectEqualStrings(&blank, try draw_state.fixture.row(line, arena)); } }