Commit Diff


commit - 21c3a6f2c5b42e9206109bd04e55d9f3d597e342
commit + 85c09f10232b26acc41a65f71074a056c56f4192
blob - ddf61bef646fa749ba11af67d28f06350ed14dae
blob + 06a8fe641ef1dad7cda17b5030b883792760561f
--- src/fzf.zig
+++ src/fzf.zig
@@ -22,68 +22,34 @@ fn gotoFile(allocator: std.mem.Allocator, db: ?*hist.D
         if (command != null) break;
     }
 
-    const tty = std.fs.openFileAbsolute("/dev/tty", .{ .mode = .read_write }) catch |err| blk: {
+    if (db) |d| {
+        d.logFile(file_path, ext, action) catch |err| io.warn("failed to log file: {}\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});
-        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 (command) |cmd| {
+        return std.process.execv(allocator, &.{ cmd, file_path });
     }
-    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});
-    }
 
-    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();
-        }
-
-        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 {