╭ changed ────────╮ │src/serve_ssh.zig│ ╰─────────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │38 }; │ │39 │ │40 fn runTuiSession(handler: *const SessionHandler, sess: *ssh.SessionCtx, pty: ssh.PtySize) !void { │ │41 + // runTui owns the terminal, whose deinit restores the client's screen and │ │42 + // runs as the function unwinds — so any error it returns leaves the TUI torn │ │43 + // down and we can surface the failure on the restored screen before exiting. │ │44 + runTui(handler, sess, pty) catch |err| { │ │45 + std.debug.print("ssh tui session failed: {s}\n", .{@errorName(err)}); │ │46 + var buf: [256]u8 = undefined; │ │47 + const msg = std.fmt.bufPrint(&buf, "haxy ssh: {s}\r\n", .{@errorName(err)}) catch "haxy ssh: internal error\r\n";│ │48 + sess.writeBytes(msg) catch {}; │ │49 + try sess.exit(1); │ │50 + return; │ │51 + }; │ │52 + try sess.exit(0); │ │53 +} │ │54 + │ │55 +fn runTui(handler: *const SessionHandler, sess: *ssh.SessionCtx, pty: ssh.PtySize) !void { │ │56 const allocator = sess.allocator; │ │57 const io = sess.io; │ │58 │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────────────────────────────────────────────────╮ │ 78 │ │ 79 var root = try ui.initRoot(allocator, &page, &ui_session); │ │ 80 defer root.deinit(allocator); │ │ 81 + │ │ 82 + var session_writer_buf: [8192]u8 = undefined; │ │ 83 + var session_writer = ssh.SessionWriter.init(sess, &session_writer_buf); │ │ 84 + │ │ 85 + // the terminal's deinit writes leave-alt / show-cursor / disable-mouse and │ │ 86 + // flushes, restoring the client's screen. as a function-scoped defer it runs │ │ 87 + // before runTui returns — on the normal path and on any error — so the │ │ 88 + // caller can write to the client afterward (and sess.exit can close cleanly). │ │ 89 + var terminal = try StreamTerminal.init(allocator, &session_writer.interface, .{ │ │ 90 + .width = effective_pty.width_cells, │ │ 91 + .height = effective_pty.height_cells, │ │ 92 + }); │ │ 93 + defer terminal.deinit(); │ │ 94 │ │ 67 - // terminal and its writer adapter live in a nested block so the deinit │ │ 68 - // (which writes leave-alt / show-cursor / disable-mouse to the channel) │ │ 69 - // fires BEFORE sess.exit closes the channel. otherwise the client's │ │ 70 - // terminal is left in alt-screen mode after disconnect. │ │ 71 - { │ │ 72 - var session_writer_buf: [8192]u8 = undefined; │ │ 73 - var session_writer = ssh.SessionWriter.init(sess, &session_writer_buf); │ │ 95 + var last_size = Size{ .width = 0, .height = 0 }; │ │ 96 + var last_grid = try Grid.init(allocator, last_size); │ │ 97 + defer last_grid.deinit(); │ │ 98 + │ │ 99 + // initial render — user sees the page immediately │ │100 + try root.build(allocator, .{ │ │101 + .min_size = .{ .width = null, .height = null }, │ │102 + .max_size = .{ .width = last_size.width, .height = last_size.height }, │ │103 + }, root.getFocus()); │ │104 + _ = try terminal.render(&root, &last_grid, &last_size); │ │105 │ │ 75 - var terminal = try StreamTerminal.init(allocator, &session_writer.interface, .{ │ │ 76 - .width = effective_pty.width_cells, │ │ 77 - .height = effective_pty.height_cells, │ │ 78 - }); │ │ 79 - defer terminal.deinit(); │ │106 + // event loop. nextEvent blocks until something interesting arrives; │ │107 + // for each event, rebuild the widget tree and re-render so the user │ │108 + // sees the effect of their input on the same iteration. │ │109 + while (!terminal.shouldQuit()) { │ │110 + const event = try sess.nextEvent(); │ │111 + switch (event) { │ │112 + .data => |payload| { │ │113 + defer allocator.free(payload); │ │114 + try terminal.writeBytes(payload); │ │115 + while (terminal.popKey()) |key| { │ │116 + try ui.inputKey(allocator, &root, key, &terminal); │ │117 + } │ │118 + }, │ │119 + .resize => |sz| { │ │120 + terminal.pushResize(.{ .width = sz.width_cells, .height = sz.height_cells });│ │121 + while (terminal.popKey()) |key| { │ │122 + try ui.inputKey(allocator, &root, key, &terminal); │ │123 + } │ │124 + }, │ │125 + .close => terminal.requestQuit(), │ │126 + } │ │127 │ │ 81 - var last_size = Size{ .width = 0, .height = 0 }; │ │ 82 - var last_grid = try Grid.init(allocator, last_size); │ │ 83 - defer last_grid.deinit(); │ │128 + // apply any actions the widgets queued this frame, persisting those │ │129 + // that belong to a logged-in user │ │130 + try ui.Action.dispatchPending(repo_opts, io, allocator, &repo, &ui_session); │ │131 │ │ 85 - // initial render — user sees the page immediately │ │132 try root.build(allocator, .{ │ │133 .min_size = .{ .width = null, .height = null }, │ │134 .max_size = .{ .width = last_size.width, .height = last_size.height }, │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮ │135 }, root.getFocus()); │ │136 _ = try terminal.render(&root, &last_grid, &last_size); │ │ 91 - │ │ 92 - // event loop. nextEvent blocks until something interesting arrives; │ │ 93 - // for each event, rebuild the widget tree and re-render so the user │ │ 94 - // sees the effect of their input on the same iteration. │ │ 95 - while (!terminal.shouldQuit()) { │ │ 96 - const event = try sess.nextEvent(); │ │ 97 - switch (event) { │ │ 98 - .data => |payload| { │ │ 99 - defer allocator.free(payload); │ │100 - try terminal.writeBytes(payload); │ │101 - while (terminal.popKey()) |key| { │ │102 - try ui.inputKey(allocator, &root, key, &terminal); │ │103 - } │ │104 - }, │ │105 - .resize => |sz| { │ │106 - terminal.pushResize(.{ .width = sz.width_cells, .height = sz.height_cells });│ │107 - while (terminal.popKey()) |key| { │ │108 - try ui.inputKey(allocator, &root, key, &terminal); │ │109 - } │ │110 - }, │ │111 - .close => terminal.requestQuit(), │ │112 - } │ │113 - │ │114 - try root.build(allocator, .{ │ │115 - .min_size = .{ .width = null, .height = null }, │ │116 - .max_size = .{ .width = last_size.width, .height = last_size.height }, │ │117 - }, root.getFocus()); │ │118 - _ = try terminal.render(&root, &last_grid, &last_size); │ │119 - } │ │137 } │ │121 - │ │122 - try sess.exit(0); │ │138 } │ │139 │ │140 // --------------------------------------------------------------------------- │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯