summaryrefslogtreecommitdiff
path: root/src/cli/ssh_cache.zig
diff options
context:
space:
mode:
authorJason Rayne <yo@arcayne.dev>2025-06-25 12:47:38 -0700
committerJason Rayne <yo@arcayne.dev>2025-06-25 15:46:18 -0700
commit0565ed39546a982b7f47d0dd0ba341bc41cc092f (patch)
treeb2cc6fa4f0609c4befe649b30afcbb864aff15a6 /src/cli/ssh_cache.zig
parent6789b7fb6e459b8ca143b946fd0f4f7e6d8fda34 (diff)
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.
Diffstat (limited to 'src/cli/ssh_cache.zig')
-rw-r--r--src/cli/ssh_cache.zig71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/cli/ssh_cache.zig b/src/cli/ssh_cache.zig
new file mode 100644
index 000000000..c3484735a
--- /dev/null
+++ b/src/cli/ssh_cache.zig
@@ -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
+}