diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:32:07 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:02:08 -0300 |
| commit | 8660de901a08b98cc4668ac433974879a6bff0db (patch) | |
| tree | 6ac6a9604051a161230e626edd6d77ba3d815e4e /src/terminal.zig | |
| parent | 49aadeb500c588c555a35f8649db9eb770f016fe (diff) | |
| download | pardes-8660de901a08b98cc4668ac433974879a6bff0db.tar.gz pardes-8660de901a08b98cc4668ac433974879a6bff0db.zip | |
terminal: create makes its State as one literal, every other field its default, and create and a dump's load share State.wire (the stream over the emulator, its three effects, the PNG decoder) where each set them up field by field; mouseFormatSgr goes, one test's assertion its only reader; ptyDeviceAttrs returns ghostty's device_attributes.Attributes by name where a @typeInfo chain dug the type out, and it, ptyReport and ghostColor are the file's own
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/terminal.zig')
| -rw-r--r-- | src/terminal.zig | 54 |
1 files changed, 19 insertions, 35 deletions
diff --git a/src/terminal.zig b/src/terminal.zig index ca9b50f2..1d97318b 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -307,17 +307,13 @@ pub const State = struct { if (comptime images_enabled) inline for (.{ .primary, .alternate }) |key| { if (out.vt.screens.get(key)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); }; - out.stream = .init(.{ .handler = .{ .inner = out.vt.vtHandler() }, .allocator = gpa }); - out.stream.handler.inner.effects.write_pty = ptyReport; - out.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; - out.stream.handler.inner.effects.size = ptySize; + out.wire(); const reply = parts.reply[0..@min(parts.reply.len, out.reply.len)]; @memcpy(out.reply[0..reply.len], reply); out.reply_len = @intCast(reply.len); out.reply_dropped = parts.reply_dropped; out.mouse_cell = parts.mouse_cell; out.images = if (images_enabled) .{ .petscii = parts.petscii } else {}; - if (comptime images_enabled) TerminalImages.installPngDecoder(); const h = &out.stream.handler; h.phase = parts.phase; h.started = parts.started; @@ -335,6 +331,17 @@ pub const State = struct { const bottom = screen.pages.scrollbar().offset; screen.scroll(.{ .delta_row = @as(isize, @intCast(parts.view)) - @as(isize, @intCast(bottom)) }); } + + /// The stream over the emulator, its answers to the program and its + /// size reports routed here, PNG decoded: what `create` and a dump's + /// load both do once the emulator is in place. + fn wire(s: *State) void { + s.stream = .init(.{ .handler = .{ .inner = s.vt.vtHandler() }, .allocator = s.gpa }); + s.stream.handler.inner.effects.write_pty = ptyReport; + s.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; + s.stream.handler.inner.effects.size = ptySize; + if (comptime images_enabled) TerminalImages.installPngDecoder(); + } }; const GColor = ghostty_vt.color; @@ -757,13 +764,6 @@ pub fn tracksAllMotion(pane: *const Pane) bool { return state.vt.flags.mouse_event == .any; } -/// ...and wants them in SGR (1006) rather than the legacy X10 bytes. -pub fn mouseFormatSgr(pane: *const Pane) bool { - if (comptime !enabled) return false; - const state = pane.terminal orelse return false; - return state.vt.modes.get(.mouse_format_sgr); -} - /// A wheel over the program is the program's: it tracks the mouse, or it is /// on the alternate screen, which has no scrollback, with alternate scroll /// (DECSET 1007, on unless turned off) asking for arrow keys instead. @@ -946,22 +946,10 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { const own = emulatorGpa(gpa); const state = try own.create(State); errdefer own.destroy(state); - state.gpa = own; - state.vt = try emulator(own, cols, rows); - state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = own }); - state.reply_len = 0; - state.changes = 0; - state.read = false; - state.resize_to = null; - // Every field its default (create leaves them what the heap had, which - // a digest then reads: two replicas must agree, src/replica.zig). - state.reply_dropped = 0; - state.mouse_cell = null; - state.images = if (images_enabled) .{} else {}; - if (comptime images_enabled) TerminalImages.installPngDecoder(); - state.stream.handler.inner.effects.write_pty = ptyReport; - state.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; - state.stream.handler.inner.effects.size = ptySize; + // Every other field its default, never what the heap had: a digest + // reads them, and two replicas must agree (src/replica.zig). + state.* = .{ .gpa = own, .vt = try emulator(own, cols, rows), .stream = undefined }; + state.wire(); pane.terminal = state; pane.tty_filter = true; return pane; @@ -1074,7 +1062,6 @@ test "document construction allocates only the pane and has inert terminal defau try std.testing.expectEqual(Scrollbar{}, scrollbar(pane)); try std.testing.expect(!bracketedPaste(pane)); try std.testing.expect(!reportsMouse(pane)); - try std.testing.expect(!mouseFormatSgr(pane)); try std.testing.expect(!promptInputReady(pane)); scrollGrid(pane, 100); followOutput(pane); @@ -3116,7 +3103,7 @@ pub fn palColor(p: *Pardes, idx: u8) pardes.Color { return .{ .index = idx }; } -pub fn ghostColor(p: *Pardes, color: ghostty_vt.Style.Color, is_bg: bool) pardes.Color { +fn ghostColor(p: *Pardes, color: ghostty_vt.Style.Color, is_bg: bool) pardes.Color { return switch (color) { .none => blk: { const t = if (is_bg) p.theme().bg else p.theme().fg; @@ -3166,7 +3153,7 @@ pub fn redo(p: *Pardes, pane: *Pane) void { pane.ensureCursorVisible(); } -pub fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: []const u8) void { +fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: []const u8) void { const state: *State = @alignCast(@fieldParentPtr("vt", handler.terminal)); const room = state.reply.len - state.reply_len; // A cut answer is worse than none: the program would read the start of @@ -3179,10 +3166,7 @@ pub fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: []const u8) state.reply_len += @intCast(data.len); } -const DeviceAttrs = @typeInfo(@typeInfo(@typeInfo( - @FieldType(ghostty_vt.TerminalStream.Handler.Effects, "device_attributes"), -).optional.child).pointer.child).@"fn".return_type.?; -pub fn ptyDeviceAttrs(_: *ghostty_vt.TerminalStream.Handler) DeviceAttrs { +fn ptyDeviceAttrs(_: *ghostty_vt.TerminalStream.Handler) ghostty_vt.device_attributes.Attributes { return .{}; } |
