diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:32:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:21:29 -0300 |
| commit | e38820811c306e7575e87d3fe9a84c7a1503f7d9 (patch) | |
| tree | d1e4adcc312786753e5a7ab91020a5e555103809 | |
| parent | 1a09af0153d9970880e896207a596ed70c11d768 (diff) | |
| download | pardes-e38820811c306e7575e87d3fe9a84c7a1503f7d9.tar.gz pardes-e38820811c306e7575e87d3fe9a84c7a1503f7d9.zip | |
tui: a Screen makes, drops and destroys its own heap copy (Screen.create, drop, destroy) where attachTerminal and resumeTerminals each built one inline and dropScreen repeated freeScreen; a Restore closes each pty through closePty instead of a copy of it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | src/tui/tui.zig | 102 |
1 files changed, 45 insertions, 57 deletions
diff --git a/src/tui/tui.zig b/src/tui/tui.zig index c6a1b844..6030f797 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -1509,6 +1509,40 @@ const Screen = struct { fn setStub(sc: *Screen, fd: posix.fd_t) void { if (sc.primary) daemon.stub = fd else sc.stub = fd; } + + /// Another terminal's Screen, on the heap with a buffer of its own + /// (`destroy` lets both go); null with no room for either. + fn create(io: std.Io, gpa: std.mem.Allocator, env: *std.process.Environ.Map) ?*Screen { + const sc = gpa.create(Screen) catch return null; + const buf = gpa.alloc(u8, 0x10000) catch { + gpa.destroy(sc); + return null; + }; + sc.* = .{ .io = io, .gpa = gpa, .env = env, .buf = buf, .kitty = .init(gpa) }; + return sc; + } + + /// The terminal put back and its stub told; an attached one's Screen + /// freed (the session's own stays, a view of none). + fn drop(sc: *Screen, how: Shell.Leaving) void { + sc.leave(); + if (sc.primary) return sc.sayDetached(); + switch (how) { + .detach => sc.sayDetached(), + .exit => { + daemon.tellExit(sc.stub, 0); + sc.stub = -1; + }, + } + sc.destroy(); + } + + fn destroy(sc: *Screen) void { + sc.glide.deinit(sc.gpa); + sc.kitty.deinit(); + sc.gpa.free(sc.buf); + sc.gpa.destroy(sc); + } }; /// SIGTERM or SIGHUP: each terminal put back as a clean exit leaves it -- @@ -1626,7 +1660,7 @@ fn localSession( // its stub told. defer for (&sh.terms) |*slot| if (slot.*) |sc| if (!sc.primary) { stopInput(io, &sc.thread); - sh.dropScreen(sc, .exit); + sc.drop(.exit); slot.* = null; }; defer { @@ -1749,13 +1783,7 @@ fn localSession( } sh.pipe_tasks.cancelAll(io); for (&sh.gens) |*generation| generation.* +%= 1; - for (&sh.ptys) |*slot| if (slot.*) |*pt| { - pt.reader.cancel(io) catch {}; - _ = libc.close(pt.file.handle); - daemon.closed(pt.pid); - host_io.retireShell(pt.pid); - slot.* = null; - }; + for (0..pardes.MAX_PANES) |pane| Shell.closePty(&sh, @intCast(pane)); for (0..pardes.MAX_PANES) |wid| file_watch.watchPane( sh.inotify_fd, &sh.watches, @@ -1770,7 +1798,7 @@ fn localSession( const live = sh.term().?; for (&sh.terms) |*slot| if (slot.*) |sc| if (sc != live) { stopInput(io, &sc.thread); - sh.dropScreen(sc, .detach); + sc.drop(.detach); slot.* = null; }; sh.terms = @splat(null); @@ -2029,28 +2057,7 @@ const Shell = struct { stopInput(s.io, &sc.thread); s.terms[k] = null; s.core.closeView(k); - s.dropScreen(sc, how); - } - - /// A terminal put back and its stub told; an attached one's Screen freed - /// (the session's own stays, a view of none). - fn dropScreen(s: *Shell, sc: *Screen, how: Leaving) void { - if (sc.primary) { - sc.leave(); - return sc.sayDetached(); - } - sc.leave(); - switch (how) { - .detach => sc.sayDetached(), - .exit => { - daemon.tellExit(sc.stub, 0); - sc.stub = -1; - }, - } - sc.glide.deinit(s.gpa); - sc.kitty.deinit(); - s.gpa.free(sc.buf); - s.gpa.destroy(sc); + sc.drop(how); } fn leaveTerminal(s: *Shell) void { @@ -2081,32 +2088,24 @@ const Shell = struct { _ = libc.close(fd); return refuse(stub, "that session has no terminal of its own to attach to"); }; - const screen = if (!live.on) live else extra: { - const sc = s.gpa.create(Screen) catch break :extra null; - const buf = s.gpa.alloc(u8, 0x10000) catch { - s.gpa.destroy(sc); - break :extra null; - }; - sc.* = .{ .io = s.io, .gpa = s.gpa, .env = live.env, .buf = buf, .kitty = .init(s.gpa) }; - break :extra sc; - } orelse { + const screen = if (!live.on) live else Screen.create(s.io, s.gpa, live.env) orelse { _ = libc.close(fd); return refuse(stub, "that session has no room for another terminal"); }; const extra = screen != live; const tty = Screen.ttyOn(s.io, screen.buf, fd, cooked) catch { _ = libc.close(fd); - if (extra) s.freeScreen(screen) else daemon.setState(true); + if (extra) screen.destroy() else daemon.setState(true); return refuse(stub, "that terminal would not go raw"); }; screen.enter(tty) catch { - if (extra) s.freeScreen(screen) else daemon.setState(true); + if (extra) screen.destroy() else daemon.setState(true); return refuse(stub, "that terminal would not take the screen"); }; if (extra) { const k = s.core.openView() orelse { screen.leave(); - s.freeScreen(screen); + screen.destroy(); return refuse(stub, "that session has as many terminals as it takes"); }; screen.feed.view = k; @@ -2120,12 +2119,6 @@ const Shell = struct { daemon.setState(false); } - fn freeScreen(s: *Shell, sc: *Screen) void { - sc.kitty.deinit(); - s.gpa.free(sc.buf); - s.gpa.destroy(sc); - } - /// A terminal refused: its stub told why (a test's has none). fn refuse(stub: posix.fd_t, why: []const u8) void { if (stub >= 0) daemon.refuseStub(stub, why); @@ -2274,18 +2267,13 @@ const Shell = struct { continue; } const sc = if (t.primary) primary else extra: { - const sc = s.gpa.create(Screen) catch break :extra null; - const buf = s.gpa.alloc(u8, 0x10000) catch { - s.gpa.destroy(sc); - break :extra null; - }; - sc.* = .{ .io = s.io, .gpa = s.gpa, .env = primary.env, .buf = buf, .kitty = .init(s.gpa) }; + const sc = Screen.create(s.io, s.gpa, primary.env) orelse break :extra null; const tty = Screen.ttyOn(s.io, sc.buf, t.fd, t.termios) catch { - s.freeScreen(sc); + sc.destroy(); break :extra null; }; sc.enter(tty) catch { - s.freeScreen(sc); + sc.destroy(); break :extra null; }; sc.setStub(t.stub); |
