@@ -19,6 +19,7 @@ const usage_mod = @import("usage.zig");
1919 const diff_view = @import("diff_view.zig");
2020 const tool_view = @import("tool_view.zig");
2121 const image_view = @import("image.zig");
22+const picker_mod = @import("picker.zig");
2223 const screen = @import("screen.zig");
2324 const history = @import("../history.zig");
2425 const status = @import("../status.zig");
@@ -29,25 +30,11 @@ const BODY_PREFIX = " │ ";
2930 const ERROR_PREFIX = " ! ";
3031 const TOOL_BODY: styles.Style = .{ .dim = true };
3132
32-const physicalRows = render.physicalRows;
33-const rowsForCells = render.rowsForCells;
34-
3533 const PendingCall = tool_view.PendingCall;
34+const Picker = picker_mod.Picker;
3635
3736 const Command = commands.Command;
3837
39-const Picker = struct {
40- kind: enum { provider, model, thinking, history },
41- title: []const u8,
42- ids: []const []const u8 = &.{},
43- labels: []const []const u8 = &.{},
44- provider: []const u8 = "",
45- draft: []const u8 = "",
46- query: std.ArrayList(u8) = .empty,
47- matches: std.ArrayList(usize) = .empty,
48- sel: usize = 0,
49-};
50-
5138 var resize_flag = std.atomic.Value(bool).init(false);
5239 var exit_flag = std.atomic.Value(bool).init(false);
5340 var exit_signal = std.atomic.Value(u32).init(0);
@@ -343,7 +330,7 @@ const Tui = struct {
343330 const width = @max(self.term.width(), 1);
344331 const height = @max(self.term.height() - 1, 1);
345332 const status_line = self.statusLine();
346- const status_rows = physicalRows(status_line, width);
333+ const status_rows = render.physicalRows(status_line, width);
347334 const editor_budget = height -| status_rows;
348335 const ed = self.editor.layout(width, @max(editor_budget, 1));
349336
@@ -491,26 +478,26 @@ const Tui = struct {
491478 }
492479 }
493480
494- fn renderPendingCall(self: *Tui) void {
495- if (self.pending_calls.items.len == 0) return;
496- const call = &self.pending_calls.items[0];
481+ fn renderCall(self: *Tui, call: *PendingCall) void {
497482 if (call.rendered) return;
498483 self.scrollback.separator = true;
499484 for (tool_view.callBody(self.s, call.name, call.summary, @max(self.term.width(), 1))) |row| self.push(row);
500485 call.rendered = true;
501486 }
502487
488+ fn renderPendingCall(self: *Tui) void {
489+ if (self.pending_calls.items.len == 0) return;
490+ self.renderCall(&self.pending_calls.items[0]);
491+ }
492+
503493 fn commitToolResult(self: *Tui, name: []const u8, text: []const u8, is_error: bool, diffs: []const tools_index.FileDiff, body: ?[]const u8, images: []const message_mod.ImageContent) void {
504494 const shown = render.stripAnsi(self.s, text);
505495 const meta: ?[]const u8 = if (!is_error) body else null;
506496 var head_committed = false;
507497 if (self.pending_calls.items.len > 0) {
508- const call = self.pending_calls.orderedRemove(0);
498+ var call = self.pending_calls.orderedRemove(0);
509499 head_committed = true;
510- if (!call.rendered) {
511- self.scrollback.separator = true;
512- for (tool_view.callBody(self.s, call.name, call.summary, @max(self.term.width(), 1))) |row| self.push(row);
513- }
500+ self.renderCall(&call);
514501 }
515502 if (!head_committed) self.scrollback.separator = true;
516503 if (self.cfg.show_images) self.commitImages(images);
@@ -557,12 +544,12 @@ const Tui = struct {
557544 }
558545
559546 fn flushCalls(self: *Tui) void {
560- for (self.pending_calls.items) |call| {
561- if (call.rendered) continue;
562- self.scrollback.separator = true;
563- for (tool_view.callBody(self.s, call.name, call.summary, @max(self.term.width(), 1))) |row| self.push(row);
564- }
547+ for (self.pending_calls.items) |*call| self.renderCall(call);
565548 self.pending_calls.clearRetainingCapacity();
549+ self.resetCalls();
550+ }
551+
552+ fn resetCalls(self: *Tui) void {
566553 _ = self.calls.reset(.retain_capacity);
567554 self.ca = self.calls.allocator();
568555 }
@@ -609,7 +596,7 @@ const Tui = struct {
609596 p.labels = labels;
610597 p.draft = self.picker_m.dupe(u8, self.editor.contents()) catch "";
611598 self.editor.clear();
612- self.pickerRefresh();
599+ p.refresh(self.picker_m);
613600 self.dirty = true;
614601 }
615602
@@ -812,8 +799,7 @@ const Tui = struct {
812799 self.retry = false;
813800 self.phase = .idle;
814801 self.stream_kind = .none;
815- _ = self.calls.reset(.retain_capacity);
816- self.ca = self.calls.allocator();
802+ self.resetCalls();
817803 self.dirty = true;
818804 }
819805
@@ -881,17 +867,17 @@ const Tui = struct {
881867 if (self.picker) |*p| p.provider = self.model_provider;
882868 }
883869
884- fn openPicker(self: *Tui, kind: @FieldType(Picker, "kind"), title: []const u8, ids: []const []const u8, labels: []const []const u8) void {
870+ fn openPicker(self: *Tui, kind: picker_mod.Kind, title: []const u8, ids: []const []const u8, labels: []const []const u8) void {
885871 self.startPicker(kind, title);
886872 if (self.picker) |*p| {
887873 p.ids = ids;
888874 p.labels = labels;
875+ p.refresh(self.picker_m);
889876 }
890- self.pickerRefresh();
891877 self.dirty = true;
892878 }
893879
894- fn startPicker(self: *Tui, kind: @FieldType(Picker, "kind"), title: []const u8) void {
880+ fn startPicker(self: *Tui, kind: picker_mod.Kind, title: []const u8) void {
895881 _ = self.picker_arena.reset(.retain_capacity);
896882 self.picker_m = self.picker_arena.allocator();
897883 self.picker = .{ .kind = kind, .title = title };
@@ -902,59 +888,26 @@ const Tui = struct {
902888 self.picker = null;
903889 }
904890
905- fn pickerRefresh(self: *Tui) void {
906- const p = &(self.picker orelse return);
907- p.matches.clearRetainingCapacity();
908- const query = p.query.items;
909- const n = @min(p.ids.len, p.labels.len);
910- for (p.ids[0..n], p.labels[0..n], 0..) |id, label, i| {
911- if (query.len != 0 and std.ascii.findIgnoreCase(label, query) == null and
912- std.ascii.findIgnoreCase(id, query) == null) continue;
913- p.matches.append(self.picker_m, i) catch {};
914- }
915- p.sel = if (p.matches.items.len == 0) 0 else @min(p.sel, p.matches.items.len - 1);
916- }
917-
918891 fn pickerKey(self: *Tui, key: input.Key) void {
919892 const p = &(self.picker orelse return);
920- switch (key) {
921- .text => |t| {
922- p.query.appendSlice(self.picker_m, t) catch {};
923- p.sel = 0;
924- self.pickerRefresh();
925- },
926- .backspace => {
927- if (p.query.items.len > 0) {
928- var start = p.query.items.len - 1;
929- while (start > 0 and (p.query.items[start] & 0xc0) == 0x80) start -= 1;
930- p.query.shrinkRetainingCapacity(start);
931- p.sel = 0;
932- self.pickerRefresh();
933- }
934- },
935- .up, .history_prev => if (p.sel > 0) {
936- p.sel -= 1;
937- },
938- .down, .history_next => if (p.sel + 1 < p.matches.items.len) {
939- p.sel += 1;
940- },
941- .submit => self.pickerAccept(),
942- .escape, .interrupt => {
893+ switch (p.key(self.picker_m, key)) {
894+ .ignore => return,
895+ .update => {},
896+ .accept => self.pickerAccept(),
897+ .cancel => {
943898 const draft = p.draft;
944899 const restore = p.kind == .history;
945900 self.closePicker();
946901 self.command_active = false;
947902 if (restore) self.editor.setText(draft);
948903 },
949- else => return,
950904 }
951905 self.dirty = true;
952906 }
953907
954908 fn pickerAccept(self: *Tui) void {
955909 const p = self.picker orelse return;
956- if (p.matches.items.len == 0) return;
Cut short: this commit's diff is over 1000 lines.