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, url: []const u8,
) !void { ) !void {
const cmd: OpenCommand = switch (builtin.os.tag) { const cmd: OpenCommand = switch (builtin.os.tag) {
.linux => .{ .child = std.process.Child.init( .linux => try determineOpenCommandLinux(alloc, url),
&.{ "xdg-open", url },
alloc,
) },
.windows => .{ .child = std.process.Child.init( .windows => .{ .child = std.process.Child.init(
&.{ "rundll32", "url.dll,FileProtocolHandler", url }, &.{ "rundll32", "url.dll,FileProtocolHandler", url },
alloc, alloc,
) }, ) },
.macos => try determineOpenCommandMacOS(alloc, typ, url),
.macos => .{
.child = std.process.Child.init(
switch (typ) {
.text => &.{ "open", "-t", url },
.unknown => &.{ "open", url },
},
alloc,
),
.wait = true,
},
.ios => return error.Unimplemented, .ios => return error.Unimplemented,
else => @compileError("unsupported OS"), 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 { const OpenCommand = struct {
child: std.process.Child, child: std.process.Child,
wait: bool = false, wait: bool = false,