Merge pull request #1423 from allancalix/install-cli

Automatically inject ghostty cli to end of $PATH
This commit is contained in:
Mitchell Hashimoto
2024-01-31 18:58:39 -08:00
committed by GitHub
2 changed files with 41 additions and 6 deletions

View File

@ -2,6 +2,12 @@ const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
/// The separator used in environment variables such as PATH.
pub const PATH_SEP = switch (builtin.os.tag) {
.windows => ";",
else => ":",
};
/// Append a value to an environment variable such as PATH.
/// The returned value is always allocated so it must be freed.
pub fn appendEnv(
@ -13,14 +19,9 @@ pub fn appendEnv(
if (current.len == 0) return try alloc.dupe(u8, value);
// Otherwise we must prefix.
const sep = switch (builtin.os.tag) {
.windows => ";",
else => ":",
};
return try std.fmt.allocPrint(alloc, "{s}{s}{s}", .{
current,
sep,
PATH_SEP,
value,
});
}

View File

@ -944,6 +944,40 @@ const Subprocess = struct {
try env.put("COLORTERM", "truecolor");
}
// Add our binary to the path if we can find it.
ghostty_path: {
var exe_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const exe_bin_path = std.fs.selfExePath(&exe_buf) catch |err| {
log.warn("failed to get ghostty exe path err={}", .{err});
break :ghostty_path;
};
const exe_dir = std.fs.path.dirname(exe_bin_path) orelse break :ghostty_path;
log.debug("appending ghostty bin to path dir={s}", .{exe_dir});
// We always set this so that if the shell overwrites the path
// scripts still have a way to find the Ghostty binary when
// running in Ghostty.
try env.put("GHOSTTY_BIN_DIR", exe_dir);
// Append if we have a path. We want to append so that ghostty is
// the last priority in the path. If we don't have a path set
// then we just set it to the directory of the binary.
if (env.get("PATH")) |path| {
// Verify that our path doesn't already contain this entry
var it = std.mem.tokenizeScalar(u8, path, internal_os.PATH_SEP[0]);
while (it.next()) |entry| {
if (std.mem.eql(u8, entry, exe_dir)) break :ghostty_path;
}
try env.put(
"PATH",
try internal_os.appendEnv(alloc, path, exe_dir),
);
} else {
try env.put("PATH", exe_dir);
}
}
// Set environment variables used by some programs (such as neovim) to detect
// which terminal emulator and version they're running under.
try env.put("TERM_PROGRAM", "ghostty");