refactor: replace ghostty wrapper with proper CLI actions for terminfo cache management

- Add +list-ssh-cache and +clear-ssh-cache CLI actions
- Remove ghostty() wrapper functions from all shell integrations
- Improve variable naming in shell scripts for readability

Addresses @00-kat's feedback about CLI discoverability and naming
consistency. The new CLI actions follow established Ghostty patterns
and are discoverable via `ghostty --help`, while maintaining clean
separation of concerns between shell logic and cache management.
This commit is contained in:
Jason Rayne
2025-06-25 12:47:38 -07:00
parent 6789b7fb6e
commit 0565ed3954
8 changed files with 299 additions and 173 deletions

71
src/cli/ssh_cache.zig Normal file
View File

@@ -0,0 +1,71 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Child = std.process.Child;
/// Get the path to the shared cache script
fn getCacheScriptPath(alloc: Allocator) ![]u8 {
// Use GHOSTTY_RESOURCES_DIR if available, otherwise assume relative path
const resources_dir = std.process.getEnvVarOwned(alloc, "GHOSTTY_RESOURCES_DIR") catch {
// Fallback: assume we're running from build directory
return try alloc.dupe(u8, "src/shell-integration/shared/ghostty-ssh-cache");
};
defer alloc.free(resources_dir);
return try std.fs.path.join(alloc, &[_][]const u8{ resources_dir, "shell-integration", "shared", "ghostty-ssh-cache" });
}
/// List cached hosts by calling the external script
pub fn listCachedHosts(alloc: Allocator, writer: anytype) !void {
const script_path = try getCacheScriptPath(alloc);
defer alloc.free(script_path);
var child = Child.init(&[_][]const u8{ script_path, "list" }, alloc);
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
const stdout = try child.stdout.?.readToEndAlloc(alloc, std.math.maxInt(usize));
defer alloc.free(stdout);
const stderr = try child.stderr.?.readToEndAlloc(alloc, std.math.maxInt(usize));
defer alloc.free(stderr);
_ = try child.wait();
// Output the results regardless of exit code
try writer.writeAll(stdout);
if (stderr.len > 0) {
try writer.writeAll(stderr);
}
// Script handles its own success/error messaging, so we don't need to check exit code
}
/// Clear cache by calling the external script
pub fn clearCache(alloc: Allocator, writer: anytype) !void {
const script_path = try getCacheScriptPath(alloc);
defer alloc.free(script_path);
var child = Child.init(&[_][]const u8{ script_path, "clear" }, alloc);
child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe;
try child.spawn();
const stdout = try child.stdout.?.readToEndAlloc(alloc, std.math.maxInt(usize));
defer alloc.free(stdout);
const stderr = try child.stderr.?.readToEndAlloc(alloc, std.math.maxInt(usize));
defer alloc.free(stderr);
_ = try child.wait();
// Output the results regardless of exit code
try writer.writeAll(stdout);
if (stderr.len > 0) {
try writer.writeAll(stderr);
}
// Script handles its own success/error messaging, so we don't need to check exit code
}