summaryrefslogtreecommitdiff
path: root/src/host_io.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-29 22:38:16 -0300
committerGabriel Schneider <[email protected]>2026-10-01 00:12:17 -0300
commit6f42f51cd652e189408eace598f99ec25b9542f8 (patch)
tree633d074c637cc25f3cb851d88414ff9e982e6fa6 /src/host_io.zig
parent7e8e2655567cab1c28597f44d0aec79e46b0deb8 (diff)
downloadpardes-6f42f51cd652e189408eace598f99ec25b9542f8.tar.gz
pardes-6f42f51cd652e189408eace598f99ec25b9542f8.zip
The hosts' Save, Dump and DumpThemes writes and their shell reaper are host_io's, once, where the tty, the gui and the detached session each had a copy
writeFile, writeDump and dumpThemes were byte-for-byte the same in tty.zig, gui.zig and detached/server.zig but for how each reaches its core, and the gui's and the session's reapers the same but for a pid <= 0 against == 0 (a pid is never negative there). Each vtable entry is now a line calling host_io's. ttyTaken, fgName, killJob and ptyResize stay each shell's: they are already one call to host_io each, and what differs is how a shell reaches its pty (an optional with a file, an optional with an fd, a bounds-checked array). macos.zig keeps its copies (no macOS build here to check it). No behaviour changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/host_io.zig')
-rw-r--r--src/host_io.zig52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/host_io.zig b/src/host_io.zig
index 05ccca09..ac25e63c 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -5,6 +5,7 @@ const libc = std.c;
const pardes = @import("pardes.zig");
const ninep_io = @import("9p_io.zig");
const filesystem = @import("fs.zig");
+const file_watch = @import("file_watch.zig");
pub const Host = struct {
ctx: ?*anyopaque = null,
@@ -2522,3 +2523,54 @@ test "a full-screen program takes the tty until it quits" {
}
if (ran == 0) return error.SkipZigTest;
}
+
+// ---- what every host's vtable does alike ----
+
+/// A Save's write: the file written through the core's filesystem, the
+/// pane's watch told the new text is the editor's own (so it is no outside
+/// change), and "saved" said on the pane.
+pub fn writeFile(core: *pardes.Pardes, watches: *file_watch.Table, pane: u8, path: []const u8, bytes: []const u8) void {
+ filesystem.write(core, path, bytes) catch |err|
+ return core.saveFailed(pane, path, err);
+ if (core.panes[pane]) |pn| if (pn.file) |f| if (std.mem.eql(u8, f.path, path)) {
+ if (watches[pane]) |*w| if (w.serial == pn.serial) switch (w.generation) {
+ .text => w.generation = .{ .text = std.hash.Wyhash.hash(0, bytes) },
+ .pdf => {},
+ };
+ };
+ var mbuf: [256]u8 = undefined;
+ core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "saved", path));
+}
+
+/// A Dump's write, into the DumpDir.
+pub fn writeDump(core: *pardes.Pardes, bytes: []const u8) void {
+ var pbuf: [1024:0]u8 = undefined;
+ const path = pardes.dump.outPath(&pbuf, core.settings.dump_dir.get()) orelse return core.dumpFailed(core.settings.dump_dir.get(), error.NoDumpDirectory);
+ filesystem.write(core, path, bytes) catch |err| return core.dumpFailed(path, err);
+ core.setLastDump(path);
+}
+
+/// DumpThemes: the built-in themes written below the config directory.
+pub fn dumpThemes(core: *pardes.Pardes, io: std.Io, gpa: std.mem.Allocator, pane: u8) void {
+ const config_dir = core.opts.config_dir orelse return;
+ const out_dir = pardes.config.User.dumpThemes(io, gpa, config_dir, pardes.themes) catch |err| {
+ core.reportError(pane, "dump themes", err);
+ return;
+ };
+ defer gpa.free(out_dir);
+ var mbuf: [256]u8 = undefined;
+ core.setMessage(pane, pardes.Messages.Message.stamp(&mbuf, "dumped themes", out_dir));
+}
+
+/// A retired shell reaped once it has exited, killed once its grace is up.
+pub fn reapRetired(pid: *std.c.pid_t, kill_at: *i64, now: i64) void {
+ if (pid.* <= 0) return;
+ const result = std.c.waitpid(pid.*, null, std.c.W.NOHANG);
+ if (result > 0 or (result < 0 and std.c.errno(result) == .CHILD)) {
+ pid.* = 0;
+ kill_at.* = 0;
+ } else if (kill_at.* != 0 and now >= kill_at.*) {
+ _ = std.c.kill(pid.*, std.c.SIG.KILL);
+ kill_at.* = 0;
+ }
+}