diff options
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/Lsp.zig | 14 | ||||
| -rw-r--r-- | dev/file_watch.zig | 6 | ||||
| -rw-r--r-- | dev/gui/gui.zig | 17 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 247 |
4 files changed, 260 insertions, 24 deletions
diff --git a/dev/Lsp.zig b/dev/Lsp.zig index 4b8bfa03..b799c9f6 100644 --- a/dev/Lsp.zig +++ b/dev/Lsp.zig @@ -43,6 +43,14 @@ pub const Job = struct { } }; +/// A pane's file's text, `0`-ended, owned. +fn fileSource(gpa: std.mem.Allocator, f: ?*const pardes.panes.File.State) ![:0]u8 { + const file = f orelse return gpa.dupeSentinel(u8, "", 0); + const text = try pardes.panes.File.fileText(file, gpa); + defer if (text.ptr != file.content.ptr) gpa.free(text); + return gpa.dupeSentinel(u8, text, 0); +} + // Copy before starting a worker; the editor may replace any source slice afterward. pub fn snapshot(gpa: std.mem.Allocator, core: *const pardes.Pardes, req: Request) !*Job { if (req.pane.index() >= core.panes.len) return error.NoPane; @@ -53,7 +61,9 @@ pub fn snapshot(gpa: std.mem.Allocator, core: *const pardes.Pardes, req: Request const declared_path = if (file) |f| f.path else ""; const path = try gpa.dupe(u8, filesystem.localPath(declared_path) orelse declared_path); errdefer gpa.free(path); - const source = try gpa.dupeSentinel(u8, if (file) |f| f.content else "", 0); + // The file's text (File.fileText: the `Changes` layer's lines are none + // of it), as every position sent with it is. + const source = try fileSource(gpa, if (file) |*f| f else null); errdefer gpa.free(source); const arg = try gpa.dupe(u8, req.arg); errdefer gpa.free(arg); @@ -94,7 +104,7 @@ pub fn snapshot(gpa: std.mem.Allocator, core: *const pardes.Pardes, req: Request copied += f.content.len; const local = filesystem.localPath(f.path) orelse continue; const other_path = try gpa.dupe(u8, local); - const other_source = gpa.dupeSentinel(u8, f.content, 0) catch |err| { + const other_source = fileSource(gpa, &f) catch |err| { gpa.free(other_path); return err; }; diff --git a/dev/file_watch.zig b/dev/file_watch.zig index a0a500d7..d97e1d3d 100644 --- a/dev/file_watch.zig +++ b/dev/file_watch.zig @@ -471,7 +471,7 @@ pub fn reloadPane( } // The pane holds the disk's text already (a Zerox twin saved, its // other twin watching the same file): nothing reloaded, nothing said. - if (std.mem.eql(u8, file.content, bytes)) { + if (pardes.panes.File.fileEql(&file, bytes)) { if (watches[id]) |*live| if (live.serial == watched.serial) { live.generation = .{ .text = hash }; }; @@ -481,7 +481,7 @@ pub fn reloadPane( const current = core.panes[id] orelse return false; if (current.serial != watched.serial) return false; const current_file = current.file orelse return false; - if (!std.mem.eql(u8, current_file.content, bytes)) return false; + if (!pardes.panes.File.fileEql(¤t_file, bytes)) return false; if (watches[id]) |*live| { if (live.serial == watched.serial) live.generation = .{ .text = hash }; @@ -545,7 +545,7 @@ pub fn applyEffect( serial = pane.serial; if (pane.file) |file| { path = file.path; - generation = .{ .text = std.hash.Wyhash.hash(0, file.content) }; + generation = .{ .text = pardes.panes.File.fileHash(&file) }; } else if (kindPath(pane)) |kind_path| { path = kind_path; generation = .{ .kind = null }; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 76af0ec8..2756f69b 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -809,9 +809,12 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, detached: bool, di var want_w: c_int = 1120; var want_h: c_int = 720; var want_at: ?[2]c_int = null; - // ...or the size an Upgrade's exec says its window had (client.zig + // ...or the size an Upgrade's exec says its window had: a session's in + // its record (upgrade.zig `Record.size`), ahead of the `--geometry` it + // was started with, a client's in PARDES_JOIN_SIZE (client.zig // `execAgain`). - if (opts_in.geometry orelse if (std.c.getenv("PARDES_JOIN_SIZE")) |e| std.mem.span(e) else null) |spec| geometry: { + const kept = if (upgrade.resumed) |rec| rec.size else null; + if (kept orelse opts_in.geometry orelse if (std.c.getenv("PARDES_JOIN_SIZE")) |e| std.mem.span(e) else null) |spec| geometry: { const x_at = std.mem.indexOfScalar(u8, spec, 'x') orelse break :geometry; const plus = std.mem.indexOfAnyPos(u8, spec, x_at, "+-") orelse spec.len; want_w = std.math.clamp(std.fmt.parseInt(c_int, spec[0..x_at], 10) catch break :geometry, 200, 16384); @@ -881,7 +884,8 @@ fn runNative(init: std.process.Init, opts_in: pardes.Options, detached: bool, di const cell_w: u32 = @intCast(@max(cw, 1)); const cell_h: u32 = @intCast(@max(chh, 1)); const tagline = taglineRaster(&atlas, px, cell_w, cell_h, config.gui_tagline_font_percent); - if (test_mode) { + // An upgraded window keeps its size in a test too. + if (test_mode and kept == null) { const cols = envU16(env, "PARDES_TEST_COLS") orelse 80; const rows = envU16(env, "PARDES_TEST_ROWS") orelse 24; // PARDES_TEST_PAD: extra pixels past the grid, as a real window has. @@ -2535,6 +2539,13 @@ const Shell = struct { // dials the session again once it is back. s.peers.refuseAll(core); var rec: upgrade.Record = .{ .link = daemon.link, .window = s.gui != null, .exe = path }; + // Its window's size now, the new one's (the user's since its start). + var size_buf: [32]u8 = undefined; + if (s.gui) |g| { + var w: c_int = 0; + var h: c_int = 0; + if (c.SDL_GetWindowSize(g.window, &w, &h)) rec.size = std.fmt.bufPrint(&size_buf, "{d}x{d}", .{ w, h }) catch null; + } s.ptys.record(&rec); var at_buf: [1024:0]u8 = undefined; const failed = upgrade.dumpInto(core, &rec, &at_buf, &why) orelse upgrade.execAs(s.gpa, path, &rec, &at_buf, s.fs, &why); diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index d1bbe140..f0011273 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -119,6 +119,9 @@ const Msg = union(enum) { paste: []const u8, paste_start, paste_end, + /// A sequence past max_sequence was dropped (a clipboard's answer past + /// the paste limit): said on the notice line. + reply_dropped, fs_ready, /// A replica's reader posted: the step boundary takes it in. replica_mail, @@ -154,23 +157,61 @@ fn inputReader(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) void { loop.postEvent(.quit) catch {}; } +/// The longest sequence the terminal's input carries whole: a clipboard +/// read's answer (OSC 52) of the paste limit's text, in base64, with its +/// framing. One longer is dropped and said (`reply_dropped`). +const max_sequence: usize = std.base64.standard.Encoder.calcSize(max_paste_bytes) + 16; + +/// A string sequence's start (OSC, DCS, APC, PM, SOS): it ends at a BEL or +/// an ESC, however long it is. +fn isString(head: []const u8) bool { + return head[0] == 0x1b and std.mem.indexOfScalar(u8, "]P_^X", head[1]) != null; +} + fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { try loop.postEvent(.{ .winsize = try tty.getWinsize() }); var parser: vaxis.Parser = .{}; - var buf: [1024]u8 = undefined; + // Grown while one sequence is longer (a clipboard's answer: megabytes), + // up to max_sequence, and back to its first size after. + const gpa = vx.opts.system_clipboard_allocator orelse std.heap.c_allocator; + var buf = try gpa.alloc(u8, 1024); + defer gpa.free(buf); var carried: usize = 0; + // The sequence carried is past max_sequence, and said so. + var dropping = false; while (true) { // stopInput cancels the task; the read then fails with Canceled - if (carried == buf.len) return error.InputSequenceTooLong; + if (carried == buf.len and buf.len < max_sequence) { + if (gpa.realloc(buf, @min(2 * buf.len, max_sequence))) |grown| buf = grown else |_| {} + } + if (carried == buf.len) { + // Dropped, and said once. A string keeps its introducer and a + // byte after it that makes it no reply vaxis knows, so the parse + // still finds its end and passes it over; anything else (a CSI + // of megabytes) is let go where it stands. + if (!dropping) try loop.postEvent(.reply_dropped); + dropping = true; + carried = if (isString(buf)) 3 else 0; + buf[2] = 'x'; + } const received = try tty.read(buf[carried..]); if (received == 0) return; // The terminal's answers to frames (FrameAck) come out first, // wherever they sit: never events, and never glued to a key (an - // Escape pressed just before one would read as Alt on it). - const end = feed.ack.take(buf[0 .. carried + received], loop); + // Escape pressed just before one would read as Alt on it). What + // was carried is clear of them but for one cut across the read. + const from = carried -| (FrameAck.answer.len - 1); + const end = from + feed.ack.take(buf[from .. carried + received], loop); if (end == 0) { carried = 0; continue; } + // A string still arriving (a clipboard's answer in a pty's 4 KiB + // reads) has no end till a BEL or an ESC comes: it is read on, not + // parsed again from its start at every read. + if (carried > 2 and isString(buf) and std.mem.indexOfAny(u8, buf[carried - 1 .. end], "\x07\x1b") == null) { + carried = end; + continue; + } if (feed.shm_probing.load(.acquire)) if (std.mem.indexOf(u8, buf[0..end], std.fmt.comptimePrint("\x1b_Gi={d};", .{kitty_shm_probe_id}))) |at| { feed.shm_probing.store(false, .release); feed.shm.store(std.mem.startsWith(u8, buf[at..end], std.fmt.comptimePrint("\x1b_Gi={d};OK", .{kitty_shm_probe_id})), .release); @@ -217,7 +258,7 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { if (result.event) |event| if (event == .key_press and event.key_press.isModifier()) continue; if (result.event) |event| vaxis.loop.handleEventGeneric(loop, vx, &feed.cache, Msg, event, vx.opts.system_clipboard_allocator) catch |err| { - if (event == .paste) if (vx.opts.system_clipboard_allocator) |gpa| gpa.free(@constCast(event.paste)); + if (event == .paste) if (vx.opts.system_clipboard_allocator) |clip| clip.free(@constCast(event.paste)); return err; }; // The last answer to the startup queries is no event of its @@ -229,7 +270,13 @@ fn readInput(loop: *Loop, tty: anytype, vx: *vaxis.Vaxis, feed: *Feed) !void { }; } carried = end - consumed; - std.mem.copyForwards(u8, buf[0..carried], buf[consumed..end]); + if (consumed > 0) { + std.mem.copyForwards(u8, buf[0..carried], buf[consumed..end]); + dropping = false; + } + if (buf.len > 1024 and carried <= 1024) if (gpa.realloc(buf, 1024)) |first| { + buf = first; + } else |_| {}; } } @@ -333,10 +380,13 @@ const TestInput = struct { }; /// A terminal that hands over `parts`, a read each (an empty one passed -/// over), and says more follows (within `gap_ms`) while some are left. +/// over; one longer than the reader's room in reads of that room, as a tty +/// does), and says more follows (within `gap_ms`) while some are left. const TestReader = struct { parts: []const []const u8, next: usize = 0, + /// How much of `parts[next]` was read. + at: usize = 0, gap_ms: u32 = 0, asked: bool = false, @@ -351,11 +401,15 @@ const TestReader = struct { fn read(r: *TestReader, buf: []u8) !usize { while (r.next < r.parts.len) { - const part = r.parts[r.next]; - r.next += 1; - if (part.len == 0) continue; - @memcpy(buf[0..part.len], part); - return part.len; + const part = r.parts[r.next][r.at..]; + const n = @min(part.len, buf.len); + @memcpy(buf[0..n], part[0..n]); + r.at += n; + if (n == part.len) { + r.next += 1; + r.at = 0; + } + if (n > 0) return n; } return 0; } @@ -1556,7 +1610,7 @@ const Shell = struct { if (!s.peers.recording) return false; switch (ev) { .key_press => if (s.in_paste) return false, - .mouse, .paste, .paste_end, .winsize, .focus_in, .focus_out => {}, + .mouse, .paste, .paste_end, .reply_dropped, .winsize, .focus_in, .focus_out => {}, else => return false, } if (s.took_output or s.took_input) { @@ -1583,14 +1637,14 @@ const Shell = struct { s.frames.input(pardes.FrameStats.nowNs()); s.input_seen = true; }, - .paste, .paste_start, .winsize => s.input_seen = true, + .paste, .paste_start, .reply_dropped, .winsize => s.input_seen = true, else => {}, } if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds); if (event != .replica_mail) s.mail_only = false; // The terminal's input goes into its view. switch (event) { - .key_press, .mouse, .paste, .paste_start, .paste_end, .winsize, .focus_in, .focus_out, .quit => { + .key_press, .mouse, .paste, .paste_start, .paste_end, .reply_dropped, .winsize, .focus_in, .focus_out, .quit => { if (s.local() and s.client == null) core.toView(.at(s.own)); }, else => {}, @@ -1659,7 +1713,8 @@ const Shell = struct { .winsize => |ws| if (!s.detached()) s.resized(s.screen, ws), .key_press => |key| if (s.in_paste) { const bytes = vaxis_input.pasteBytes(key); - const room = max_paste_bytes -| s.paste_buf.written().len; + // A byte past the limit kept: the core cuts there and says so. + const room = max_paste_bytes + 1 -| s.paste_buf.written().len; s.paste_buf.writer.writeAll(bytes[0..@min(bytes.len, room)]) catch {}; } else s.feed(vaxis_input.keyEvent(key)), .mouse => |m| if (mouseEvent(m)) |ev| s.feed(ev), @@ -1677,6 +1732,7 @@ const Shell = struct { if (pasted.len > 0) s.feed(.{ .paste = pasted }); s.paste_buf.clearRetainingCapacity(); }, + .reply_dropped => if (s.client) |c| c.link.notify("{s}", .{reply_dropped_words}) else core.setMessage(.at(core.view().active), reply_dropped_words), .fs_ready => {}, } return false; @@ -2098,6 +2154,7 @@ fn wakeFs(ctx: ?*anyopaque) void { } const max_paste_bytes: usize = pardes.memory.limits.max_paste_bytes; +const reply_dropped_words = std.fmt.comptimePrint("a terminal reply past {d} KiB dropped: a clipboard past {d} KiB is not pasted", .{ max_sequence / 1024, max_paste_bytes / 1024 }); fn mouseEvent(m: vaxis.Mouse) ?pardes.Event { const button: pardes.Mouse.Button = switch (m.button) { @@ -2365,6 +2422,155 @@ test "terminal replies and malformed sequences never become typed text" { try std.testing.expect(alt_up and clicked); } +/// The terminal's answer to a clipboard read (OSC 52) of `text`, between a +/// typed `x` and `y`, ended by `end` (BEL or ST). +fn testClipboardReply(gpa: std.mem.Allocator, text: []const u8, end: []const u8) ![]u8 { + const enc = std.base64.standard.Encoder; + const head = "x\x1b]52;c;"; + const reply = try gpa.alloc(u8, head.len + enc.calcSize(text.len) + end.len + 1); + @memcpy(reply[0..head.len], head); + const b64 = enc.encode(reply[head.len..], text); + @memcpy(reply[head.len + b64.len ..][0..end.len], end); + reply[reply.len - 1] = 'y'; + return reply; +} + +/// The loop's events after a `testClipboardReply`, one word each. +fn testReplyEvents(gpa: std.mem.Allocator, loop: *Loop, text: []const u8) ![]u8 { + var got: std.ArrayList(u8) = .empty; + errdefer got.deinit(gpa); + while (try loop.tryEvent()) |event| switch (event) { + .winsize => {}, + .key_press => |key| try got.print(gpa, "{u} ", .{key.codepoint}), + .paste => |bytes| { + defer gpa.free(@constCast(bytes)); + try got.appendSlice(gpa, if (std.mem.eql(u8, bytes, text)) "clipboard " else "other "); + }, + else => |other| try got.print(gpa, "{t} ", .{other}), + }; + return got.toOwnedSlice(gpa); +} + +test "a clipboard reply of 4 KB or 1 MB is taken, whole, in a pty's reads and cut inside its end, and input goes on after it" { + const gpa = std.testing.allocator; + var t: TestInput = undefined; + try t.init(); + defer t.deinit(); + // 3 KiB and 768 KiB of clipboard: a 4 KiB and a 1 MiB reply. Up to + // 2026-11-23 a reply over 1 KiB of unread input ended it, and the + // session quit (InputSequenceTooLong). + for ([_]usize{ 3 << 10, 768 << 10 }) |len| { + const text = try gpa.alloc(u8, len); + defer gpa.free(text); + for (text, 0..) |*b, i| b.* = @truncate(i *% 131 +% i / 7); + for ([_][]const u8{ "\x07", "\x1b\\" }) |end| { + const reply = try testClipboardReply(gpa, text, end); + defer gpa.free(reply); + // A pty's reads: 4 KiB at most. + const pieces = try gpa.alloc([]const u8, (reply.len + 4095) / 4096); + defer gpa.free(pieces); + for (pieces, 0..) |*piece, i| piece.* = reply[i * 4096 .. @min(reply.len, (i + 1) * 4096)]; + // Cut just before the end's last byte (inside an ST). + const cut = reply.len - 2; + const ways = [_][]const []const u8{ &.{reply}, pieces, &.{ reply[0..cut], reply[cut..] } }; + for (ways, 0..) |parts, way| { + var reader: TestReader = .{ .parts = parts }; + try readInput(&t.loop, &reader, &t.vx, &t.feed); + const got = try testReplyEvents(gpa, &t.loop, text); + defer gpa.free(got); + std.testing.expectEqualStrings("x clipboard y ", got) catch |err| { + std.debug.print("clipboard of {d} bytes, end {f}, way {d}\n", .{ len, std.ascii.hexEscape(end, .lower), way }); + return err; + }; + } + } + } +} + +test "a clipboard reply past the cap is dropped and said once, and the input after it, another reply, is taken; a DCS and a kitty graphics answer the same" { + const gpa = std.testing.allocator; + var t: TestInput = undefined; + try t.init(); + defer t.deinit(); + const big = try gpa.alloc(u8, max_paste_bytes + 12); + defer gpa.free(big); + @memset(big, 'b'); + for ([_][]const u8{ "\x07", "\x1b\\" }) |end| { + const too_long = try testClipboardReply(gpa, big, end); + defer gpa.free(too_long); + try std.testing.expect(too_long.len > max_sequence); + const reply = try testClipboardReply(gpa, "clip", end); + defer gpa.free(reply); + // In a pty's 4 KiB reads, the reply after them. + const pieces = try gpa.alloc([]const u8, (too_long.len + 4095) / 4096 + 1); + defer gpa.free(pieces); + for (pieces[0 .. pieces.len - 1], 0..) |*piece, i| piece.* = too_long[i * 4096 .. @min(too_long.len, (i + 1) * 4096)]; + pieces[pieces.len - 1] = reply; + const cut = too_long.len - 2; + const ways = [_][]const []const u8{ &.{ too_long, reply }, pieces, &.{ too_long[0..cut], too_long[cut..], reply } }; + for (ways, 0..) |parts, way| { + var reader: TestReader = .{ .parts = parts }; + try readInput(&t.loop, &reader, &t.vx, &t.feed); + const got = try testReplyEvents(gpa, &t.loop, "clip"); + defer gpa.free(got); + std.testing.expectEqualStrings("x reply_dropped y x clipboard y ", got) catch |err| { + std.debug.print("end {f}, way {d}\n", .{ std.ascii.hexEscape(end, .lower), way }); + return err; + }; + } + } + // A DCS and a kitty graphics answer (APC): 4 KiB taken (no event of + // pardes's), one past the cap dropped and said. + for ([_][]const u8{ "\x1bP", "\x1b_G" }) |intro| for ([_]usize{ 4096, max_sequence }) |len| { + const long = try gpa.alloc(u8, 1 + intro.len + len + 3); + defer gpa.free(long); + long[0] = 'x'; + @memcpy(long[1..][0..intro.len], intro); + @memset(long[1 + intro.len .. long.len - 3], 'a'); + @memcpy(long[long.len - 3 ..], "\x1b\\y"); + var reader: TestReader = .{ .parts = &.{long} }; + try readInput(&t.loop, &reader, &t.vx, &t.feed); + const got = try testReplyEvents(gpa, &t.loop, ""); + defer gpa.free(got); + try std.testing.expectEqualStrings(if (len == max_sequence) "x reply_dropped y " else "x y ", got); + }; +} + +test "a bracketed paste past the paste limit is cut and said, and a reply dropped is said, on the notice line" { + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true }); + defer core.deinit(); + const pane = try core.setTestFile(""); + pane.body.mode = .insert; + var no_peers: replica.Peers = .{}; + var paste_buf: std.Io.Writer.Allocating = .init(gpa); + defer paste_buf.deinit(); + var shell: Shell = .{ + .io = std.testing.io, + .gpa = gpa, + .lsp_gpa = gpa, + .core = core, + .frames = core.frames, + .loop = undefined, + .frame = undefined, + .paste_buf = &paste_buf, + .watches = undefined, + .peers = &no_peers, + .ptys = undefined, + }; + const big = try gpa.alloc(u8, max_paste_bytes); + defer gpa.free(big); + @memset(big, 'x'); + // Up to 2026-11-23 the tty kept the limit's bytes and no more, so the + // core never saw the paste was longer: cut, and nothing said. + const paste = [_]Msg{ .paste_start, .{ .key_press = .{ .codepoint = 'x', .text = big } }, .{ .key_press = .{ .codepoint = 'y', .text = "y" } }, .paste_end }; + for (paste) |ev| _ = shell.apply(ev); + try std.testing.expectEqual(max_paste_bytes, pane.file.?.content.len); + try std.testing.expect(std.mem.indexOf(u8, pane.msg[0..pane.msg_len], "paste cut") != null); + _ = shell.apply(.reply_dropped); + try std.testing.expectEqualStrings(reply_dropped_words, pane.msg[0..pane.msg_len]); +} + test "terminal input split at every byte with a gap reads as it does whole" { const gpa = std.testing.allocator; var t: TestInput = undefined; @@ -2917,6 +3123,15 @@ test "a mouse report at row or column 0, or before it, is the window's edge, nev } } +test "an X10 mouse report with a byte below 32, and one with a button no mouse has, are no event, never a panic" { + // A legacy terminal's `ESC [ M` then a control byte: Alt+[, Shift+M, Enter and two keys. + for ([_][]const u8{ "\x1b[M\x0d\x20\x20", "\x1b[M\x20\x0d\x20", "\x1b[M\x20\x20\x1f", "\x1b[<192;1;1M", "\x1b[<195;5;5m" }) |sequence| { + var parser: vaxis.Parser = .{}; + const result = try parser.parse(sequence, std.testing.allocator); + try std.testing.expect(result.event == null and result.n > 0); + } +} + test "a mouse's side buttons, xterm's 8 and 9, are Back and Forward, as SDL's X1 and X2 are" { for ([_]vaxis.Mouse.Button{ .button_8, .button_9 }, [_]pardes.Mouse.Button{ .back, .forward }) |from, to| { const event = mouseEvent(.{ .col = 3, .row = 2, .button = from, .mods = .{}, .type = .press }).?; |
