@@ -20,6 +20,7 @@ const usage_mod = @import("usage.zig");
2020 const diff_view = @import("diff_view.zig");
2121 const tool_view = @import("tool_view.zig");
2222 const screen = @import("screen.zig");
23+const history = @import("../history.zig");
2324 const status = @import("../status.zig");
2425
2526 const SPINNER = "⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐ …
@@ -34,6 +35,12 @@ const PendingCall = tool_view.PendingCall;
3435
3536 const Command = commands.Command;
3637
38+const Search = struct {
39+ query: std.ArrayList(u8) = .empty,
40+ saved: []const u8 = "",
41+ match: ?[]const u8 = null,
42+};
43+
3744 var resize_flag = std.atomic.Value(bool).init(false);
3845 var exit_flag = std.atomic.Value(bool).init(false);
3946
@@ -49,6 +56,7 @@ fn onExitSignal(_: std.posix.SIG) callconv(.c) void {
4956 const Tui = struct {
5057 opts: *agent.Options,
5158 cfg: *const config.Config,
59+ history: history.Store = undefined,
5260 tool_names: []const config.ToolName,
5361 term: term.Terminal = .{},
5462 editor: editor_mod.Editor,
@@ -105,6 +113,8 @@ const Tui = struct {
105113 last_input_ms: i64 = 0,
106114 command_active: bool = false,
107115
116+ search: ?Search = null,
117+
108118 prompt_open: bool = false,
109119 prompt_options: []const []const u8 = &.{},
110120 prompt_ids: []const []const u8 = &.{},
@@ -303,16 +313,48 @@ const Tui = struct {
303313
304314 var lines: std.ArrayList([]const u8) = .empty;
305315 lines.append(self.s, status_line) catch {};
306- const caret_line = lines.items.len + ed.cursor_row;
307- for (ed.rows) |r| lines.append(self.s, r) catch {};
316+ var caret_line: usize = 0;
317+ var caret_col: usize = 0;
318+ if (self.search) |sr| {
319+ const prompt = std.fmt.allocPrint(self.s, "reverse: {s}", .{sr.query.items}) catch "reverse: ";
320+ caret_line = 1;
321+ caret_col = "reverse: ".len + sr.query.items.len;
322+ lines.append(self.s, prompt) catch {};
323+ if (self.searchMatch(&sr)) |m| {
324+ const text = render.sanitize(self.s, m);
325+ const budget_rows = @max(height -| 2, 1);
326+ var used: usize = 0;
327+ var it = std.mem.splitScalar(u8, text, '\n');
328+ while (it.next()) |raw| {
329+ const part = if (raw.len == 0) " " else raw;
330+ for (render.wrapLine(self.s, part, width)) |row| {
331+ if (used >= budget_rows) break;
332+ used += 1;
333+ lines.append(self.s, styles.dim(self.s, row)) catch {};
334+ }
335+ }
336+ } else {
337+ lines.append(self.s, styles.dim(self.s, "no matches")) catch {};
338+ }
339+ } else {
340+ for (ed.rows) |r| lines.append(self.s, r) catch {};
341+ caret_line = 1 + ed.cursor_row;
342+ caret_col = ed.cursor_col;
343+ }
308344
309- const frame_text = self.live.draw(self.s, width, lines.items, caret_line, ed.cursor_col, self.scrollback.buf.items);
345+ const frame_text = self.live.draw(self.s, width, lines.items, caret_line, caret_col, self.scrollback.buf.items);
310346 self.term.write(frame_text);
311347 self.scrollback.clear();
312348 _ = self.sarena.reset(.retain_capacity);
313349 self.sa = self.sarena.allocator();
314350 }
315351
352+ fn searchMatch(self: *Tui, sr: *const Search) ?[]const u8 {
353+ for (self.history.entries.items) |entry| {
354+ if (sr.query.items.len == 0 or std.ascii.indexOfIgnoreCase(entry, sr.query.items) != null) return entry;
355+ }
356+ return null;
357+ }
316358 fn keepName(self: *Tui, slot: *?[]const u8, value: ?[]const u8) void {
317359 if (slot.*) |old| self.a.free(old);
318360 slot.* = if (value) |v| self.a.dupe(u8, v) catch null else null;
@@ -456,8 +498,63 @@ const Tui = struct {
456498 self.pending_calls.clearRetainingCapacity();
457499 }
458500
501+ fn openSearch(self: *Tui) void {
502+ const draft = self.editor.contents();
503+ self.search = .{ .saved = self.a.dupe(u8, draft) catch return };
504+ self.editor.clear();
505+ self.dirty = true;
506+ }
507+
508+ fn closeSearch(self: *Tui, restore: bool, chosen: ?[]const u8) void {
509+ const search = self.search orelse return;
510+ self.search = null;
511+ self.editor.clear();
512+ defer self.dirty = true;
513+ if (chosen) |text| self.editor.setText(text);
514+ if (restore) self.editor.setText(search.saved);
515+ }
516+
517+ fn searchKey(self: *Tui, key: input.Key) void {
518+ const search = &(self.search orelse return);
519+ switch (key) {
520+ .text => |t| search.query.appendSlice(self.a, t) catch {},
521+ .backspace => {
522+ if (search.query.items.len > 0) {
523+ var start = search.query.items.len - 1;
524+ while (start > 0 and (search.query.items[start] & 0xc0) == 0x80) start -= 1;
525+ search.query.shrinkRetainingCapacity(start);
526+ }
527+ },
528+ .interrupt => return self.closeSearch(true, null),
529+ .escape => return self.closeSearch(true, null),
530+ .submit => return self.searchSubmit(),
531+ else => {},
532+ }
533+ self.dirty = true;
534+ }
535+
536+ fn searchSubmit(self: *Tui) void {
537+ var match: ?[]const u8 = null;
538+ const query = self.search.?.query.items;
539+ for (self.history.entries.items) |entry| {
540+ if (query.len == 0 or std.ascii.indexOfIgnoreCase(entry, query) != null) {
541+ match = entry;
542+ break;
543+ }
544+ }
545+ if (match) |text| self.closeSearch(false, text) else self.dirty = true;
546+ }
547+
459548 fn handleKey(self: *Tui, key: input.Key) void {
460549 self.resetScratch();
550+ if (self.search != null) {
551+ self.searchKey(key);
552+ return;
553+ }
554+ if (key == .reverse_search) {
555+ self.openSearch();
556+ return;
557+ }
461558 if (key == .eof) {
462559 if (!self.active and !self.command_active and self.editor.contents().len == 0) self.exit();
463560 return;
@@ -545,6 +642,7 @@ const Tui = struct {
545642 return;
546643 };
547644 self.commitUser(text);
645+ self.history.add(text);
548646 self.startTurn();
549647 }
550648
@@ -874,6 +972,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
874972 const self = try gpa.create(Tui);
875973 self.* = Tui.init(gpa, opts_ptr, cfg, tool_names);
876974 self.bindAllocators();
975+ self.history = history.Store.init(gpa, config.historyPath(gpa), cfg.history_size);
877976
878977 self.term.start();
879978 defer self.term.stop();