Skip to content

Commit

Permalink
Ensure setup_features runs even when shell detection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
liby committed Jan 15, 2025
1 parent 61ff3b7 commit 491af71
Showing 1 changed file with 59 additions and 51 deletions.
110 changes: 59 additions & 51 deletions src/termio/shell_integration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,65 +58,73 @@ pub fn setup(
break :exe std.fs.path.basename(command[0..idx]);
};

const result: ShellIntegration = shell: {
if (std.mem.eql(u8, "bash", exe)) {
// Apple distributes their own patched version of Bash 3.2
// on macOS that disables the ENV-based POSIX startup path.
// This means we're unable to perform our automatic shell
// integration sequence in this specific environment.
//
// If we're running "/bin/bash" on Darwin, we can assume
// we're using Apple's Bash because /bin is non-writable
// on modern macOS due to System Integrity Protection.
if (comptime builtin.target.isDarwin()) {
if (std.mem.eql(u8, "/bin/bash", command)) {
return null;
}
}
const result = try setupShell(alloc_arena, resource_dir, command, env, exe);

const new_command = try setupBash(
alloc_arena,
command,
resource_dir,
env,
) orelse return null;
break :shell .{
.shell = .bash,
.command = new_command,
};
}
// Setup our feature env vars
try setupFeatures(env, features);

if (std.mem.eql(u8, "elvish", exe)) {
try setupXdgDataDirs(alloc_arena, resource_dir, env);
break :shell .{
.shell = .elvish,
.command = try alloc_arena.dupe(u8, command),
};
}
return result;
}

if (std.mem.eql(u8, "fish", exe)) {
try setupXdgDataDirs(alloc_arena, resource_dir, env);
break :shell .{
.shell = .fish,
.command = try alloc_arena.dupe(u8, command),
};
fn setupShell(
alloc_arena: Allocator,
resource_dir: []const u8,
command: []const u8,
env: *EnvMap,
exe: []const u8,
) !?ShellIntegration {
if (std.mem.eql(u8, "bash", exe)) {
// Apple distributes their own patched version of Bash 3.2
// on macOS that disables the ENV-based POSIX startup path.
// This means we're unable to perform our automatic shell
// integration sequence in this specific environment.
//
// If we're running "/bin/bash" on Darwin, we can assume
// we're using Apple's Bash because /bin is non-writable
// on modern macOS due to System Integrity Protection.
if (comptime builtin.target.isDarwin()) {
if (std.mem.eql(u8, "/bin/bash", command)) {
return null;
}
}

if (std.mem.eql(u8, "zsh", exe)) {
try setupZsh(resource_dir, env);
break :shell .{
.shell = .zsh,
.command = try alloc_arena.dupe(u8, command),
};
}
const new_command = try setupBash(
alloc_arena,
command,
resource_dir,
env,
) orelse return null;
return .{
.shell = .bash,
.command = new_command,
};
}

return null;
};
if (std.mem.eql(u8, "elvish", exe)) {
try setupXdgDataDirs(alloc_arena, resource_dir, env);
return .{
.shell = .elvish,
.command = try alloc_arena.dupe(u8, command),
};
}

// Setup our feature env vars
try setupFeatures(env, features);
if (std.mem.eql(u8, "fish", exe)) {
try setupXdgDataDirs(alloc_arena, resource_dir, env);
return .{
.shell = .fish,
.command = try alloc_arena.dupe(u8, command),
};
}

return result;
if (std.mem.eql(u8, "zsh", exe)) {
try setupZsh(resource_dir, env);
return .{
.shell = .zsh,
.command = try alloc_arena.dupe(u8, command),
};
}

return null;
}

test "force shell" {
Expand Down

0 comments on commit 491af71

Please sign in to comment.