diff options
| -rw-r--r-- | core/Core.zig | 12 | ||||
| -rw-r--r-- | core/Effect.zig | 11 | ||||
| -rw-r--r-- | core/ninep/harness.zig | 2 | ||||
| -rw-r--r-- | core/replica.zig | 4 | ||||
| -rw-r--r-- | core/root.zig | 1 | ||||
| -rw-r--r-- | dev/Host.zig | 4 | ||||
| -rw-r--r-- | dev/Ptys.zig | 27 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 2 | ||||
| -rw-r--r-- | dev/host_io.zig | 42 | ||||
| -rw-r--r-- | runtime/macos.zig | 10 | ||||
| -rw-r--r-- | test/process/terminals.zig | 93 |
11 files changed, 155 insertions, 53 deletions
diff --git a/core/Core.zig b/core/Core.zig index 91f647d4..8616aa35 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -2013,6 +2013,16 @@ pub fn sync(p: *Pardes) void { tagline.revealHeader(p); } +/// Pane `id`'s pty as its host is to size it: its grid (or `cells`, a +/// size 9P's pty/ctl gave), in pixels at the cell of the view that sizes +/// it: a program sizes its kitty graphics by them. +pub fn ptySize(p: *const Pardes, id: Pane.Id, cells: ?[2]u16) pardes.PtySize { + const pane = p.panes[id.index()] orelse return .{ .cols = 1, .rows = 1 }; + const c = cells orelse .{ pane.cols, pane.rows }; + const cell = (p.views[pane.sized_by] orelse p.views[p.live].?).cell_pixels; + return .{ .cols = @max(c[0], 1), .rows = @max(c[1], 1), .w_px = std.math.cast(u16, @as(u32, c[0]) * cell.w) orelse 0, .h_px = std.math.cast(u16, @as(u32, c[1]) * cell.h) orelse 0 }; +} + /// The view typing goes into is the one its terminal is sized for /// (`sizes`). fn typedInto(p: *Pardes) void { @@ -2888,7 +2898,7 @@ test "a spawn is for the pane it was made for, not whatever took the slot since" var command: [32]u8 = undefined; var core: *Pardes = undefined; const vtable: Host.VTable = .{ .spawn = spawn }; - fn spawn(_: ?*anyopaque, id: pardes.Pane.Id, _: []const u8) void { + fn spawn(_: ?*anyopaque, id: pardes.Pane.Id, _: []const u8, _: pardes.PtySize) void { const pane = id.index(); spawns += 1; const line = core.panes[pane].?.command.?; diff --git a/core/Effect.zig b/core/Effect.zig index cabfe8ea..abfe4711 100644 --- a/core/Effect.zig +++ b/core/Effect.zig @@ -22,6 +22,10 @@ const Host = pardes.Host; /// to the shell itself when the shell is in the foreground. pub const PtySignal = enum(u8) { int, term, hup, quit, kill }; +/// A pty's size as the core gives it to its host: in cells, and in pixels +/// at the cell of the view that sizes its pane (Core.ptySize). +pub const PtySize = struct { cols: u16, rows: u16, w_px: u16 = 0, h_px: u16 = 0 }; + /// IO the core wants done. Payloads are inline (fixed buffers): effects are /// queued values with no lifetime ties back into the core. pub const WatchMode = enum { reconcile, baseline_disk }; @@ -118,14 +122,15 @@ fn performInner(p: *Pardes, e: Effect) void { defer panes.File.syncTwins(p); switch (e) { // The host reads the pane (its command, its shell) as it forks, - // so the slot must still hold the pane the spawn was for. - .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .spawn, .{ s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice() }) orelse { + // so the slot must still hold the pane the spawn was for. It forks + // at the pane's grid: laid out by the step that asked (`sync`). + .spawn => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .spawn, .{ s.pane, if (s.cwd.len == 0) (p.spawn_dirs[s.pane.index()] orelse "") else s.cwd.slice(), p.ptySize(s.pane, null) }) orelse { p.fallback.spawned[s.pane.index()] = true; }, // A pane with no child is silent: nothing invents output on its // screen, and the bytes are dropped rather than transcribed. .write => |w| _ = p.tape.host(p, .pty_write, .{ w.pane, w.bytes.slice() }), - .resize_pty => |r| _ = p.tape.host(p, .pty_resize, .{ r.pane, r.cols, r.rows }), + .resize_pty => |r| _ = p.tape.host(p, .pty_resize, .{ r.pane, p.ptySize(r.pane, .{ r.cols, r.rows }) }), // A signal for a pane that closed, its slot retaken in the same // step, would reach another pane's child. .signal_pty => |s| if (if (p.panes[s.pane.index()]) |pane| pane.serial != s.serial else true) {} else p.tape.host(p, .pty_signal, .{ s.pane, s.sig }) orelse {}, diff --git a/core/ninep/harness.zig b/core/ninep/harness.zig index cf75d80d..1449ae54 100644 --- a/core/ninep/harness.zig +++ b/core/ninep/harness.zig @@ -153,7 +153,7 @@ pub fn nameAt(list: []const Dirent, want: []const u8) ?Dirent { /// none. pub const starting: pardes.Host.VTable = .{ .spawn = startNothing }; -fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} +fn startNothing(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8, _: pardes.PtySize) void {} pub const FakeTty = struct { taken: bool, diff --git a/core/replica.zig b/core/replica.zig index ae1ae6c3..3aac5e82 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -3298,7 +3298,7 @@ const TestHost = struct { /// A host with ptys whose pump polls for a frame, as a client's does. const PtyHost = struct { - fn resized(_: ?*anyopaque, _: pardes.Pane.Id, _: u16, _: u16) void {} + fn resized(_: ?*anyopaque, _: pardes.Pane.Id, _: pardes.PtySize) void {} fn poll(_: ?*anyopaque) void {} const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .pty_resize = resized, .poll_frame = poll }; fn host() pardes.Host { @@ -4238,7 +4238,7 @@ fn firstOnPair(by_dump: bool) !void { if (pardes.Term.ops == null) return error.SkipZigTest; // a shell const gpa = std.testing.allocator; const Spawns = struct { - fn spawn(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void { + fn spawn(ctx: ?*anyopaque, _: pardes.Pane.Id, _: []const u8, _: pardes.PtySize) void { const n: *u32 = @ptrCast(@alignCast(ctx.?)); n.* += 1; } diff --git a/core/root.zig b/core/root.zig index fb606909..99fb4845 100644 --- a/core/root.zig +++ b/core/root.zig @@ -66,6 +66,7 @@ pub const CellPixels = @import("Event.zig").CellPixels; pub const Event = @import("Event.zig").Event; /// What goes out (Effect.zig). pub const PtySignal = @import("Effect.zig").PtySignal; +pub const PtySize = @import("Effect.zig").PtySize; pub const WatchMode = @import("Effect.zig").WatchMode; pub const effect_path_cap = @import("Effect.zig").effect_path_cap; pub const effect_write_cap = @import("Effect.zig").effect_write_cap; diff --git a/dev/Host.zig b/dev/Host.zig index cb7aefe1..52205518 100644 --- a/dev/Host.zig +++ b/dev/Host.zig @@ -30,9 +30,9 @@ pub const VTable = struct { /// Upgrade: this process exec'd as the pardes installed since, at /// the step's end (upgrade.zig); `pane` hears why if it cannot be. upgrade: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, - spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8) void = null, + spawn: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void = null, pty_write: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, bytes: []const u8) void = null, - pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, cols: u16, rows: u16) void = null, + pty_resize: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, size: pardes.PtySize) void = null, pty_signal: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id, sig: pardes.PtySignal) void = null, close_pty: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) void = null, tty_taken: ?*const fn (ctx: ?*anyopaque, pane: pardes.Pane.Id) bool = null, diff --git a/dev/Ptys.zig b/dev/Ptys.zig index 583e3c38..5b8705ef 100644 --- a/dev/Ptys.zig +++ b/dev/Ptys.zig @@ -45,16 +45,15 @@ pub fn Calls(comptime Ctx: type) type { fn of(ctx: ?*anyopaque) *Ctx { return @ptrCast(@alignCast(ctx.?)); } - pub fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { + pub fn spawn(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const s = of(ctx); - s.ptys.spawn(s.core, id, cwd); + s.ptys.spawn(s.core, id, cwd, size); } pub fn write(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { of(ctx).ptys.write(id, bytes); } - pub fn resize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { - const s = of(ctx); - s.ptys.resize(s.core, id, cols, rows); + pub fn resize(ctx: ?*anyopaque, id: pardes.Pane.Id, size: pardes.PtySize) void { + of(ctx).ptys.resize(id, size); } pub fn signal(ctx: ?*anyopaque, id: pardes.Pane.Id, sig: pardes.PtySignal) void { of(ctx).ptys.signal(id, sig); @@ -82,14 +81,14 @@ pub fn Calls(comptime Ctx: type) type { } /// Pane `id`'s shell, in `cwd` (empty: where its pane's directory says), -/// or the adopted one waiting for it. -pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8) void { +/// at `size`, or the adopted one waiting for it. +pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const pane = id.index(); - if (p.adoptions.take(id)) |a| return p.take(core, pane, a); + if (p.adoptions.take(id)) |a| return p.take(core, pane, a, size); // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = p.slots[pane] != null; - const child = host_io.forkShell(core, pane, p.prompt_rcs, core.shellBin(), cwd, core.view().screen_h, core.view().screen_w) catch |err| { + const child = host_io.forkShell(core, pane, p.prompt_rcs, core.shellBin(), cwd, size) catch |err| { if (running) return core.restartFailed(id, err); return core.shellFailed(id, err); }; @@ -107,7 +106,7 @@ pub fn spawn(p: *Ptys, core: *pardes.Pardes, id: pardes.Pane.Id, cwd: []const u8 /// An adopted shell, running already, as pane `pane`'s: told the size /// twice (a size it never had, then the pane's), which makes it redraw. -fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption) void { +fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption, size: pardes.PtySize) void { p.close(.at(pane)); p.gens[pane] +%= 1; p.slots[pane] = .{ .fd = a.fd, .pid = a.pid }; @@ -117,7 +116,7 @@ fn take(p: *Ptys, core: *pardes.Pardes, pane: u8, a: host_io.Adoption) void { if (host_io.shellCwd(a.pid, &lbuf)) |wd| core.setCwd(.at(pane), wd); const tiny: posix.winsize = .{ .row = 1, .col = 1, .xpixel = 0, .ypixel = 0 }; _ = posix.system.ioctl(a.fd, TIOCSWINSZ, @intFromPtr(&tiny)); - if (core.panes[pane]) |pn| p.resize(core, .at(pane), @max(pn.cols, 1), @max(pn.rows, 1)); + p.resize(.at(pane), size); p.read(core, pane); } @@ -144,9 +143,9 @@ pub fn write(p: *Ptys, id: pardes.Pane.Id, bytes: []const u8) void { if (p.slots[id.index()]) |pt| _ = host_io.writeFd(pt.fd, bytes); } -pub fn resize(p: *Ptys, core: *const pardes.Pardes, id: pardes.Pane.Id, cols: u16, rows: u16) void { +pub fn resize(p: *Ptys, id: pardes.Pane.Id, size: pardes.PtySize) void { const pt = p.slots[id.index()] orelse return; - const ws = host_io.ptyWinsize(core, cols, rows); + const ws = host_io.ptyWinsize(size); _ = posix.system.ioctl(pt.fd, TIOCSWINSZ, @intFromPtr(&ws)); } @@ -290,7 +289,7 @@ fn testing(queue: *Inbox) Ptys { } fn testShell(p: *Ptys, pane: u8) !libc.pid_t { - const child = try host_io.forkShell(null, pane, p.prompt_rcs, "/bin/sh", "", 24, 80); + const child = try host_io.forkShell(null, pane, p.prompt_rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); p.slots[pane] = .{ .fd = child.file.handle, .pid = child.pid }; p.slots[pane].?.reader = try p.queue.startReader(.{ .handle = child.file.handle, .flags = .{ .nonblocking = false } }, .at(pane), p.gens[pane]); return child.pid; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index bfe658f9..d9d38f06 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -7677,7 +7677,7 @@ test "a detached GUI authority follows terminal directory changes with no window defer ptys.deinit(); var no_peers: replica.Peers = .{}; var shell: Shell = .{ .core = core, .io = io, .gpa = gpa, .lsp_allocator = gpa, .ptys = &ptys, .queue = &queue, .watches = undefined, .peers = &no_peers }; - const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try host_io.forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); ptys.slots[0] = .{ .fd = child.file.handle, .pid = child.pid }; try std.testing.expect(host_io.writeFd(child.file.handle, "cd child\n")); const until = std.Io.Clock.awake.now(io).toMilliseconds() + 2_000; diff --git a/dev/host_io.zig b/dev/host_io.zig index 419c4570..f189c8ea 100644 --- a/dev/host_io.zig +++ b/dev/host_io.zig @@ -420,13 +420,8 @@ pub const exec_command_max = pardes.exec.command_max; /// A pane's pty size, in cells and in pixels: a program sizes its kitty /// graphics by the pixels (yazi's previews; pardes nested in a pane). -pub fn ptyWinsize(core: ?*const pardes.Pardes, cols: u16, rows: u16) posix.winsize { - var ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; - if (core) |c| { - ws.xpixel = std.math.cast(u16, @as(u32, cols) * c.view().cell_pixels.w) orelse 0; - ws.ypixel = std.math.cast(u16, @as(u32, rows) * c.view().cell_pixels.h) orelse 0; - } - return ws; +pub fn ptyWinsize(size: pardes.PtySize) posix.winsize { + return .{ .row = size.rows, .col = size.cols, .xpixel = size.w_px, .ypixel = size.h_px }; } pub fn forkShell( @@ -435,8 +430,7 @@ pub fn forkShell( prompt_rcs: *const Shell.PromptFiles, bin: []const u8, cwd: []const u8, - rows: u16, - cols: u16, + size: pardes.PtySize, ) !Child { const native_cwd = filesystem.localPath(cwd) orelse cwd; if (std.mem.indexOfScalar(u8, native_cwd, 0) != null) return error.InvalidPath; @@ -494,7 +488,7 @@ pub fn forkShell( else .terminal; const envp = ChildEnv.build(&env_slots, paging); - const ws = ptyWinsize(core, cols, rows); + const ws = ptyWinsize(size); // The child says here why it could not become the shell (its chdir or // its exec failed, as for a script whose interpreter is not there): the // pipe closes on exec, so the parent reads nothing when the shell runs, @@ -1672,9 +1666,9 @@ test "the child environment replaces the launcher's terminal identity exactly on test "shell spawn rejects invalid directories before creating a child" { const rcs: Shell.PromptFiles = .{}; - try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", 24, 80)); + try std.testing.expectError(error.InvalidPath, forkShell(null, 0, &rcs, "/bin/sh", "/tmp\x00/ignored", .{ .cols = 80, .rows = 24 })); const too_long = @as([4096]u8, @splat('x')); - try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, 24, 80)); + try std.testing.expectError(error.NameTooLong, forkShell(null, 0, &rcs, "/bin/sh", &too_long, .{ .cols = 80, .rows = 24 })); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); @@ -1683,12 +1677,12 @@ test "shell spawn rejects invalid directories before creating a child" { const directory = directory_buf[0..try tmp.dir.realPath(std.testing.io, &directory_buf)]; var path_buf: [4096]u8 = undefined; const file = try std.fmt.bufPrint(&path_buf, "{s}/file", .{directory}); - try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, 24, 80)); + try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", file, .{ .cols = 80, .rows = 24 })); const explicit_file = try std.fmt.bufPrint(&path_buf, "/n/os{s}/file", .{directory}); - try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, 24, 80)); + try std.testing.expectError(error.NotDir, forkShell(null, 0, &rcs, "/bin/sh", explicit_file, .{ .cols = 80, .rows = 24 })); const missing = try std.fmt.bufPrint(&path_buf, "{s}/missing/" ++ (std.mem.asBytes(&@as([50][6]u8, @splat("child/".*)))), .{directory}); try std.testing.expect(missing.len > 256); - try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, 24, 80)); + try std.testing.expectError(error.FileNotFound, forkShell(null, 0, &rcs, "/bin/sh", missing, .{ .cols = 80, .rows = 24 })); } test "a forked shell is told which terminal it is talking to" { @@ -1711,7 +1705,7 @@ test "a forked shell is told which terminal it is talking to" { _ = Shell.setenv("TERM", "dumb", 1); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1739,7 +1733,7 @@ test "shell spawn uses an explicit OS directory longer than 256 bytes" { var explicit_buf: [4096]u8 = undefined; const explicit = try std.fmt.bufPrint(&explicit_buf, "/n/os{s}", .{directory}); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", explicit, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", explicit, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1768,7 +1762,7 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne // A wrapper exec'd over the shell (timeout standing in for 9ns) runs // another, two directories down, in the foreground. { - const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1787,7 +1781,7 @@ test "a terminal is where its shell is: under a wrapper the foreground shell, ne // A job of the shell's own, gone into a/b, leaves the pane where the // shell is. { - const child = try forkShell(null, 0, &rcs, "/bin/sh", start, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", start, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1812,7 +1806,7 @@ test "shell cwd rejects truncation and preserves an owned child path longer than const directory = path_buf[0..try tmp.dir.realPathFile(io, nested, &path_buf)]; try std.testing.expect(directory.len > 1024); const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1839,7 +1833,7 @@ test "a shell whose directory was removed is not named `... (deleted)`" { var path_buf: [4096]u8 = undefined; const directory = path_buf[0..try tmp.dir.realPathFile(io, "gone", &path_buf)]; const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, 24, 80); + const child = try forkShell(null, 0, &rcs, "/bin/sh", directory, .{ .cols = 80, .rows = 24 }); defer { _ = libc.kill(child.pid, libc.SIG.KILL); _ = libc.waitpid(child.pid, null, 0); @@ -1949,7 +1943,7 @@ test "a command pane's child is its shell running the one line, and its exit sta _ = try panes.newCommand(core, id, "", "printf 'said '; exit 7"); while (core.nextEffect()) |_| {} const rcs: Shell.PromptFiles = .{}; - const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer _ = libc.close(child.file.handle); var got: [256]u8 = undefined; var n: usize = 0; @@ -1980,7 +1974,7 @@ test "a command's exit is its process's, told by its watcher, whether or not its const id = core.freeSlot().?; _ = try panes.newCommand(core, id, "", c.line); while (core.nextEffect()) |_| {} - const child = try forkShell(core, id, &rcs, "/bin/sh", "", 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", "", .{ .cols = 80, .rows = 24 }); defer _ = libc.close(child.file.handle); try std.testing.expect(watchExit(child.pid)); var got: ?libc.pid_t = null; @@ -2021,7 +2015,7 @@ test "a command's background job outlives it and its pty, and Kill stops its who const id = core.freeSlot().?; _ = try panes.newCommand(core, id, dir, c.line); while (core.nextEffect()) |_| {} - const child = try forkShell(core, id, &rcs, "/bin/sh", dir, 24, 80); + const child = try forkShell(core, id, &rcs, "/bin/sh", dir, .{ .cols = 80, .rows = 24 }); if (c.kill) { sleepMs(150); signalTty(child.pid, child.file.handle, .term, true); diff --git a/runtime/macos.zig b/runtime/macos.zig index 23d70b27..3221737a 100644 --- a/runtime/macos.zig +++ b/runtime/macos.zig @@ -1649,14 +1649,14 @@ fn hostState(ctx: ?*anyopaque) *State { return @ptrCast(@alignCast(ctx.?)); } -fn spawnShell(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8) void { +fn spawnShell(ctx: ?*anyopaque, id: pardes.Pane.Id, cwd: []const u8, size: pardes.PtySize) void { const pane = id.index(); const st = hostState(ctx); const core = st.core; // The new shell starts before a running one goes: one that cannot // start leaves the running one be (pty/ctl's exec). const running = st.ptys[pane] != null; - const child = host_io.forkShell(core, pane, &st.prompt_rcs, core.shellBin(), cwd, core.view().screen_h, core.view().screen_w) catch |err| { + const child = host_io.forkShell(core, pane, &st.prompt_rcs, core.shellBin(), cwd, size) catch |err| { if (running) return core.restartFailed(id, err); return core.shellFailed(id, err); }; @@ -1685,11 +1685,11 @@ fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { if (st.ptys[pane]) |pt| _ = host_io.writeFd(pt.file.handle, bytes); } -fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, cols: u16, rows: u16) void { +fn ptyResize(ctx: ?*anyopaque, id: pardes.Pane.Id, size: pardes.PtySize) void { const pane = id.index(); const st = hostState(ctx); const pt = st.ptys[pane] orelse return; - const ws: posix.winsize = .{ .row = rows, .col = cols, .xpixel = 0, .ypixel = 0 }; + const ws = host_io.ptyWinsize(size); _ = posix.system.ioctl(pt.file.handle, TIOCSWINSZ, @intFromPtr(&ws)); } @@ -2339,7 +2339,7 @@ test "mac Restore cancels a PTY reader waiting for output capacity" { while (st.core.nextEffect()) |_| {} st.inbox.acceptReader(.at(1), 1); for (0..Inbox.output_capacity) |_| _ = st.inbox.pushOutput(.at(1), 1, try gpa.dupe(u8, "retained output")); - const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", 12, 40); + const child = try host_io.forkShell(null, 0, &st.prompt_rcs, "/bin/sh", "", .{ .cols = 40, .rows = 12 }); st.gens[0] = 1; st.ptys[0] = .{ .file = child.file, .pid = child.pid, .gen = 1, .reader = .{ .any_future = null, .result = {} } }; defer { diff --git a/test/process/terminals.zig b/test/process/terminals.zig index d9c3bcca..151b221b 100644 --- a/test/process/terminals.zig +++ b/test/process/terminals.zig @@ -16,6 +16,7 @@ pub const cases = .{ .{ "terminals: a program's prompt shows and holds still, input sent unsubmitted shows, and ^U takes the screen back to what it was", prompt }, .{ "terminals: what a program prints past a screenful stays in the terminal's body, and its prompt after it shows", history }, .{ "terminals: a control byte through pty/data reaches a program in raw mode, which then carries on", rawKey }, + .{ "terminals: a shell started into a pane runs at the pane's grid, not the screen's: a new terminal, its shell started again by pty/ctl's exec, a finished command pane run again", sizedFork }, }; fn has(hay: []const u8, needle: []const u8) bool { @@ -78,6 +79,98 @@ const Other = struct { } }; +/// What `stty size` in terminal `term` says, beside the grid its pty/ctl +/// reads back, each as "rows cols". +fn sttySize(a: std.mem.Allocator, c: *process.Conn, term: u32) ![2][]const u8 { + var lines = std.mem.splitScalar(u8, try run(a, c, term, "stty size\n"), '\n'); + _ = lines.next(); + return .{ lines.next() orelse "", try grid(a, c, term) }; +} + +/// Pane `n`'s grid, as pty/ctl reads it back ("winsize C R"): "rows cols". +fn grid(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const u8 { + var w = std.mem.tokenizeAny(u8, try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/pty/ctl", .{n})), " \n"); + _ = w.next(); + const cols = w.next() orelse ""; + return std.fmt.allocPrint(a, "{s} {s}", .{ w.next() orelse "", cols }); +} + +/// The lines of pane `n`'s body that are two numbers (`stty size`'s). +fn sizeLines(a: std.mem.Allocator, c: *process.Conn, n: u32) ![]const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + var rows = std.mem.tokenizeScalar(u8, try c.read(a, try std.fmt.allocPrint(a, "/pane/{d}/body", .{n})), '\n'); + while (rows.next()) |row| { + var w = std.mem.tokenizeScalar(u8, std.mem.trim(u8, row, " \r"), ' '); + const r = w.next() orelse continue; + const k = w.next() orelse continue; + if (w.next() != null) continue; + _ = std.fmt.parseInt(u16, r, 10) catch continue; + _ = std.fmt.parseInt(u16, k, 10) catch continue; + try out.append(a, try std.fmt.allocPrint(a, "{s} {s}", .{ r, k })); + } + return out.items; +} + +/// A shell takes runs: its first prompt, or the one started again. +fn prompted(a: std.mem.Allocator, c: *process.Conn, term: u32) !void { + const deadline = eh.nowMs() + 10_000; + while (!std.mem.eql(u8, try run(a, c, term, "true\n"), "exit 0\n")) { + if (eh.nowMs() > deadline) return error.NoPrompt; + process.sleep(50); + } +} + +fn sizedFork(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + var d = try process.Detached.start(gpa, exe, "sized", .{}); + defer d.deinit(); + const f = &d.files; + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const c = try process.Conn.dial(gpa, d.socket()); + defer c.deinit(gpa); + const text = (try serials(a, c))[0]; + try c.write("/exec", "Shell bash\n"); + // A terminal beside the text: its grid is its rect less the tag, never + // the screen's 160x50. + try c.write("/exec", "Term\n"); + const term = try c.newest(gpa); + try prompted(a, c, term); + var got = try sttySize(a, c, term); + try expect(!std.mem.eql(u8, got[1], "50 160")); + try expectEqualStrings(got[1], got[0]); + // Its shell started again: the pane is sized already, and nothing + // resizes it after. + const pid = try run(a, c, term, "echo $$\n"); + try c.write(f.at("/pane/{d}/pty/ctl", .{term}), "exec\n"); + const again_by = eh.nowMs() + 10_000; + while (true) { + const now = run(a, c, term, "echo $$\n") catch ""; + if (std.mem.startsWith(u8, now, "exit 0\n") and !std.mem.eql(u8, now, pid)) break; + if (eh.nowMs() > again_by) return error.NeverStartedAgain; + process.sleep(50); + } + got = try sttySize(a, c, term); + try expectEqualStrings(got[1], got[0]); + // A command pane, then the same line again: the finished pane is run + // again (exec.zig `runCommand`), a pane sized already. (The new one is + // placed a step after its fork: its first size can be the screen's.) + try c.exec(text, "stty size"); + var words = std.mem.tokenizeAny(u8, try c.pane(a, text, "exec"), " \n"); + var last: []const u8 = ""; + while (words.next()) |w| last = w; + const cmd = try std.fmt.parseInt(u32, last, 10); + for (1..3) |k| { + if (k == 2) try c.exec(text, "stty size"); + const by = eh.nowMs() + 10_000; + while ((try sizeLines(a, c, cmd)).len < k) { + if (eh.nowMs() > by) return error.NoSize; + process.sleep(50); + } + if (k == 2) try expectEqualStrings(try grid(a, c, cmd), (try sizeLines(a, c, cmd))[1]); + } +} + fn runFile(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var d = try process.Detached.start(gpa, exe, "run", .{}); defer d.deinit(); |
