repos / zmx

session persistence for terminal processes
git clone https://github.com/neurosnap/zmx.git

commit
cedbe7f
parent
dd289a7
author
Eric Bower
date
2025-10-15 19:03:42 -0400 EDT
chore: centralize winsize change logic
1 files changed,  +21, -43
M src/daemon.zig
+21, -43
 1@@ -799,38 +799,11 @@ fn handleAttachSession(ctx: *ServerContext, client: *Client, session_name: []con
 2         client.muted = false;
 3 
 4         // Now send TIOCSWINSZ to trigger app (vim) redraw - client will receive it
 5-        if (rows > 0 and cols > 0) {
 6-            var ws = c.struct_winsize{
 7-                .ws_row = rows,
 8-                .ws_col = cols,
 9-                .ws_xpixel = 0,
10-                .ws_ypixel = 0,
11-            };
12-            const result = c.ioctl(session.pty_master_fd, c.TIOCSWINSZ, &ws);
13-            if (result < 0) {
14-                return error.IoctlFailed;
15-            }
16-
17-            // Send in-band size report if mode 2048 is enabled
18-            try sendInBandSizeReportIfEnabled(session, rows, cols);
19-        }
20+        try applyWinsize(session, rows, cols);
21     } else if (!is_reattach and rows > 0 and cols > 0) {
22         // New session: just resize normally
23         try session.vt.resize(session.allocator, cols, rows);
24-
25-        var ws = c.struct_winsize{
26-            .ws_row = rows,
27-            .ws_col = cols,
28-            .ws_xpixel = 0,
29-            .ws_ypixel = 0,
30-        };
31-        const result = c.ioctl(session.pty_master_fd, c.TIOCSWINSZ, &ws);
32-        if (result < 0) {
33-            return error.IoctlFailed;
34-        }
35-
36-        // Send in-band size report if mode 2048 is enabled
37-        try sendInBandSizeReportIfEnabled(session, rows, cols);
38+        try applyWinsize(session, rows, cols);
39     }
40 
41     // Only start PTY reading if not already started
42@@ -878,6 +851,23 @@ fn handlePtyInput(client: *Client, text: []const u8) !void {
43     _ = written;
44 }
45 
46+fn applyWinsize(session: *Session, rows: u16, cols: u16) !void {
47+    if (rows == 0 or cols == 0) return;
48+
49+    var ws = c.struct_winsize{
50+        .ws_row = rows,
51+        .ws_col = cols,
52+        .ws_xpixel = 0,
53+        .ws_ypixel = 0,
54+    };
55+    const result = c.ioctl(session.pty_master_fd, c.TIOCSWINSZ, &ws);
56+    if (result < 0) {
57+        return error.IoctlFailed;
58+    }
59+
60+    try sendInBandSizeReportIfEnabled(session, rows, cols);
61+}
62+
63 fn sendInBandSizeReportIfEnabled(session: *Session, rows: u16, cols: u16) !void {
64     // Check if in-band size reports mode (2048) is enabled
65     if (!session.vt.modes.get(.in_band_size_reports)) {
66@@ -910,20 +900,8 @@ fn handleWindowResize(client: *Client, rows: u16, cols: u16) !void {
67     // Update libghostty-vt terminal size
68     try session.vt.resize(session.allocator, cols, rows);
69 
70-    // Update PTY window size
71-    var ws = c.struct_winsize{
72-        .ws_row = rows,
73-        .ws_col = cols,
74-        .ws_xpixel = 0,
75-        .ws_ypixel = 0,
76-    };
77-    const result = c.ioctl(session.pty_master_fd, c.TIOCSWINSZ, &ws);
78-    if (result < 0) {
79-        return error.IoctlFailed;
80-    }
81-
82-    // Send in-band size report if mode 2048 is enabled
83-    try sendInBandSizeReportIfEnabled(session, rows, cols);
84+    // Update PTY window size and send notifications
85+    try applyWinsize(session, rows, cols);
86 }
87 
88 fn readFromPty(ctx: *ServerContext, client: *Client, session: *Session) !void {