mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-14 15:56:13 +03:00
Merge pull request #1233 from mitchellh/shell-alloc
termio: support XDG data dirs greater than 4k for fish shell integration
This commit is contained in:
@ -1137,6 +1137,7 @@ const Subprocess = struct {
|
|||||||
const dir = opts.resources_dir orelse break :shell null;
|
const dir = opts.resources_dir orelse break :shell null;
|
||||||
|
|
||||||
break :shell try shell_integration.setup(
|
break :shell try shell_integration.setup(
|
||||||
|
gpa,
|
||||||
dir,
|
dir,
|
||||||
path,
|
path,
|
||||||
&env,
|
&env,
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
const EnvMap = std.process.EnvMap;
|
const EnvMap = std.process.EnvMap;
|
||||||
const config = @import("../config.zig");
|
const config = @import("../config.zig");
|
||||||
|
|
||||||
@ -14,7 +15,13 @@ pub const Shell = enum {
|
|||||||
/// integrated shell integration. This returns true if shell
|
/// integrated shell integration. This returns true if shell
|
||||||
/// integration was successful. False could mean many things:
|
/// integration was successful. False could mean many things:
|
||||||
/// the shell type wasn't detected, etc.
|
/// the shell type wasn't detected, etc.
|
||||||
|
///
|
||||||
|
/// The allocator is only used for temporary values, so it should
|
||||||
|
/// be given a general purpose allocator. No allocated memory remains
|
||||||
|
/// after this function returns except anything allocated by the
|
||||||
|
/// EnvMap.
|
||||||
pub fn setup(
|
pub fn setup(
|
||||||
|
alloc: Allocator,
|
||||||
resource_dir: []const u8,
|
resource_dir: []const u8,
|
||||||
command_path: []const u8,
|
command_path: []const u8,
|
||||||
env: *EnvMap,
|
env: *EnvMap,
|
||||||
@ -28,7 +35,7 @@ pub fn setup(
|
|||||||
|
|
||||||
const shell: Shell = shell: {
|
const shell: Shell = shell: {
|
||||||
if (std.mem.eql(u8, "fish", exe)) {
|
if (std.mem.eql(u8, "fish", exe)) {
|
||||||
try setupFish(resource_dir, env);
|
try setupFish(alloc, resource_dir, env);
|
||||||
break :shell .fish;
|
break :shell .fish;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,6 +58,7 @@ pub fn setup(
|
|||||||
/// Fish will automatically load configuration in XDG_DATA_DIRS
|
/// Fish will automatically load configuration in XDG_DATA_DIRS
|
||||||
/// "fish/vendor_conf.d/*.fish".
|
/// "fish/vendor_conf.d/*.fish".
|
||||||
fn setupFish(
|
fn setupFish(
|
||||||
|
alloc_gpa: Allocator,
|
||||||
resource_dir: []const u8,
|
resource_dir: []const u8,
|
||||||
env: *EnvMap,
|
env: *EnvMap,
|
||||||
) !void {
|
) !void {
|
||||||
@ -71,17 +79,19 @@ fn setupFish(
|
|||||||
if (env.get("XDG_DATA_DIRS")) |old| {
|
if (env.get("XDG_DATA_DIRS")) |old| {
|
||||||
// We have an old value, We need to prepend our value to it.
|
// We have an old value, We need to prepend our value to it.
|
||||||
|
|
||||||
// We use a 4K buffer to hold our XDG_DATA_DIR value. The stack
|
// We attempt to avoid allocating by using the stack up to 4K.
|
||||||
// on macOS is at least 512K and Linux is 8MB or more. So this
|
// Max stack size is considerably larger on macOS and Linux but
|
||||||
// should fit. If the user has a XDG_DATA_DIR value that is longer
|
// 4K is a reasonable size for this for most cases. However, env
|
||||||
// than this then it will fail... and we will cross that bridge
|
// vars can be significantly larger so if we have to we fall
|
||||||
// when we actually get there. This avoids us needing an allocator.
|
// back to a heap allocated value.
|
||||||
var buf: [4096]u8 = undefined;
|
var stack_alloc = std.heap.stackFallback(4096, alloc_gpa);
|
||||||
const prepended = try std.fmt.bufPrint(
|
const alloc = stack_alloc.get();
|
||||||
&buf,
|
const prepended = try std.fmt.allocPrint(
|
||||||
|
alloc,
|
||||||
"{s}{c}{s}",
|
"{s}{c}{s}",
|
||||||
.{ integ_dir, std.fs.path.delimiter, old },
|
.{ integ_dir, std.fs.path.delimiter, old },
|
||||||
);
|
);
|
||||||
|
defer alloc.free(prepended);
|
||||||
|
|
||||||
try env.put("XDG_DATA_DIRS", prepended);
|
try env.put("XDG_DATA_DIRS", prepended);
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user