diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:31:14 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:21:29 -0300 |
| commit | 1a09af0153d9970880e896207a596ed70c11d768 (patch) | |
| tree | 015edd1743ee3259d925b1bf560bb94206e45091 /src | |
| parent | 8cbd23a7dca97d7438e07f494c0592ad7da120f6 (diff) | |
| download | pardes-1a09af0153d9970880e896207a596ed70c11d768.tar.gz pardes-1a09af0153d9970880e896207a596ed70c11d768.zip | |
tui: helpers with one caller are their caller's lines (copyToClipboard, requestClipboard, paintCursor, updateCoreTerminalSize), and kittyPlacement returns at once in a build without PDFs instead of wrapping its whole body in the comptime check
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
| -rw-r--r-- | src/tui/tui.zig | 212 |
1 files changed, 97 insertions, 115 deletions
diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 0389b12c..c6a1b844 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -765,10 +765,6 @@ fn terminalCellPixels(total_pixels: u16, cells: u16, fallback: u32) u32 { return @max(1, @as(u32, total_pixels) / cells); } -fn updateCoreTerminalSize(core: *pardes.Pardes, cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) void { - core.update(.{ .resize = terminalSize(cols, rows, pixel_w, pixel_h) }); -} - fn terminalSize(cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) @FieldType(pardes.Event, "resize") { return .{ .cols = cols, @@ -846,103 +842,100 @@ fn kittyPlacement( screen_pixel_w: u16, screen_pixel_h: u16, ) ?KittyPlacement { - if (comptime pardes.pdf_enabled) { - if (place.native.fit == .contain) return null; - const cell_w = terminalCellPixels(screen_pixel_w, screen_cols, 8); - const cell_h = terminalCellPixels(screen_pixel_h, screen_rows, 16); - const body_w = std.math.mul(u32, place.w, cell_w) catch return null; - const body_h = std.math.mul(u32, place.h, cell_h) catch return null; - const geometry = place.native.geometry orelse pardes.image.nativeGeometry( - place.iw, - place.ih, - body_w, - body_h, - place.native.fit, - place.native.pan_x, - place.native.pan_y, - ) orelse return null; + if (comptime !pardes.pdf_enabled) return null; + if (place.native.fit == .contain) return null; + const cell_w = terminalCellPixels(screen_pixel_w, screen_cols, 8); + const cell_h = terminalCellPixels(screen_pixel_h, screen_rows, 16); + const body_w = std.math.mul(u32, place.w, cell_w) catch return null; + const body_h = std.math.mul(u32, place.h, cell_h) catch return null; + const geometry = place.native.geometry orelse pardes.image.nativeGeometry( + place.iw, + place.ih, + body_w, + body_h, + place.native.fit, + place.native.pan_x, + place.native.pan_y, + ) orelse return null; - const safe_rows_u32 = geometry.dst.h / cell_h; - if (safe_rows_u32 == 0) return null; - const safe_pixel_h = safe_rows_u32 * cell_h; - var safe_src_h: u32 = geometry.src.h; - var declared_cols: u32 = 0; - var declared_rows: u32 = 0; - switch (place.native.fit) { - .width => { - declared_cols = @intCast(@max( - @as(u64, 1), - (@as(u64, geometry.dst.w) + cell_w - 1) / cell_w, - )); - const declared_pixel_w = @as(u64, declared_cols) * cell_w; - const max_src_h: u32 = @intCast( - @as(u64, safe_pixel_h) * geometry.src.w / declared_pixel_w, - ); - if (max_src_h == 0) return null; - safe_src_h = @min(safe_src_h, max_src_h); - const aspect_pixel_h: u32 = @intCast( - (@as(u64, safe_src_h) * declared_pixel_w + geometry.src.w - 1) / - geometry.src.w, - ); - declared_rows = @intCast( - (@as(u64, aspect_pixel_h) + cell_h - 1) / cell_h, - ); - }, - .height => { - declared_rows = safe_rows_u32; - safe_src_h = @max(@as(u32, 1), @as(u32, @intCast( - @as(u64, safe_pixel_h) * geometry.src.h / geometry.dst.h, - ))); - safe_src_h = @min(safe_src_h, geometry.src.h); - const aspect_pixel_w: u32 = @intCast( - (@as(u64, geometry.src.w) * safe_pixel_h + safe_src_h - 1) / - safe_src_h, - ); - declared_cols = @intCast( - (@as(u64, aspect_pixel_w) + cell_w - 1) / cell_w, - ); - }, - .contain => unreachable, - } - if (declared_rows == 0 or declared_rows > safe_rows_u32) return null; + const safe_rows_u32 = geometry.dst.h / cell_h; + if (safe_rows_u32 == 0) return null; + const safe_pixel_h = safe_rows_u32 * cell_h; + var safe_src_h: u32 = geometry.src.h; + var declared_cols: u32 = 0; + var declared_rows: u32 = 0; + switch (place.native.fit) { + .width => { + declared_cols = @intCast(@max( + @as(u64, 1), + (@as(u64, geometry.dst.w) + cell_w - 1) / cell_w, + )); + const declared_pixel_w = @as(u64, declared_cols) * cell_w; + const max_src_h: u32 = @intCast( + @as(u64, safe_pixel_h) * geometry.src.w / declared_pixel_w, + ); + if (max_src_h == 0) return null; + safe_src_h = @min(safe_src_h, max_src_h); + const aspect_pixel_h: u32 = @intCast( + (@as(u64, safe_src_h) * declared_pixel_w + geometry.src.w - 1) / + geometry.src.w, + ); + declared_rows = @intCast( + (@as(u64, aspect_pixel_h) + cell_h - 1) / cell_h, + ); + }, + .height => { + declared_rows = safe_rows_u32; + safe_src_h = @max(@as(u32, 1), @as(u32, @intCast( + @as(u64, safe_pixel_h) * geometry.src.h / geometry.dst.h, + ))); + safe_src_h = @min(safe_src_h, geometry.src.h); + const aspect_pixel_w: u32 = @intCast( + (@as(u64, geometry.src.w) * safe_pixel_h + safe_src_h - 1) / + safe_src_h, + ); + declared_cols = @intCast( + (@as(u64, aspect_pixel_w) + cell_w - 1) / cell_w, + ); + }, + .contain => unreachable, + } + if (declared_rows == 0 or declared_rows > safe_rows_u32) return null; - const src_x = std.math.cast(u16, geometry.src.x) orelse return null; - const src_y = std.math.cast(u16, geometry.src.y) orelse return null; - const src_w = std.math.cast(u16, geometry.src.w) orelse return null; - const src_h = std.math.cast(u16, safe_src_h) orelse return null; - const cell_x = std.math.cast(u16, geometry.dst.x / cell_w) orelse return null; - const cell_y = std.math.cast(u16, geometry.dst.y / cell_h) orelse return null; - if (cell_x >= place.w or cell_y >= place.h) return null; - const pixel_x = std.math.cast(u16, geometry.dst.x % cell_w) orelse return null; - const pixel_y = std.math.cast(u16, geometry.dst.y % cell_h) orelse return null; - const cell_cols = std.math.cast(u16, @min( - @as(u32, place.w - cell_x), - (@as(u64, pixel_x) + @as(u64, declared_cols) * cell_w + cell_w - 1) / cell_w, - )) orelse return null; - const cell_rows = std.math.cast(u16, @min( - @as(u32, place.h - cell_y), - (@as(u64, pixel_y) + declared_rows * cell_h + cell_h - 1) / cell_h, - )) orelse return null; - if (cell_cols == 0 or cell_rows == 0) return null; + const src_x = std.math.cast(u16, geometry.src.x) orelse return null; + const src_y = std.math.cast(u16, geometry.src.y) orelse return null; + const src_w = std.math.cast(u16, geometry.src.w) orelse return null; + const src_h = std.math.cast(u16, safe_src_h) orelse return null; + const cell_x = std.math.cast(u16, geometry.dst.x / cell_w) orelse return null; + const cell_y = std.math.cast(u16, geometry.dst.y / cell_h) orelse return null; + if (cell_x >= place.w or cell_y >= place.h) return null; + const pixel_x = std.math.cast(u16, geometry.dst.x % cell_w) orelse return null; + const pixel_y = std.math.cast(u16, geometry.dst.y % cell_h) orelse return null; + const cell_cols = std.math.cast(u16, @min( + @as(u32, place.w - cell_x), + (@as(u64, pixel_x) + @as(u64, declared_cols) * cell_w + cell_w - 1) / cell_w, + )) orelse return null; + const cell_rows = std.math.cast(u16, @min( + @as(u32, place.h - cell_y), + (@as(u64, pixel_y) + declared_rows * cell_h + cell_h - 1) / cell_h, + )) orelse return null; + if (cell_cols == 0 or cell_rows == 0) return null; - return .{ - .cell_x = cell_x, - .cell_y = cell_y, - .cell_cols = cell_cols, - .cell_rows = cell_rows, - .options = .{ - .clip_region = .{ .x = src_x, .y = src_y, .width = src_w, .height = src_h }, - .pixel_offset = if (pixel_x != 0 or pixel_y != 0) .{ .x = pixel_x, .y = pixel_y } else null, - .size = switch (place.native.fit) { - .width => .{ .cols = std.math.cast(u16, declared_cols) orelse return null }, - .height => .{ .rows = std.math.cast(u16, declared_rows) orelse return null }, - .contain => unreachable, - }, + return .{ + .cell_x = cell_x, + .cell_y = cell_y, + .cell_cols = cell_cols, + .cell_rows = cell_rows, + .options = .{ + .clip_region = .{ .x = src_x, .y = src_y, .width = src_w, .height = src_h }, + .pixel_offset = if (pixel_x != 0 or pixel_y != 0) .{ .x = pixel_x, .y = pixel_y } else null, + .size = switch (place.native.fit) { + .width => .{ .cols = std.math.cast(u16, declared_cols) orelse return null }, + .height => .{ .rows = std.math.cast(u16, declared_rows) orelse return null }, + .contain => unreachable, }, - }; - } else { - return null; - } + }, + }; } test "a terminal's image fragment goes to kitty cropped to its cells' scale, a full row one row high" { @@ -1795,7 +1788,7 @@ fn localSession( core = nc; sh.core = nc; if (comptime pardes.pdf_enabled) if (live.on) { - updateCoreTerminalSize(core, live.vx.screen.width, live.vx.screen.height, live.vx.screen.width_pix, live.vx.screen.height_pix); + core.update(.{ .resize = terminalSize(live.vx.screen.width, live.vx.screen.height, live.vx.screen.width_pix, live.vx.screen.height_pix) }); }; } } @@ -2840,7 +2833,10 @@ const Shell = struct { vx.freeImage(sc.tty.writer(), removed.value.id); if (stale_len < stale.len) break; } - if (glided.cursor) |cur| paintCursor(win, cur.x, cur.y, cur.bar); + if (glided.cursor) |cur| { + win.showCursor(cur.x, cur.y); + win.setCursorShape(if (cur.bar) .beam else .default); + } const tz_render = tracy.zone(@src(), "vx.render"); const before = sc.tty.tty_writer.pos + sc.tty.writer().end; vx.render(sc.tty.writer()) catch {}; @@ -3001,14 +2997,14 @@ const Shell = struct { // Detached, the session keeps it (a 9P read, a paste here). const sc = s.term() orelse return s.core.fallback.setClipboard(text); if (!sc.on) return s.core.fallback.setClipboard(text); - copyToClipboard(&sc.vx, &sc.tty, s.gpa, text); + if (text.len > 0) sc.vx.copyToSystemClipboard(sc.tty.writer(), text, s.gpa) catch {}; } fn readClipboard(ctx: ?*anyopaque) void { const s = of(ctx); const sc = s.term() orelse return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); if (!sc.on) return s.core.update(.{ .paste = s.core.fallback.clipboard.items }); - requestClipboard(&sc.vx, &sc.tty); + sc.vx.requestSystemClipboard(sc.tty.writer()) catch {}; } fn openLink(_: ?*anyopaque, url: []const u8) void { @@ -3148,15 +3144,6 @@ test "TTY worker setup failures finish matching LSP and pipe requests" { } } -fn copyToClipboard(vx: *vaxis.Vaxis, tty: *vaxis.Tty, gpa: std.mem.Allocator, text: []const u8) void { - if (text.len == 0) return; - vx.copyToSystemClipboard(tty.writer(), text, gpa) catch {}; -} - -fn requestClipboard(vx: *vaxis.Vaxis, tty: *vaxis.Tty) void { - vx.requestSystemClipboard(tty.writer()) catch {}; -} - fn lspStatusSink(ctx: ?*anyopaque, text: []const u8) void { const s: *Shell = @ptrCast(@alignCast(ctx orelse return)); const copy = s.lsp_gpa.dupe(u8, text) catch return; @@ -3757,11 +3744,6 @@ const JumpTrail = struct { } }; -fn paintCursor(win: vaxis.Window, x: u16, y: u16, bar: bool) void { - win.showCursor(x, y); - win.setCursorShape(if (bar) .beam else .default); -} - fn vaxisStyle(s: pardes.CellStyle) vaxis.Style { return .{ .fg = vaxisColor(s.fg), |
