Compare commits

...

8 commits

3 changed files with 80 additions and 6 deletions

View file

@ -22,6 +22,7 @@ animation_max_lag: usize = 50, //milliseconds
hover_time_ms: usize = 500, //milliseconds
input_idle_time_ms: usize = 150, //milliseconds
idle_actions: []const IdleAction = &default_actions,
idle_commands: ?[]const []const u8 = null, // a list of simple commands
enable_format_on_save: bool = false,
restore_last_cursor_position: bool = true,
follow_cursor_on_buffer_switch: bool = false, //scroll cursor into view on buffer switch
@ -32,6 +33,9 @@ limit_auto_save_file_types: ?[]const []const u8 = null, // null means *all*
enable_prefix_keyhints: bool = true,
enable_auto_find: bool = true,
auto_run_time_seconds: usize = 120, //seconds
auto_run_commands: ?[]const []const u8 = &.{"save_session_quiet"}, // a list of simple commands
indent_size: usize = 4,
tab_width: usize = 8,
indent_mode: IndentMode = .auto,

View file

@ -355,6 +355,26 @@ const cmds = struct {
}
pub const quit_without_saving_meta: Meta = .{ .description = "Quit without saving" };
pub fn save_session(self: *Self, _: Ctx) Result {
const logger = log.logger("session");
defer logger.deinit();
logger.print("saving session...", .{});
try self.write_restore_info();
logger.print("session saved", .{});
}
pub const save_session_meta: Meta = .{ .description = "Save session" };
pub fn save_session_quiet(self: *Self, _: Ctx) Result {
try self.write_restore_info();
}
pub const save_session_quiet_meta: Meta = .{};
pub fn save_session_and_quit(self: *Self, _: Ctx) Result {
try self.write_restore_info();
try tp.self_pid().send("quit");
}
pub const save_session_and_quit_meta: Meta = .{ .description = "Save session and quit" };
pub fn open_project_cwd(self: *Self, _: Ctx) Result {
if (try project_manager.open(".")) |state|
try self.restore_state(state);
@ -789,9 +809,6 @@ const cmds = struct {
pub const close_buffer_meta: Meta = .{ .arguments = &.{.string} };
pub fn restore_session(self: *Self, _: Ctx) Result {
if (tp.env.get().str("project").len == 0) {
try open_project_cwd(self, .{});
}
try self.create_editor();
try self.read_restore_info();
tui.need_render();
@ -1665,6 +1682,8 @@ pub fn write_restore_info(self: *Self) WriteStateError!void {
}
pub fn write_state(self: *Self, writer: *std.Io.Writer) WriteStateError!void {
const current_project = tp.env.get().str("project");
try cbor.writeValue(writer, current_project);
if (self.get_active_editor()) |editor| {
try cbor.writeValue(writer, editor.file_path);
editor.update_meta();
@ -1701,20 +1720,36 @@ fn read_restore_info(self: *Self) !void {
const size = try file.readAll(buf);
var iter: []const u8 = buf[0..size];
try self.extract_state(&iter);
try self.extract_state(&iter, .with_project);
}
fn restore_state(self: *Self, state: []const u8) !void {
var iter = state;
try self.extract_state(&iter);
try self.extract_state(&iter, .no_project);
}
fn extract_state(self: *Self, iter: *[]const u8) !void {
fn extract_state(self: *Self, iter: *[]const u8, mode: enum { no_project, with_project }) !void {
const logger = log.logger("extract_state");
defer logger.deinit();
tp.trace(tp.channel.debug, .{ "mainview", "extract" });
var project_dir: []const u8 = undefined;
var editor_file_path: ?[]const u8 = undefined;
var prev_len = iter.len;
if (!try cbor.matchValue(iter, cbor.extract(&project_dir))) {
logger.print("restore project_dir failed", .{});
return error.MatchStoredProjectFailed;
}
switch (mode) {
.with_project => {
_ = try project_manager.open(project_dir);
tui.rdr().set_terminal_working_directory(project_dir);
if (self.top_bar) |bar| _ = try bar.msg(.{ "PRJ", "open" });
if (self.bottom_bar) |bar| _ = try bar.msg(.{ "PRJ", "open" });
},
.no_project => {},
}
if (!try cbor.matchValue(iter, cbor.extract(&editor_file_path))) {
logger.print("restore editor_file_path failed", .{});
return error.MatchFilePathFailed;

View file

@ -87,6 +87,8 @@ color_scheme_locked: bool = false,
hint_mode: HintMode = .prefix,
last_palette: ?LastPalette = null,
auto_run_timer: ?tp.Cancellable = null,
const HintMode = enum { none, prefix, all };
const LastPalette = struct {
@ -257,9 +259,15 @@ fn init_delayed(self: *Self) command.Result {
try cmds.enter_mode(self, command.Context.fmt(.{keybind.default_mode}));
}
}
self.start_auto_run_timer();
}
fn deinit(self: *Self) void {
if (self.auto_run_timer) |*t| {
t.cancel() catch {};
t.deinit();
self.auto_run_timer = null;
}
if (self.input_idle_timer) |*t| {
t.cancel() catch {};
t.deinit();
@ -310,6 +318,9 @@ fn handle_input_idle(self: *Self) void {
var buf: [32]u8 = undefined;
const m = tp.message.fmtbuf(&buf, .{"input_idle"}) catch return;
_ = self.send_widgets(tp.self_pid(), m) catch return;
const idle_cmds = self.config_.idle_commands orelse return;
for (idle_cmds) |cmd|
command.executeName(cmd, .{}) catch |e| self.logger.print_err("idlerun", "idle run command '{s}' failed: {t}", .{ cmd, e });
}
fn update_input_idle_timer(self: *Self) void {
@ -346,6 +357,23 @@ fn update_mouse_idle_timer(self: *Self) void {
self.mouse_idle_timer = tp.self_pid().delay_send_cancellable(self.allocator, "tui.mouse_idle_timer", delay, .{"MOUSE_IDLE"}) catch return;
}
fn auto_run(self: *Self) void {
const auto_run_cmds = self.config_.auto_run_commands orelse return;
for (auto_run_cmds) |cmd|
command.executeName(cmd, .{}) catch |e| self.logger.print_err("autorun", "auto run command '{s}' failed: {t}", .{ cmd, e });
self.start_auto_run_timer();
}
fn start_auto_run_timer(self: *Self) void {
const delay = std.time.us_per_s * @as(u64, self.config_.auto_run_time_seconds);
if (self.auto_run_timer) |*t| {
t.cancel() catch {};
t.deinit();
self.auto_run_timer = null;
}
self.auto_run_timer = tp.self_pid().delay_send_cancellable(self.allocator, "tui.auto_run_timer", delay, .{"AUTO_RUN"}) catch return;
}
fn receive(self: *Self, from: tp.pid_ref, m: tp.message) tp.result {
const frame = tracy.initZone(@src(), .{ .name = "tui" });
defer frame.deinit();
@ -510,6 +538,13 @@ fn receive_safe(self: *Self, from: tp.pid_ref, m: tp.message) !void {
return;
}
if (try m.match(.{"AUTO_RUN"})) {
if (self.auto_run_timer) |*t| t.deinit();
self.auto_run_timer = null;
self.auto_run();
return;
}
if (try m.match(.{ "fontface", "done" })) {
return self.enter_overlay_mode(@import("mode/overlay/fontface_palette.zig").Type);
}