mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-08-02 14:57:31 +03:00
Merge pull request #1811 from gpanders/open-no-wait-linux
os/open: do not wait for commands which do not terminate
This commit is contained in:
@ -7,34 +7,43 @@ const Allocator = std.mem.Allocator;
|
|||||||
/// Any output on stderr is logged as a warning in the application logs.
|
/// Any output on stderr is logged as a warning in the application logs.
|
||||||
/// Output on stdout is ignored.
|
/// Output on stdout is ignored.
|
||||||
pub fn open(alloc: Allocator, url: []const u8) !void {
|
pub fn open(alloc: Allocator, url: []const u8) !void {
|
||||||
const argv = switch (builtin.os.tag) {
|
// Some opener commands terminate after opening (macOS open) and some do not
|
||||||
.linux => &.{ "xdg-open", url },
|
// (xdg-open). For those which do not terminate, we do not want to wait for
|
||||||
.macos => &.{ "open", url },
|
// the process to exit to collect stderr.
|
||||||
.windows => &.{ "rundll32", "url.dll,FileProtocolHandler", url },
|
const argv, const wait = switch (builtin.os.tag) {
|
||||||
|
.linux => .{ &.{ "xdg-open", url }, false },
|
||||||
|
.macos => .{ &.{ "open", url }, true },
|
||||||
|
.windows => .{ &.{ "rundll32", "url.dll,FileProtocolHandler", url }, false },
|
||||||
.ios => return error.Unimplemented,
|
.ios => return error.Unimplemented,
|
||||||
else => @compileError("unsupported OS"),
|
else => @compileError("unsupported OS"),
|
||||||
};
|
};
|
||||||
|
|
||||||
var exe = std.process.Child.init(argv, alloc);
|
var exe = std.process.Child.init(argv, alloc);
|
||||||
|
|
||||||
// Pipe stdout/stderr so we can collect output from the command
|
if (comptime wait) {
|
||||||
exe.stdout_behavior = .Pipe;
|
// Pipe stdout/stderr so we can collect output from the command
|
||||||
exe.stderr_behavior = .Pipe;
|
exe.stdout_behavior = .Pipe;
|
||||||
var stdout = std.ArrayList(u8).init(alloc);
|
exe.stderr_behavior = .Pipe;
|
||||||
var stderr = std.ArrayList(u8).init(alloc);
|
|
||||||
defer {
|
|
||||||
stdout.deinit();
|
|
||||||
stderr.deinit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 50 KiB is the default value used by std.process.Child.run
|
|
||||||
const output_max_size = 50 * 1024;
|
|
||||||
|
|
||||||
try exe.spawn();
|
try exe.spawn();
|
||||||
try exe.collectOutput(&stdout, &stderr, output_max_size);
|
|
||||||
_ = try exe.wait();
|
|
||||||
|
|
||||||
// If we have any stderr output we log it. This makes it easier for
|
if (comptime wait) {
|
||||||
// users to debug why some open commands may not work as expected.
|
// 50 KiB is the default value used by std.process.Child.run
|
||||||
if (stderr.items.len > 0) std.log.err("open stderr={s}", .{stderr.items});
|
const output_max_size = 50 * 1024;
|
||||||
|
|
||||||
|
var stdout = std.ArrayList(u8).init(alloc);
|
||||||
|
var stderr = std.ArrayList(u8).init(alloc);
|
||||||
|
defer {
|
||||||
|
stdout.deinit();
|
||||||
|
stderr.deinit();
|
||||||
|
}
|
||||||
|
|
||||||
|
try exe.collectOutput(&stdout, &stderr, output_max_size);
|
||||||
|
_ = try exe.wait();
|
||||||
|
|
||||||
|
// If we have any stderr output we log it. This makes it easier for
|
||||||
|
// users to debug why some open commands may not work as expected.
|
||||||
|
if (stderr.items.len > 0) std.log.err("open stderr={s}", .{stderr.items});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user