shell-integration: handle 'bash -c command'

When the -c option is present, then commands are read from the first
non-option argument command string. Our simple implementation assumes
that if we see at least the '-c' option, a command string was given, and
the shell is always considered to be non-interactive - even if the '-i'
(interactive) option is also given.
This commit is contained in:
Jon Parise
2024-05-14 10:40:25 -07:00
parent c1eda22047
commit 016c58cfe4

View File

@ -156,8 +156,6 @@ fn setupBash(
// Some additional cases we don't yet cover:
//
// - If the `c` shell option is set, interactive mode is disabled, so skip
// loading our shell integration.
// - If additional file arguments are provided (after a `-` or `--` flag),
// and the `i` shell option isn't being explicitly set, we can assume a
// non-interactive shell session and skip loading our shell integration.
@ -173,6 +171,12 @@ fn setupBash(
if (iter.next()) |rcfile| {
try env.put("GHOSTTY_BASH_RCFILE", rcfile);
}
} else if (arg.len > 1 and arg[0] == '-' and arg[1] != '-') {
// '-c command' is always non-interactive
if (std.mem.indexOfScalar(u8, arg, 'c') != null) {
return null;
}
try args.append(arg);
} else {
try args.append(arg);
}
@ -297,6 +301,17 @@ test "bash: rcfile" {
}
}
test "bash: -c command" {
const testing = std.testing;
const alloc = testing.allocator;
var env = EnvMap.init(alloc);
defer env.deinit();
try testing.expect(try setupBash(alloc, "bash -c script.sh", ".", &env) == null);
try testing.expect(try setupBash(alloc, "bash -ic script.sh", ".", &env) == null);
}
test "bash: HISTFILE" {
const testing = std.testing;
const alloc = testing.allocator;