terminal: add persistent REPL history

Persists REPL history to .lp-history, except in script mode.
This commit is contained in:
Adrià Arrufat
2026-04-10 11:59:55 +02:00
parent 81413d9291
commit d0c36f7e3e
2 changed files with 16 additions and 4 deletions

View File

@@ -111,11 +111,15 @@ pub fn init(allocator: std.mem.Allocator, app: *App, opts: Config.Agent) !*Self
return error.ToolInitFailed;
};
// Persist REPL history in a cwd-relative `.lp-history`. Skipped in pure
// replay mode (no REPL is opened).
const history_path: ?[:0]const u8 = if (is_script_mode) null else ".lp-history";
self.* = .{
.allocator = allocator,
.ai_client = ai_client,
.tool_executor = tool_executor,
.terminal = Terminal.init(),
.terminal = Terminal.init(history_path),
.cmd_executor = undefined,
.recorder = Recorder.init(if (opts.save) opts.script_file else null),
.messages = .empty,

View File

@@ -13,6 +13,8 @@ const ansi_green = "\x1b[32m";
const ansi_yellow = "\x1b[33m";
const ansi_red = "\x1b[31m";
history_path: ?[:0]const u8,
const CommandInfo = struct { name: [:0]const u8, hint: [:0]const u8 };
const commands = [_]CommandInfo{
@@ -31,11 +33,14 @@ const commands = [_]CommandInfo{
.{ .name = "EXIT", .hint = "" },
};
pub fn init() Self {
pub fn init(history_path: ?[:0]const u8) Self {
c.linenoiseSetMultiLine(1);
c.linenoiseSetCompletionCallback(&completionCallback);
c.linenoiseSetHintsCallback(&hintsCallback);
return .{};
if (history_path) |path| {
_ = c.linenoiseHistoryLoad(path.ptr);
}
return .{ .history_path = history_path };
}
fn completionCallback(buf: [*c]const u8, lc: [*c]c.linenoiseCompletions) callconv(.c) void {
@@ -63,11 +68,14 @@ fn hintsCallback(buf: [*c]const u8, color: [*c]c_int, bold: [*c]c_int) callconv(
return null;
}
pub fn readLine(_: *Self, prompt: [*:0]const u8) ?[]const u8 {
pub fn readLine(self: *Self, prompt: [*:0]const u8) ?[]const u8 {
const line = c.linenoise(prompt) orelse return null;
const slice = std.mem.sliceTo(line, 0);
if (slice.len > 0) {
_ = c.linenoiseHistoryAdd(line);
if (self.history_path) |path| {
_ = c.linenoiseHistorySave(path.ptr);
}
}
return slice;
}