mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-06-11 17:46:32 -04:00
terminal: add persistent REPL history
Persists REPL history to .lp-history, except in script mode.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user