From 9d9bf1dc2ae4e322efb44f3be837ccadd8480238 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 2 Mar 2026 10:49:00 +0100 Subject: [PATCH] WIP: merge focus_terminal and open_terminal commands --- src/keybind/builtin/flow.json | 4 ++-- src/tui/home.zig | 4 ++-- src/tui/mainview.zig | 26 +++++++++++++++++++------- src/tui/terminal_view.zig | 11 +++++++++++ src/tui/tui.zig | 3 +++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/keybind/builtin/flow.json b/src/keybind/builtin/flow.json index 4e6b6dbe..269ce510 100644 --- a/src/keybind/builtin/flow.json +++ b/src/keybind/builtin/flow.json @@ -23,7 +23,7 @@ ["ctrl+6", "focus_split", 5], ["ctrl+7", "focus_split", 6], ["ctrl+8", "focus_split", 7], - ["ctrl+`", "focus_terminal"], + ["ctrl+`", "open_terminal"], ["ctrl+j", "toggle_panel"], ["ctrl+shift+j", "toggle_maximize_panel"], ["ctrl+q", "quit"], @@ -596,7 +596,7 @@ ["ctrl+6", "focus_split", 5], ["ctrl+7", "focus_split", 6], ["ctrl+8", "focus_split", 7], - ["ctrl+`", "focus_terminal"], + ["ctrl+`", "unfocus_terminal"], ["ctrl+j", "toggle_panel"], ["ctrl+shift+page_down", "terminal_scroll_down"], ["ctrl+shift+page_up", "terminal_scroll_up"], diff --git a/src/tui/home.zig b/src/tui/home.zig index 40b41c03..478a8b2e 100644 --- a/src/tui/home.zig +++ b/src/tui/home.zig @@ -34,7 +34,7 @@ const style = struct { \\open_recent_project \\find_in_files \\open_command_palette - \\focus_terminal + \\open_terminal \\run_task \\add_task \\open_config @@ -53,7 +53,7 @@ const style = struct { \\open_recent_project \\find_in_files \\open_command_palette - \\focus_terminal + \\open_terminal \\run_task \\add_task \\open_config diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 4633709d..8f8531bd 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -929,7 +929,7 @@ const cmds = struct { else if (self.is_panel_view_showing(terminal_view)) try self.toggle_panel_view(terminal_view, .toggle) else - try focus_terminal(self, .{}); + try open_terminal(self, .{}); } pub const toggle_panel_meta: Meta = .{ .description = "Toggle panel" }; @@ -985,20 +985,32 @@ const cmds = struct { pub const toggle_terminal_view_meta: Meta = .{ .description = "Toggle terminal" }; pub fn open_terminal(self: *Self, ctx: Ctx) Result { - try self.toggle_panel_view_with_args(terminal_view, .enable, ctx); - } - pub const open_terminal_meta: Meta = .{ .description = "Open terminal", .arguments = &.{.string} }; + const have_args = ctx.args.buf.len > 0 and try ctx.args.match(.{ tp.string, tp.more }); + + if (have_args and terminal_view.is_vt_running()) { + var msg: std.Io.Writer.Allocating = .init(self.allocator); + defer msg.deinit(); + try msg.writer.writeAll("terminal is already running '"); + try terminal_view.get_running_cmd(&msg.writer); + try msg.writer.writeAll("'"); + return tp.exit(msg.written()); + } - pub fn focus_terminal(self: *Self, _: Ctx) Result { if (self.get_panel_view(terminal_view)) |vt| { vt.toggle_focus(); } else { - try self.toggle_panel_view(terminal_view, .enable); + try self.toggle_panel_view_with_args(terminal_view, .enable, ctx); if (self.get_panel_view(terminal_view)) |vt| vt.focus(); } } - pub const focus_terminal_meta: Meta = .{ .description = "Open terminal" }; + pub const open_terminal_meta: Meta = .{ .description = "Open terminal" }; + + pub fn unfocus_terminal(self: *Self, _: Ctx) Result { + if (self.get_panel_view(terminal_view)) |vt| + vt.toggle_focus(); + } + pub const unfocus_terminal_meta: Meta = .{}; pub fn close_terminal(self: *Self, _: Ctx) Result { if (self.get_panel_view(terminal_view)) |_| diff --git a/src/tui/terminal_view.zig b/src/tui/terminal_view.zig index d84f8b22..c94532d6 100644 --- a/src/tui/terminal_view.zig +++ b/src/tui/terminal_view.zig @@ -509,6 +509,17 @@ const Vt = struct { }; var global_vt: ?Vt = null; +pub fn is_vt_running() bool { + return global_vt != null; +} + +pub fn get_running_cmd(writer: *std.Io.Writer) std.Io.Writer.Error!void { + const cmd_argv = if (global_vt) |vt| vt.vt.cmd.argv else &.{}; + if (cmd_argv.len > 0) { + _ = argv.write(writer, cmd_argv) catch {}; + } +} + // Platform-specific pty actor: POSIX uses tp.file_descriptor + SIGCHLD, // Windows uses tp.file_stream with IOCP overlapped reads on the ConPTY output pipe. const pty = if (builtin.os.tag == .windows) pty_windows else pty_posix; diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 9622bb8d..60c415c6 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1516,9 +1516,11 @@ const cmds = struct { }; pub fn run_task_in_terminal(self: *Self, ctx: Ctx) Result { + std.log.debug("open_terminal: 1", .{}); const expansion = @import("expansion.zig"); var task: []const u8 = undefined; if (!try ctx.args.match(.{tp.extract(&task)})) return; + std.log.debug("open_terminal: task {s}", .{task}); const args = expansion.expand_cbor(self.allocator, ctx.args.buf) catch |e| switch (e) { error.NotFound => return error.Stop, else => |e_| return e_, @@ -1529,6 +1531,7 @@ const cmds = struct { cmd = task; call_add_task(task); var buf: [tp.max_message_size]u8 = undefined; + std.log.debug("open_terminal: 2", .{}); try command.executeName("open_terminal", try command.fmtbuf(&buf, .{cmd})); } pub const run_task_in_terminal_meta: Meta = .{