Append the default value of XDG_DATA_DIRS when setting up shell integration

This commit is contained in:
notcancername
2024-11-17 15:25:36 +01:00
parent 4a71812352
commit 8e736aa4eb

View File

@ -447,27 +447,30 @@ fn setupXdgDataDirs(
// so that our modifications don't interfere with other commands. // so that our modifications don't interfere with other commands.
try env.put("GHOSTTY_SHELL_INTEGRATION_XDG_DIR", integ_dir); try env.put("GHOSTTY_SHELL_INTEGRATION_XDG_DIR", integ_dir);
if (env.get("XDG_DATA_DIRS")) |old| { {
// We have an old value, We need to prepend our value to it.
// We attempt to avoid allocating by using the stack up to 4K. // We attempt to avoid allocating by using the stack up to 4K.
// Max stack size is considerably larger on macOS and Linux but // Max stack size is considerably larger on macOS and Linux but
// 4K is a reasonable size for this for most cases. However, env // 4K is a reasonable size for this for most cases. However, env
// vars can be significantly larger so if we have to we fall // vars can be significantly larger so if we have to we fall
// back to a heap allocated value. // back to a heap allocated value.
var stack_alloc = std.heap.stackFallback(4096, alloc_arena); var stack_alloc_state = std.heap.stackFallback(4096, alloc_arena);
const alloc = stack_alloc.get(); const stack_alloc = stack_alloc_state.get();
const old_value = if (env.get("XDG_DATA_DIRS")) |old|
old
else
// No XDG_DATA_DIRS set, we prepend to the default value.
// <https://specifications.freedesktop.org/basedir-spec/0.6/#variables>
"/usr/local/share:/usr/share";
const prepended = try std.fmt.allocPrint( const prepended = try std.fmt.allocPrint(
alloc, stack_alloc,
"{s}{c}{s}", "{s}{c}{s}",
.{ integ_dir, std.fs.path.delimiter, old }, .{ integ_dir, std.fs.path.delimiter, old_value },
); );
defer alloc.free(prepended); defer stack_alloc.free(prepended);
try env.put("XDG_DATA_DIRS", prepended); try env.put("XDG_DATA_DIRS", prepended);
} else {
// No XDG_DATA_DIRS set, we just set it our desired value.
try env.put("XDG_DATA_DIRS", integ_dir);
} }
} }