From 3ca246ceb931fd39c9b8112a46e188eb3f5028ec Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 13 Nov 2024 12:28:26 -0800 Subject: [PATCH] apprt: support a pwd change action --- include/ghostty.h | 7 +++++++ src/Surface.zig | 14 ++++++++++++++ src/apprt/action.zig | 19 +++++++++++++++++++ src/apprt/glfw.zig | 1 + src/apprt/gtk/App.zig | 1 + src/apprt/surface.zig | 3 +++ src/termio/stream_handler.zig | 8 ++++++++ 7 files changed, 53 insertions(+) diff --git a/include/ghostty.h b/include/ghostty.h index f4836f210..cc6cb7cf8 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -430,6 +430,11 @@ typedef struct { const char* title; } ghostty_action_set_title_s; +// apprt.action.Pwd.C +typedef struct { + const char* pwd; +} ghostty_action_pwd_s; + // terminal.MouseShape typedef enum { GHOSTTY_MOUSE_SHAPE_DEFAULT, @@ -552,6 +557,7 @@ typedef enum { GHOSTTY_ACTION_RENDER_INSPECTOR, GHOSTTY_ACTION_DESKTOP_NOTIFICATION, GHOSTTY_ACTION_SET_TITLE, + GHOSTTY_ACTION_PWD, GHOSTTY_ACTION_MOUSE_SHAPE, GHOSTTY_ACTION_MOUSE_VISIBILITY, GHOSTTY_ACTION_MOUSE_OVER_LINK, @@ -576,6 +582,7 @@ typedef union { ghostty_action_inspector_e inspector; ghostty_action_desktop_notification_s desktop_notification; ghostty_action_set_title_s set_title; + ghostty_action_pwd_s pwd; ghostty_action_mouse_shape_e mouse_shape; ghostty_action_mouse_visibility_e mouse_visibility; ghostty_action_mouse_over_link_s mouse_over_link; diff --git a/src/Surface.zig b/src/Surface.zig index 10ecfd8f1..cd23a81da 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -856,6 +856,20 @@ pub fn handleMessage(self: *Surface, msg: Message) !void { }, }, + .pwd_change => |w| { + defer w.deinit(); + + // We always allocate for this because we need to null-terminate. + const str = try self.alloc.dupeZ(u8, w.slice()); + defer self.alloc.free(str); + + try self.rt_app.performAction( + .{ .surface = self }, + .pwd, + .{ .pwd = str }, + ); + }, + .close => self.close(), // Close without confirmation. diff --git a/src/apprt/action.zig b/src/apprt/action.zig index feb2e2ba4..1f954c37c 100644 --- a/src/apprt/action.zig +++ b/src/apprt/action.zig @@ -151,6 +151,9 @@ pub const Action = union(Key) { /// Set the title of the target. set_title: SetTitle, + /// The current working directory has changed for the target terminal. + pwd: Pwd, + /// Set the mouse cursor shape. mouse_shape: terminal.MouseShape, @@ -215,6 +218,7 @@ pub const Action = union(Key) { render_inspector, desktop_notification, set_title, + pwd, mouse_shape, mouse_visibility, mouse_over_link, @@ -417,6 +421,21 @@ pub const SetTitle = struct { } }; +pub const Pwd = struct { + pwd: [:0]const u8, + + // Sync with: ghostty_action_set_pwd_s + pub const C = extern struct { + pwd: [*:0]const u8, + }; + + pub fn cval(self: Pwd) C { + return .{ + .pwd = self.pwd.ptr, + }; + } +}; + /// The desktop notification to show. pub const DesktopNotification = struct { title: [:0]const u8, diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 638f52bab..54c53139c 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -224,6 +224,7 @@ pub const App = struct { .cell_size, .renderer_health, .color_change, + .pwd, => log.info("unimplemented action={}", .{action}), } } diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index fa73c2436..2f76d8064 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -486,6 +486,7 @@ pub fn performAction( .render_inspector, .renderer_health, .color_change, + .pwd, => log.warn("unimplemented action={}", .{action}), } } diff --git a/src/apprt/surface.zig b/src/apprt/surface.zig index 07eb1d466..58faa9633 100644 --- a/src/apprt/surface.zig +++ b/src/apprt/surface.zig @@ -76,6 +76,9 @@ pub const Message = union(enum) { color: terminal.color.RGB, }, + /// The terminal has reported a change in the working directory. + pwd_change: WriteReq, + pub const ReportTitleStyle = enum { csi_21_t, diff --git a/src/termio/stream_handler.zig b/src/termio/stream_handler.zig index ab837cfd4..c0442661b 100644 --- a/src/termio/stream_handler.zig +++ b/src/termio/stream_handler.zig @@ -1123,6 +1123,14 @@ pub const StreamHandler = struct { log.debug("terminal pwd: {s}", .{path}); try self.terminal.setPwd(path); + // Report it to the surface. If creating our write request fails + // then we just ignore it. + if (apprt.surface.Message.WriteReq.init(self.alloc, path)) |req| { + self.surfaceMessageWriter(.{ .pwd_change = req }); + } else |err| { + log.warn("error notifying surface of pwd change err={}", .{err}); + } + // If we haven't seen a title, use our pwd as the title. if (!self.seen_title) { try self.changeWindowTitle(path);