From 7005bb237c40c2968c86e8364a43326b67321f54 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 19 Jun 2024 07:11:02 -0500 Subject: [PATCH] widgets(terminal): add initial_working_directory option --- examples/vt.zig | 1 + src/widgets/terminal/Command.zig | 6 ++++++ src/widgets/terminal/Terminal.zig | 2 ++ 3 files changed, 9 insertions(+) diff --git a/examples/vt.zig b/examples/vt.zig index 569681e..40d0ed7 100644 --- a/examples/vt.zig +++ b/examples/vt.zig @@ -45,6 +45,7 @@ pub fn main() !void { .y_pixel = 0, }, .scrollback_size = 0, + .initial_working_directory = env.get("HOME") orelse @panic("no $HOME"), }; const shell = env.get("SHELL") orelse "bash"; const argv = [_][]const u8{shell}; diff --git a/src/widgets/terminal/Command.zig b/src/widgets/terminal/Command.zig index e9e8dc8..84f86b8 100644 --- a/src/widgets/terminal/Command.zig +++ b/src/widgets/terminal/Command.zig @@ -9,6 +9,8 @@ const posix = std.posix; argv: []const []const u8, +working_directory: ?[]const u8, + // Set after spawn() pid: ?std.posix.pid_t = null, @@ -44,6 +46,10 @@ pub fn spawn(self: *Command, allocator: std.mem.Allocator) !void { posix.close(self.pty.tty); if (self.pty.pty > 2) posix.close(self.pty.pty); + if (self.working_directory) |wd| { + try std.posix.chdir(wd); + } + // exec const err = std.posix.execvpeZ(argv_buf.ptr[0].?, argv_buf.ptr, envp); _ = err catch {}; diff --git a/src/widgets/terminal/Terminal.zig b/src/widgets/terminal/Terminal.zig index 59a2497..9cda111 100644 --- a/src/widgets/terminal/Terminal.zig +++ b/src/widgets/terminal/Terminal.zig @@ -32,6 +32,7 @@ const log = std.log.scoped(.terminal); pub const Options = struct { scrollback_size: usize = 500, winsize: Winsize = .{ .rows = 24, .cols = 80, .x_pixel = 0, .y_pixel = 0 }, + initial_working_directory: ?[]const u8 = null, }; pub const Mode = struct { @@ -97,6 +98,7 @@ pub fn init( .argv = argv, .env_map = env, .pty = pty, + .working_directory = opts.initial_working_directory, }; var tabs = try std.ArrayList(u16).initCapacity(allocator, opts.winsize.cols / 8); var col: u16 = 0;