diff --git a/include/ghostty.h b/include/ghostty.h index 0b70e2549..6b058d78c 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -460,6 +460,7 @@ void ghostty_surface_split_resize(ghostty_surface_t, ghostty_split_resize_direct void ghostty_surface_split_equalize(ghostty_surface_t); bool ghostty_surface_binding_action(ghostty_surface_t, const char *, uintptr_t); void ghostty_surface_complete_clipboard_request(ghostty_surface_t, const char *, void *, bool); +uintptr_t ghostty_surface_pwd(ghostty_surface_t, char *, uintptr_t); ghostty_inspector_t ghostty_surface_inspector(ghostty_surface_t); void ghostty_inspector_free(ghostty_surface_t); diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 724eb395f..9b79fefca 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -1355,6 +1355,25 @@ pub const CAPI = struct { return surface.core_surface.needsConfirmQuit(); } + /// Copies the surface working directory into the provided buffer and + /// returns the copied size. If the buffer is too small, there is no pwd, + /// or there is an error, then 0 is returned. + export fn ghostty_surface_pwd(surface: *Surface, buf: [*]u8, cap: usize) usize { + const pwd_ = surface.core_surface.pwd(global.alloc) catch |err| { + log.warn("error getting pwd err={}", .{err}); + return 0; + }; + const pwd = pwd_ orelse return 0; + defer global.alloc.free(pwd); + + // If the buffer is too small, return no pwd. + if (pwd.len > cap) return 0; + + // Copy into the buffer and return the length + @memcpy(buf[0..pwd.len], pwd); + return pwd.len; + } + /// Tell the surface that it needs to schedule a render export fn ghostty_surface_refresh(surface: *Surface) void { surface.refresh();