Adding $EDITOR check for open

Allowing for users to set the $EDITOR variable and use it to open a file instead of the default on mac
This commit is contained in:
Justin Bender
2025-01-12 04:22:04 -05:00
committed by GitHub
parent e3b6bb71a0
commit cf09f6d636

View File

@ -19,27 +19,12 @@ pub fn open(
url: []const u8,
) !void {
const cmd: OpenCommand = switch (builtin.os.tag) {
.linux => .{ .child = std.process.Child.init(
&.{ "xdg-open", url },
alloc,
) },
.linux => try determineOpenCommandLinux(alloc, url),
.windows => .{ .child = std.process.Child.init(
&.{ "rundll32", "url.dll,FileProtocolHandler", url },
alloc,
) },
.macos => .{
.child = std.process.Child.init(
switch (typ) {
.text => &.{ "open", "-t", url },
.unknown => &.{ "open", url },
},
alloc,
),
.wait = true,
},
.macos => try determineOpenCommandMacOS(alloc, typ, url),
.ios => return error.Unimplemented,
else => @compileError("unsupported OS"),
};
@ -73,6 +58,33 @@ pub fn open(
}
}
fn determineOpenCommandLinux(alloc: Allocator, url: []const u8) !OpenCommand {
const editor = std.process.getEnvVarOptional("EDITOR") orelse return .{ .child = std.process.Child.init(
&.{ "xdg-open", url },
alloc,
) };
return .{ .child = std.process.Child.init(
&.{ editor, url },
alloc,
) };
}
fn determineOpenCommandMacOS(alloc: Allocator, typ: Type, url: []const u8) !OpenCommand {
const editor = std.process.getEnvVarOptional("EDITOR") orelse return .{ .child = std.process.Child.init(
switch (typ) {
.text => &.{ "open", "-t", url },
.unknown => &.{ "open", url },
},
alloc,
) };
return .{ .child = std.process.Child.init(
&.{ editor, url },
alloc,
) };
}
const OpenCommand = struct {
child: std.process.Child,
wait: bool = false,