magdalena

Log | Files | Refs | README | LICENSE

commit 85c09f10232b26acc41a65f71074a056c56f4192
parent 21c3a6f2c5b42e9206109bd04e55d9f3d597e342
Author: mtmn <miro@haravara.org>
Date:   Sat,  9 May 2026 15:24:45 +0200

fix: exec into opener instead of waiting on it

Diffstat:
Msrc/fzf.zig | 76+++++++++++++++++++++-------------------------------------------------------
1 file changed, 21 insertions(+), 55 deletions(-)

diff --git a/src/fzf.zig b/src/fzf.zig @@ -22,68 +22,34 @@ fn gotoFile(allocator: std.mem.Allocator, db: ?*hist.Db, file_path: []const u8, if (command != null) break; } - const tty = std.fs.openFileAbsolute("/dev/tty", .{ .mode = .read_write }) catch |err| blk: { - io.warn("failed to open /dev/tty: {}\n", .{err}); - break :blk null; - }; - defer if (tty) |t| t.close(); - - const saved_stdin = std.posix.dup(std.posix.STDIN_FILENO) catch |err| blk: { - io.warn("failed to save stdin: {}\n", .{err}); - break :blk null; - }; - defer if (saved_stdin) |fd| std.posix.close(fd); - const saved_stdout = std.posix.dup(std.posix.STDOUT_FILENO) catch |err| blk: { - io.warn("failed to save stdout: {}\n", .{err}); - break :blk null; - }; - defer if (saved_stdout) |fd| std.posix.close(fd); - - if (tty) |t| { - std.posix.dup2(t.handle, std.posix.STDIN_FILENO) catch |err| io.warn("failed to redirect stdin to tty: {}\n", .{err}); - std.posix.dup2(t.handle, std.posix.STDOUT_FILENO) catch |err| io.warn("failed to redirect stdout to tty: {}\n", .{err}); + if (db) |d| { + d.logFile(file_path, ext, action) catch |err| io.warn("failed to log file: {}\n", .{err}); } - defer { - if (saved_stdin) |fd| std.posix.dup2(fd, std.posix.STDIN_FILENO) catch |err| io.warn("failed to restore stdin: {}\n", .{err}); - if (saved_stdout) |fd| std.posix.dup2(fd, std.posix.STDOUT_FILENO) catch |err| io.warn("failed to restore stdout: {}\n", .{err}); + + if (std.fs.openFileAbsolute("/dev/tty", .{ .mode = .read_write })) |tty| { + std.posix.dup2(tty.handle, std.posix.STDIN_FILENO) catch |err| io.warn("failed to redirect stdin to tty: {}\n", .{err}); + std.posix.dup2(tty.handle, std.posix.STDOUT_FILENO) catch |err| io.warn("failed to redirect stdout to tty: {}\n", .{err}); + tty.close(); + } else |err| { + io.warn("failed to open /dev/tty: {}\n", .{err}); } - const term = if (command) |cmd| blk: { - var child = std.process.Child.init(&.{ cmd, file_path }, allocator); - child.stdin_behavior = .Inherit; - child.stdout_behavior = .Inherit; - child.stderr_behavior = .Inherit; - break :blk try child.spawnAndWait(); - } else blk: { - const editor = config.editor orelse - std.process.getEnvVarOwned(allocator, "EDITOR") catch |err| switch (err) { - error.EnvironmentVariableNotFound => return error.EditorNotSet, - else => return err, - }; - defer if (config.editor == null) allocator.free(editor); - - if (line) |l| { - const line_arg = try std.fmt.allocPrint(allocator, "+{s}", .{l}); - defer allocator.free(line_arg); - var child = std.process.Child.init(&.{ editor, line_arg, file_path }, allocator); - child.stdin_behavior = .Inherit; - child.stdout_behavior = .Inherit; - child.stderr_behavior = .Inherit; - break :blk try child.spawnAndWait(); - } + if (command) |cmd| { + return std.process.execv(allocator, &.{ cmd, file_path }); + } - var child = std.process.Child.init(&.{ editor, file_path }, allocator); - child.stdin_behavior = .Inherit; - child.stdout_behavior = .Inherit; - child.stderr_behavior = .Inherit; - break :blk try child.spawnAndWait(); + const editor = config.editor orelse + std.process.getEnvVarOwned(allocator, "EDITOR") catch |err| switch (err) { + error.EnvironmentVariableNotFound => return error.EditorNotSet, + else => return err, }; - if (term == .Exited and term.Exited == 0) { - if (db) |d| { - try d.logFile(file_path, ext, action); - } + if (line) |l| { + const line_arg = try std.fmt.allocPrint(allocator, "+{s}", .{l}); + return std.process.execv(allocator, &.{ editor, line_arg, file_path }); } + + return std.process.execv(allocator, &.{ editor, file_path }); } const Fzf = struct {