@@ -22,59 +22,68 @@ const SPINNER_MS = 120;
2222 const BODY_PREFIX = " | ";
2323 const ERROR_PREFIX = " ! ";
2424
25-const HISTORY_LINES = 1000;
26-
2725 const MAX_BODY_ROWS = 12;
2826 const ELIDED_HEAD = 4;
2927 const ELIDED_TAIL = 4;
3028
29+fn physicalRows(line: []const u8, width: usize) usize {
30+ return rowsForCells(render.displayWidth(line), width);
31+}
32+
33+fn rowsForCells(cells: usize, width: usize) usize {
34+ if (cells == 0) return 1;
35+ return (cells + width - 1) / width;
36+}
37+
3138 const PendingCall = struct { name: []const u8, summary: []const u8 };
3239
3340 const STATE_WIDTH = "running".len;
3441 const STATE_PAD = std.fmt.comptimePrint("{{s: <{d}}}", .{STATE_WIDTH});
3542
36-fn paintRow(a: std.mem.Allocator, line: []const u8) []const u8 {
37- return std.fmt.allocPrint(a, "{s}{s}\x1b[K", .{ theme.SGR_PLAIN, line }) catch line;
38-}
39-
40-fn styleRow(a: std.mem.Allocator, line: stream.BodyLine, width: usize) []const []const u8 {
43+fn styleLine(a: std.mem.Allocator, line: stream.BodyLine) []const u8 {
4144 const safe = render.sanitize(a, line.text);
4245 const expanded = render.expandTabs(a, safe, 4);
43- const wrapped = render.wrapLine(a, expanded, width);
44- var out: std.ArrayList([]const u8) = .empty;
45- for (wrapped) |row| {
46- if (row.len == 0) {
47- out.append(a, "") catch {};
48- continue;
49- }
50- const row_bg = if (line.bg) |bg| theme.sgrBg(a, bg) else theme.SGR_NORMAL_BG;
51- const styled = if (line.style) |s| styles.styledWith(a, .{
52- .fg = s.fg,
53- .bg = line.bg orelse theme.current.bg,
54- .bold = s.bold,
55- .italic = s.italic,
56- .underline = s.underline,
57- }, row) else row;
58- out.append(a, std.fmt.allocPrint(a, "{s}{s}{s}", .{ row_bg, styled, row_bg }) catch row) catch {};
59- }
60- return out.items;
46+ if (expanded.len == 0) return "";
47+ const row_bg = if (line.bg) |bg| theme.sgrBg(a, bg) else theme.SGR_NORMAL_BG;
48+ const styled = if (line.style) |s| styles.styledWith(a, .{
49+ .fg = s.fg,
50+ .bg = line.bg orelse theme.current.bg,
51+ .bold = s.bold,
52+ .italic = s.italic,
53+ .underline = s.underline,
54+ }, expanded) else expanded;
55+ return std.fmt.allocPrint(a, "{s}{s}{s}", .{ row_bg, styled, row_bg }) catch expanded;
6156 }
6257
63-fn renderRows(a: std.mem.Allocator, lines: []const stream.BodyLine, width: usize) []const []const u8 {
58+fn plainRows(a: std.mem.Allocator, lines: []const stream.BodyLine) []const []const u8 {
6459 var out: std.ArrayList([]const u8) = .empty;
65- for (lines) |line| {
66- for (styleRow(a, line, width)) |row| out.append(a, row) catch {};
67- }
60+ for (lines) |line| out.append(a, styleLine(a, line)) catch {};
6861 return out.items;
6962 }
7063
7164 fn bodyRows(a: std.mem.Allocator, lines: []const stream.BodyLine, width: usize) []const []const u8 {
72- const rows = renderRows(a, lines, width);
73- if (rows.len <= MAX_BODY_ROWS) return rows;
65+ const rows = plainRows(a, lines);
66+ var height: usize = 0;
67+ for (rows) |r| height += physicalRows(r, width);
68+ if (height <= MAX_BODY_ROWS or rows.len <= ELIDED_HEAD + ELIDED_TAIL) return rows;
69+ const tail_at = rows.len - ELIDED_TAIL;
70+ var shown: usize = 0;
71+ var head: usize = 0;
72+ while (head < tail_at) : (head += 1) {
73+ const h = physicalRows(rows[head], width);
74+ if (shown + h + ELIDED_TAIL > MAX_BODY_ROWS - 1) break;
75+ shown += h;
76+ }
7477 var out: std.ArrayList([]const u8) = .empty;
75- for (rows[0..ELIDED_HEAD]) |r| out.append(a, r) catch {};
76- out.append(a, styles.dim(a, std.fmt.allocPrint(a, "... {d} lines not shown ...", .{rows.len - ELIDED_HEAD - ELIDED_TAIL}) catch "")) catch {};
77- for (rows[rows.len - ELIDED_TAIL ..]) |r| out.append(a, r) catch {};
78+ for (rows[0..head]) |r| out.append(a, r) catch {};
79+ const hidden_rows: usize = blk: {
80+ var n = height;
81+ for (rows[0..head]) |r| n -= physicalRows(r, width);
82+ for (rows[tail_at..]) |r| n -= physicalRows(r, width);
83+ break :blk n;
84+ };
85+ out.append(a, styles.dim(a, std.fmt.allocPrint(a, "... {d} lines not shown ...", .{hidden_rows}) catch "")) catch {};
86+ for (rows[tail_at..]) |r| out.append(a, r) catch {};
7887 return out.items;
7988 }
8089
@@ -82,12 +91,14 @@ fn callHead(a: std.mem.Allocator, name: []const u8) []const u8 {
8291 return styles.teal(a, std.fmt.allocPrint(a, "-> {s}", .{name}) catch "->");
8392 }
8493
85-fn callRows(a: std.mem.Allocator, width: usize, call: PendingCall, running: bool) []const []const u8 {
94+fn callRows(a: std.mem.Allocator, call: PendingCall, running: bool) []const []const u8 {
8695 const padded = std.fmt.allocPrint(a, STATE_PAD, .{if (running) "running" else "queued"}) catch "";
8796 const word = if (running) styles.teal(a, padded) else styles.dim(a, padded);
8897 const head = callHead(a, call.name);
8998 const text = render.expandTabs(a, render.sanitize(a, call.summary), 4);
90- return render.wrapLine(a, std.fmt.allocPrint(a, "{s} {s} {s}", .{ word, head, text }) catch text, width);
99+ var out: std.ArrayList([]const u8) = .empty;
100+ out.append(a, std.fmt.allocPrint(a, "{s} {s} {s}", .{ word, head, text }) catch text) catch {};
101+ return out.items;
91102 }
92103
93104 fn collapseWs(a: std.mem.Allocator, s: []const u8) []const u8 {
@@ -365,36 +376,59 @@ fn contextUsageLine(a: std.mem.Allocator, used: u64, model: *const types.Model)
365376 }
366377
367378 const LiveRegion = struct {
368- rows: usize = 0,
369- cursor_up: usize = 0,
379+ widths: std.ArrayList(usize) = .empty,
380+ caret_line: usize = 0,
381+ caret_cell: usize = 0,
382+ drawn: bool = false,
383+
384+ fn remember(self: *LiveRegion, lines: []const []const u8, caret_line: usize, caret_cell: usize) void {
385+ self.widths.clearRetainingCapacity();
386+ for (lines) |line| self.widths.append(platform.gpa, render.displayWidth(line)) catch {};
387+ self.caret_line = caret_line;
388+ self.caret_cell = caret_cell;
389+ self.drawn = true;
390+ }
391+
392+ fn caretRow(self: *LiveRegion, width: usize) usize {
393+ var row: usize = 0;
394+ const lines = @min(self.caret_line, self.widths.items.len);
395+ for (self.widths.items[0..lines]) |cells| row += rowsForCells(cells, width);
396+ return row + self.caret_cell / width;
397+ }
398+
399+ fn totalRows(self: *LiveRegion, width: usize) usize {
400+ var row: usize = 0;
401+ for (self.widths.items) |cells| row += rowsForCells(cells, width);
402+ return row;
403+ }
370404
371- fn clear(self: *LiveRegion, a: std.mem.Allocator) []const u8 {
372- if (self.rows == 0) return "";
405+ fn erase(self: *LiveRegion, a: std.mem.Allocator, width: usize) []const u8 {
406+ if (!self.drawn) return "";
407+ const up = self.caretRow(width);
373408 var out: std.ArrayList(u8) = .empty;
374- if (self.cursor_up > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}B", .{self.cursor_up}) catch "") catch {};
375- if (self.rows > 1) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}A", .{self.rows - 1}) catch "") catch {};
409+ if (up > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}A", .{up}) catch "") catch {};
376410 out.appendSlice(a, "\r") catch {};
377411 out.appendSlice(a, theme.SGR_PLAIN) catch {};
378412 out.appendSlice(a, "\x1b[J") catch {};
379- self.rows = 0;
380- self.cursor_up = 0;
381413 return out.items;
382414 }
383415
384- fn draw(self: *LiveRegion, a: std.mem.Allocator, lines: []const []const u8, cursor_row: usize, cursor_col: usize, above: []const u8) []const u8 {
416+ fn draw(self: *LiveRegion, a: std.mem.Allocator, width: usize, lines: []const []const u8, caret_line: usize, caret_cell: usize, above: []const u8) []const u8 {
385417 var out: std.ArrayList(u8) = .empty;
386- out.appendSlice(a, self.clear(a)) catch {};
418+ out.appendSlice(a, self.erase(a, width)) catch {};
387419 out.appendSlice(a, above) catch {};
388420 for (lines, 0..) |line, i| {
389421 if (i > 0) out.appendSlice(a, "\r\n") catch {};
390422 out.appendSlice(a, line) catch {};
391423 }
392- const up = if (lines.len > 0 and lines.len - 1 > cursor_row) lines.len - 1 - cursor_row else 0;
424+ self.remember(lines, caret_line, caret_cell);
425+ const end_row = self.totalRows(width) - 1;
426+ const caret_row = self.caretRow(width);
427+ const up = if (end_row > caret_row) end_row - caret_row else 0;
393428 if (up > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}A", .{up}) catch "") catch {};
394429 out.appendSlice(a, "\r") catch {};
395- if (cursor_col > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}G", .{cursor_col + 1}) catch "") catch {};
396- self.rows = lines.len;
397- self.cursor_up = up;
430+ const col = caret_cell % width;
431+ if (col > 0) out.appendSlice(a, std.fmt.allocPrint(a, "\x1b[{d}C", .{col}) catch "") catch {};
398432 return out.items;
399433 }
400434 };
@@ -452,7 +486,6 @@ const Tui = struct {
452486 abort: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
453487
454488 scroll: std.ArrayList(u8) = .empty,
455- history: std.ArrayList([]const u8) = .empty,
456489 wrote: bool = false,
457490 last_blank: bool = false,
458491 separator: bool = false,
@@ -518,17 +551,13 @@ const Tui = struct {
518551 const blank = clean.len == 0 or (self.separator and self.wrote);
519552 self.separator = false;
520553 if (blank and self.wrote and !self.last_blank) {
521- self.history.append(platform.gpa, "") catch {};
522- self.capHistory();
523- self.scroll.appendSlice(self.a, paintRow(self.sa, "")) catch {};
554+ self.scroll.appendSlice(self.a, theme.SGR_PLAIN) catch {};
524555 self.scroll.appendSlice(self.a, "\r\n") catch {};
525556 self.last_blank = true;
526557 }
527558 if (clean.len != 0) {
528- if (platform.gpa.dupe(u8, clean)) |owned| {
529- if (self.history.append(platform.gpa, owned)) |_| self.capHistory() else |_| platform.gpa.free(owned);
530- } else |_| {}
531- self.scroll.appendSlice(self.a, paintRow(self.sa, clean)) catch {};
559+ self.scroll.appendSlice(self.a, clean) catch {};
560+ self.scroll.appendSlice(self.a, theme.SGR_PLAIN) catch {};
532561 self.scroll.appendSlice(self.a, "\r\n") catch {};
533562 self.wrote = true;
534563 self.last_blank = false;
@@ -536,32 +565,8 @@ const Tui = struct {
536565 self.dirty = true;
537566 }
538567
539- fn capHistory(self: *Tui) void {
540- while (self.history.items.len > HISTORY_LINES) {
541- const old = self.history.orderedRemove(0);
542- if (old.len > 0) platform.gpa.free(old);
543- }
544- }
545-
546- fn reflow(self: *Tui) void {
547- self.term.refreshSize();
548- self.live.rows = 0;
549- self.live.cursor_up = 0;
550- self.scroll.clearRetainingCapacity();
551- self.term.write("\x1b[2J\x1b[3J\x1b[H");
552- const width = @max(self.term.width(), 1);
553- for (self.history.items) |line| {
554- for (render.wrapLine(self.s, line, width)) |row| {
555- self.scroll.appendSlice(self.a, paintRow(self.sa, row)) catch {};
556- self.scroll.appendSlice(self.a, "\r\n") catch {};
557- }
558- }
559- self.dirty = true;
560- }
561-
562568 fn commitLines(self: *Tui, lines: []const stream.BodyLine) void {
563- const width = @max(self.term.width(), 1);
564- for (renderRows(self.s, lines, width)) |row| self.push(row);
569+ for (lines) |line| self.push(styleLine(self.sa, line));
565570 }
566571
567572 fn note(self: *Tui, line: []const u8) void {
@@ -638,14 +643,15 @@ const Tui = struct {
638643 fn queueRows(self: *Tui, width: usize, budget: usize) []const []const u8 {
639644 if (budget == 0) return &.{};
640645 var out: std.ArrayList([]const u8) = .empty;
646+ var used: usize = 0;
641647 for (self.pending_calls.items, 0..) |call, i| {
642- for (callRows(self.s, width, call, i == 0 and self.phase == .running_tool)) |row| out.append(self.s, row) catch {};
648+ for (callRows(self.s, call, i == 0 and self.phase == .running_tool)) |row| {
649+ const h = physicalRows(row, width);
650+ if (used + h > budget and out.items.len > 0) break;
651+ used += h;
652+ out.append(self.s, row) catch {};
653+ }
643654 }
644- if (out.items.len <= budget) return out.items;
645- const keep = budget - 1;
646- const hidden = out.items.len - keep;
647- out.shrinkRetainingCapacity(keep);
648- out.append(self.s, styles.dim(self.s, std.fmt.allocPrint(self.s, "... {d} more lines ...", .{hidden}) catch "...")) catch {};
649655 return out.items;
650656 }
651657
@@ -654,33 +660,38 @@ const Tui = struct {
654660 self.resetScratch();
655661 const width = @max(self.term.width(), 1);
656662 const height = @max(self.term.height() - 1, 1);
657- const status = render.wrapLine(self.s, self.statusLine(), width);
663+ const status = self.statusLine();
658664
659665 var inflight = self.reply.pending();
660666 if (inflight.len == 0) inflight = self.activity.pending();
661- const rows = renderRows(self.s, inflight, width);
667+ const rows = plainRows(self.s, inflight);
662668
663- const room = if (height > status.len + 1) height - status.len - 1 else 0;
669+ const status_rows = physicalRows(status, width);
670+ const room = if (height > status_rows + 1) height - status_rows - 1 else 0;
664671 const queue = self.queueRows(width, room);
665- const keep = @min(rows.len, room - queue.len);
666- const body = rows[rows.len - keep ..];
667- const ed = self.editor.layout(width, @max(height - status.len - queue.len - body.len, 1));
672+ var queue_height: usize = 0;
673+ for (queue) |r| queue_height += physicalRows(r, width);
674+ const body_budget = if (room > queue_height) room - queue_height else 0;
675+ var body_start = rows.len;
676+ var body_used: usize = 0;
677+ while (body_start > 0) {
678+ const h = physicalRows(rows[body_start - 1], width);
679+ if (body_used + h > body_budget) break;
680+ body_used += h;
681+ body_start -= 1;
682+ }
683+ const body = rows[body_start..];
684+ const editor_budget = height -| (status_rows + queue_height + body_used);
685+ const ed = self.editor.layout(width, @max(editor_budget, 1));
668686
669687 var lines: std.ArrayList([]const u8) = .empty;
670- for (body) |r| lines.append(self.s, paintRow(self.s, r)) catch {};
671- for (queue) |r| lines.append(self.s, paintRow(self.s, r)) catch {};
672- for (status) |r| lines.append(self.s, paintRow(self.s, r)) catch {};
673- for (ed.rows) |r| lines.append(self.s, paintRow(self.s, r)) catch {};
674- var cursor_row = body.len + queue.len + status.len + ed.cursor_row;
675- if (cursor_row >= lines.items.len and lines.items.len > 0) cursor_row = lines.items.len - 1;
676-
677- const scroll = self.scroll.items;
678- const reanchor = scroll.len > 0 and self.live.rows >= self.term.height() - 1;
679- const above = if (reanchor)
680- std.fmt.allocPrint(self.s, "{s}\r\n", .{scroll}) catch scroll
681- else
682- scroll;
683- const frame_text = self.live.draw(self.s, lines.items, cursor_row, ed.cursor_col, above);
688+ for (body) |r| lines.append(self.s, r) catch {};
689+ for (queue) |r| lines.append(self.s, r) catch {};
690+ lines.append(self.s, status) catch {};
691+ const caret_line = lines.items.len + ed.cursor_row;
692+ for (ed.rows) |r| lines.append(self.s, r) catch {};
693+
694+ const frame_text = self.live.draw(self.s, width, lines.items, caret_line, ed.cursor_col, self.scroll.items);
684695 self.term.write(frame_text);
685696 self.scroll.clearRetainingCapacity();
686697 _ = self.sarena.reset(.retain_capacity);
@@ -768,13 +779,13 @@ const Tui = struct {
768779 &.{.{ .text = body.? }}
769780 else
770781 resultLines(self.s, name, text, is_error);
771- const rows = if (std.mem.eql(u8, name, "edit")) renderRows(self.s, lines, width) else bodyRows(self.s, lines, width);
782+ const rows = if (std.mem.eql(u8, name, "edit")) plainRows(self.s, lines) else bodyRows(self.s, lines, width);
772783 for (rows, 0..) |row, i| {
773784 const prefix = if (is_error and i == rows.len - 1) styles.red(self.s, ERROR_PREFIX) else styles.dim(self.s, BODY_PREFIX);
774785 self.push(std.fmt.allocPrint(self.s, "{s}{s}", .{ prefix, row }) catch row);
775786 }
776787 if (diffs.len > 0) {
777- for (renderRows(self.s, diffRows(self.s, diffs), width)) |row| {
788+ for (plainRows(self.s, diffRows(self.s, diffs))) |row| {
778789 self.push(std.fmt.allocPrint(self.s, "{s}{s}", .{ styles.dim(self.s, BODY_PREFIX), row }) catch row);
779790 }
780791 }
@@ -1066,7 +1077,8 @@ const Tui = struct {
10661077 self.closed = true;
10671078 self.abort.store(true, .seq_cst);
10681079 self.steering_ready.store(true, .seq_cst);
1069- self.term.write(self.live.clear(self.a));
1080+ const width = @max(self.term.width(), 1);
1081+ self.term.write(self.live.erase(self.a, width));
10701082 self.term.write(self.scroll.items);
10711083 self.term.stop();
10721084 self.opts.session.close();
@@ -1288,7 +1300,10 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
12881300 self.dirty = true;
12891301 }
12901302
1291- if (resize_flag.swap(false, .seq_cst)) self.reflow();
1303+ if (resize_flag.swap(false, .seq_cst)) {
1304+ self.term.refreshSize();
1305+ self.dirty = true;
1306+ }
12921307 if (exit_flag.swap(false, .seq_cst)) self.exit();
12931308
12941309 const now = util.nowMs();