From dff7681c30c655b7b30a4af08abf6a7f98de975f Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Fri, 5 Jul 2024 07:36:01 -0500 Subject: [PATCH] widgets(terminal): set working_directory at spawn Fixes #49. If a user has passed an initial working directory, set that as the set that as the terminal's working directory when we spawn the widget. --- src/widgets/terminal/Terminal.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/widgets/terminal/Terminal.zig b/src/widgets/terminal/Terminal.zig index be0f3e5..1bc58ef 100644 --- a/src/widgets/terminal/Terminal.zig +++ b/src/widgets/terminal/Terminal.zig @@ -94,6 +94,10 @@ pub fn init( unicode: *const vaxis.Unicode, opts: Options, ) !Terminal { + // Verify we have an absolute path + if (opts.initial_working_directory) |pwd| { + if (!std.fs.path.isAbsolute(pwd)) return error.InvalidWorkingDirectory; + } const pty = try Pty.init(); try pty.setSize(opts.winsize); const cmd: Command = .{ @@ -160,6 +164,16 @@ pub fn spawn(self: *Terminal) !void { try self.cmd.spawn(self.allocator); + self.working_directory.clearRetainingCapacity(); + if (self.cmd.working_directory) |pwd| { + try self.working_directory.appendSlice(pwd); + } else { + const pwd = std.fs.cwd(); + var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const out_path = try std.os.getFdPath(pwd.fd, &buffer); + try self.working_directory.appendSlice(out_path); + } + { // add to our global list global_vt_mutex.lock();