summaryrefslogtreecommitdiff
path: root/dev
diff options
context:
space:
mode:
Diffstat (limited to 'dev')
-rw-r--r--dev/Host.zig46
-rw-r--r--dev/file_watch.zig14
-rw-r--r--dev/gui/gui.zig8
-rw-r--r--dev/tui/tui.zig61
4 files changed, 99 insertions, 30 deletions
diff --git a/dev/Host.zig b/dev/Host.zig
index a23c9ef5..cb7aefe1 100644
--- a/dev/Host.zig
+++ b/dev/Host.zig
@@ -1,6 +1,8 @@
+const std = @import("std");
const pardes = @import("core");
const host_io = @import("host_io.zig");
const Lsp = host_io.Lsp;
+const Host = @This();
ctx: ?*anyopaque = null,
vtable: *const VTable = &.{},
@@ -64,3 +66,47 @@ pub const VTable = struct {
/// absent, it runs on the loop.
job: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, serial: u32, token: u32) void = null,
};
+
+// What a call names, gives and gets, derived from the table: the core calls
+// out through its tape, `p.tape.host(p, .pty_write, .{ pane, bytes })`.
+
+/// One of the table's functions, by its field's name: `.pty_write`. The
+/// tape numbers a call by it (the table's order).
+pub const Fn = std.meta.FieldEnum(VTable);
+
+/// What `f` is given after `ctx`, in order.
+pub fn Args(comptime f: Fn) type {
+ const params = @typeInfo(Of(f)).@"fn".param_types;
+ var types: [params.len - 1]type = undefined;
+ for (params[1..], &types) |P, *T| T.* = P.?;
+ return @Tuple(&types);
+}
+
+/// What `f` answers.
+pub fn Ret(comptime f: Fn) type {
+ return @typeInfo(Of(f)).@"fn".return_type.?;
+}
+
+fn Of(comptime f: Fn) type {
+ return @typeInfo(@typeInfo(@FieldType(VTable, @tagName(f))).optional.child).pointer.child;
+}
+
+/// Whether this host has `f`.
+pub fn has(h: Host, comptime f: Fn) bool {
+ return @field(h.vtable.*, @tagName(f)) != null;
+}
+
+/// Which functions it has, a bit each in the table's order.
+pub fn mask(h: Host) u64 {
+ var m: u64 = 0;
+ inline for (@typeInfo(VTable).@"struct".field_names, 0..) |name, i| {
+ if (@field(h.vtable.*, name) != null) m |= @as(u64, 1) << i;
+ }
+ return m;
+}
+
+/// `f(args)` of this host; null: it has no such function.
+pub inline fn call(h: Host, comptime f: Fn, args: Args(f)) ?Ret(f) {
+ const func = @field(h.vtable.*, @tagName(f)) orelse return null;
+ return @call(.auto, func, .{h.ctx} ++ args);
+}
diff --git a/dev/file_watch.zig b/dev/file_watch.zig
index 58229d90..c575f436 100644
--- a/dev/file_watch.zig
+++ b/dev/file_watch.zig
@@ -761,7 +761,7 @@ pub fn identify(io: std.Io, path: []const u8) !Identity {
}
test "a restored file is watched again: one clean takes what its file became since the dump, one unsaved keeps its text" {
- if (comptime !supported or !filesystem.platform_has_fs) return;
+ if (comptime !supported) return;
const io = std.testing.io;
const gpa = std.testing.allocator;
for ([_]bool{ false, true }) |dirty| {
@@ -803,7 +803,7 @@ test "a restored file is watched again: one clean takes what its file became sin
}
test "a Zerox twin's save wakes its other twin's watch, which says nothing: their text is the file's already" {
- if (comptime !supported or !filesystem.platform_has_fs) return;
+ if (comptime !supported) return;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
@@ -835,7 +835,7 @@ test "a Zerox twin's save wakes its other twin's watch, which says nothing: thei
}
test "ordinary file watches still reconcile changes between open and watch" {
- if (comptime !supported or !filesystem.platform_has_fs) return;
+ if (comptime !supported) return;
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
@@ -870,7 +870,7 @@ test "ordinary file watches still reconcile changes between open and watch" {
}
test "a kind's pane whose file changed between its open and its watch opens again from it" {
- if (comptime !supported or !filesystem.platform_has_fs) return;
+ if (comptime !supported) return;
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
@@ -899,7 +899,7 @@ test "a kind's pane whose file changed between its open and its watch opens agai
}
test "explicit OS directory watches refresh prefix-preserving listings" {
- if (comptime !supported or !filesystem.platform_has_fs) return;
+ if (comptime !supported) return;
const gpa = std.testing.allocator;
const core = try pardes.Pardes.init(gpa, .{ .tty_only = true });
defer core.deinit();
@@ -1012,7 +1012,7 @@ test "theme watch reloads valid ZON and keeps the last theme across a bad save"
}
test "a watched file deleted wakes the watch at once, and its pane says so" {
- if (comptime !supported or !filesystem.platform_has_fs or builtin.os.tag != .linux) return;
+ if (comptime !supported or builtin.os.tag != .linux) return;
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
@@ -1045,7 +1045,7 @@ test "a watched file deleted wakes the watch at once, and its pane says so" {
}
test "a session's watches: a marked file's change posted to the queue, the reader ended at the end" {
- if (comptime !supported or !filesystem.platform_has_fs) return;
+ if (comptime !supported) return;
const io = std.testing.io;
const gpa = std.testing.allocator;
var tmp = std.testing.tmpDir(.{});
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig
index 72ad1eed..201c23df 100644
--- a/dev/gui/gui.zig
+++ b/dev/gui/gui.zig
@@ -1343,7 +1343,7 @@ const Client = struct {
defer cl.lock.unlock(cl.io);
for (cl.inbox.items) |msg| switch (msg) {
.unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),
- .checked => {},
+ .checked, .ended, .left => {},
};
cl.inbox.clearRetainingCapacity();
}
@@ -1428,6 +1428,12 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_
.done => {},
.gone => Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{to.session}),
.failed => Said.no(io, "{s}", .{start.said()}),
+ .left => {
+ var said: [600]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes-gui: detached from '{s}', which is still running; come back with `pardes-gui attach {s}`\n", .{ to.session, to.session }) catch "pardes-gui: detached\n") catch {};
+ std.process.exit(0);
+ },
+ .ended => |code| std.process.exit(code),
}
}
diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig
index 86956eb5..e5e28f6e 100644
--- a/dev/tui/tui.zig
+++ b/dev/tui/tui.zig
@@ -700,7 +700,7 @@ const Tty = struct {
while (t.loop.tryEvent() catch null) |ev| switch (ev) {
.said => |msg| switch (msg) {
.unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),
- .checked => {},
+ .checked, .ended, .left => {},
},
.paste => |b| t.sh.gpa.free(@constCast(b)),
else => {},
@@ -789,6 +789,22 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8,
screen.leave();
Said.no(io, "'{s}' let this client go (a Detach in it, or the session's end)", .{session});
},
+ .left => {
+ screen.leave();
+ var said: [600]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: detached from '{s}', which is still running; come back with `pardes attach {s}`\n", .{ session, session }) catch "pardes: detached\n") catch {};
+ std.process.exit(0);
+ },
+ .ended => |code| {
+ screen.leave();
+ // A pair's client is its session's parent: its status once it
+ // is gone, as a terminal's job ends with its program.
+ if (opts.pair != null) {
+ var status: c_int = 0;
+ while (libc.waitpid(@intCast(start.joined.pid), &status, 0) < 0 and libc.errno(-1) == .INTR) {}
+ }
+ std.process.exit(code);
+ },
.failed => {
screen.leave();
Said.no(io, "{s}", .{start.said()});
@@ -949,7 +965,6 @@ const Own = struct {
.test_clock = host_io.testClock(),
.peers = &l.peers,
.ptys = &l.ptys,
- .session = l,
};
const sh = &o.sh;
sh.screen = o.screen;
@@ -1077,9 +1092,6 @@ const Shell = struct {
/// The session's ptys (runtime/session.zig's); a client's, an empty
/// table over an empty queue.
ptys: *host_io.Ptys,
- /// The session's loop (its `watch`, the daemon's notes); a client's
- /// and a test's, none.
- session: ?*Own.Session = null,
/// What `takeQueue` takes off the ptys' queue, and whether the loop
/// was woken for it since it last did (one wake, not one a read ahead
/// of the terminal's keys in the loop's queue).
@@ -1150,17 +1162,9 @@ const Shell = struct {
}
fn host(s: *Shell) host_io.Host {
- return .{ .ctx = s, .vtable = if (comptime pardes.isolated) &isolated_vtable else &vtable };
+ return .{ .ctx = s, .vtable = &vtable };
}
- const isolated_vtable: host_io.Host.VTable = .{
- .wait_input = waitInput,
- .now = clockNow,
- .present = presentFrame,
- .post_present = postPresent,
- .poll_frame = ownFrame,
- };
-
const vtable: host_io.Host.VTable = .{
.wait_input = waitInput,
.now = clockNow,
@@ -1224,10 +1228,10 @@ const Shell = struct {
/// and the shell has its terminal again), the session left running.
fn detach(ctx: ?*anyopaque) void {
const s = of(ctx);
- // A replica client's: its link ends.
+ if (!daemon.kept()) return s.core.reportFailure(s.core.view().active, "Detach: no pardes daemon lists this session (it is its terminal's job)");
+ // A replica client's: its last word, and its link ends.
if (s.peers.detach(s.core.live)) return;
if (s.detached()) return s.core.reportFailure(s.core.view().active, "Detach: detached already (no terminal of its own)");
- if (!daemon.kept()) return s.core.reportFailure(s.core.view().active, "Detach: no pardes daemon lists this session (it is its terminal's job)");
s.leaveTerminal();
}
@@ -1535,10 +1539,7 @@ const Shell = struct {
return true;
}
// The terminal went: a session the daemon keeps carries on
- // without it, to be attached again. Gone before the first
- // frame, the reader that takes the daemon's `ok` (kept
- // waits on it) is not started yet: started here.
- if (s.session) |l| l.watch();
+ // without it, to be attached again.
if (daemon.kept()) {
if (!s.detached()) s.leaveTerminal();
return true;
@@ -1561,10 +1562,10 @@ const Shell = struct {
};
if (echo) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
c.take(said);
- if (said == .refused) return true;
+ if (said == .refused or said == .ended or said == .left) return true;
} else switch (said) {
.unit, .screen, .refused, .blob => |b| std.heap.c_allocator.free(b),
- .checked => {},
+ .checked, .ended, .left => {},
},
.link_gone => if (s.client) |c| {
c.link.quit = true;
@@ -2359,6 +2360,22 @@ test "an ESC read by itself is the Escape key at once" {
t.vx.caps.kitty_keyboard = false;
}
+test "an Escape read with the click after it is the Escape key, then the click" {
+ var t: TestInput = undefined;
+ try t.init();
+ defer t.deinit();
+ // A busy session reads them in one: no terminal sends Alt on a mouse
+ // report as an ESC before it (it is in the report's button).
+ var reader: TestReader = .{ .parts = &.{"\x1b\x1b[<1;105;9M"} };
+ try readInput(&t.loop, &reader, &t.vx, &t.feed);
+ try std.testing.expectEqual(.winsize, std.meta.activeTag((try t.loop.tryEvent()).?));
+ const escape = (try t.loop.tryEvent()).?.key_press;
+ try std.testing.expectEqual(vaxis.Key.escape, escape.codepoint);
+ try std.testing.expect(!escape.mods.alt);
+ try std.testing.expectEqual(.mouse, std.meta.activeTag((try t.loop.tryEvent()).?));
+ try std.testing.expect(try t.loop.tryEvent() == null);
+}
+
test "a frame's answer is taken wherever the reads cut it, and never typed" {
const gpa = std.testing.allocator;
var t: TestInput = undefined;