TypeScript
fix(web): keep wide characters in terminal links (#24013)
Victor Vogeld15233544ecf
diff --git a/apps/web/src/terminal/ghostty/core.ts b/apps/web/src/terminal/ghostty/core.tsindex 3bd8a89807e..60c81478f74 100644--- a/apps/web/src/terminal/ghostty/core.ts+++ b/apps/web/src/terminal/ghostty/core.ts@@ -1067,7 +1067,8 @@ export class GhosttyTerminalCore {}}let wide = 0;- if (text.length === 0 && cells.at(-1)?.text.length) {+ // A wrap spacer in the final column can follow a spacer tail.+ if (text.length === 0 && (cells.at(-1)?.text.length || cells.length === cols - 1)) {this.assertSuccess("ghostty_render_state_row_cells_get(raw)",this.runtime.call(diff --git a/apps/web/src/terminal/ghostty/surface.test.ts b/apps/web/src/terminal/ghostty/surface.test.tsindex 8816105599b..5473e3c4ffa 100644--- a/apps/web/src/terminal/ghostty/surface.test.ts+++ b/apps/web/src/terminal/ghostty/surface.test.ts@@ -512,6 +512,42 @@ const cell = (text: string): GhosttyCell => ({});describe("terminalLinkAtPositionWithRange", () => {+ it.each([+ { text: "https://example.com/日本", cols: 80, end: { x: 23, y: 0 } },+ { text: "src/日本/main.ts:3:1", cols: 80, end: { x: 19, y: 0 } },+ { text: "https://example.com/🙂", cols: 80, end: { x: 21, y: 0 } },+ { text: "https://example.com/日本", cols: 23, end: { x: 1, y: 1 } },+ ])("keeps every cell of $text clickable at $cols columns", async ({ text, cols, end }) => {+ const core = await GhosttyTerminalCore.create(+ cols,+ 3,+ 8,+ 16,+ {+ foreground: { r: 255, g: 255, b: 255 },+ background: { r: 0, g: 0, b: 0 },+ cursor: { r: 255, g: 255, b: 255 },+ },+ () => {},+ );+ try {+ core.write(text + " next");+ const rows = core.snapshot().rowData;+ for (let y = 0; y <= end.y; y += 1) {+ const lastColumn = y === end.y ? end.x : cols - 2;+ for (let x = 0; x <= lastColumn; x += 1) {+ expect(terminalLinkAtPositionWithRange(rows, y, x)).toEqual({+ text,+ range: { start: { x: 0, y: 0 }, end },+ });+ }+ }+ expect(terminalLinkAtPositionWithRange(rows, end.y, end.x + 1)).toBeNull();+ } finally {+ core.dispose();+ }+ });+it("activates a wrapped quoted Windows path from its spaces and position suffix", () => {const row = (text: string, isWrapContinuation: boolean): GhosttyRow => ({cells: Array.from(text, (character) => cell(character)),diff --git a/apps/web/src/terminal/ghostty/surface.ts b/apps/web/src/terminal/ghostty/surface.tsindex cb05acdc5e0..802d39e5940 100644--- a/apps/web/src/terminal/ghostty/surface.ts+++ b/apps/web/src/terminal/ghostty/surface.ts@@ -2,7 +2,9 @@ import { isMacPlatform } from "../../lib/utils";import { SELECTION_MULTI_CLICK_INTERVAL_MS } from "../../lib/selectionActions";import { collectWrappedTerminalLinkLine, extractTerminalLinks } from "../../terminal-links";import {+ GHOSTTY_CELL_WIDE,GhosttyTerminalCore,+ type GhosttyCell,type GhosttyScrollbar,type GhosttySnapshot,type GhosttyTheme,@@ -201,15 +203,23 @@ function terminalGridCellAt(options: {};}+function terminalCellText(cell: GhosttyCell): string {+ if (cell.wide === GHOSTTY_CELL_WIDE.spacerTail || cell.wide === GHOSTTY_CELL_WIDE.spacerHead) {+ return "";+ }+ return cell.text || " ";+}+function terminalRowText(row: GhosttySnapshot["rowData"][number], trimRight: boolean): string {- const text = row.cells.map((cell) => cell.text || " ").join("");+ const text = row.cells.map(terminalCellText).join("");return trimRight ? text.trimEnd() : text;}function terminalColumnOffset(row: GhosttySnapshot["rowData"][number], column: number): number {let offset = 0;for (let cellIndex = 0; cellIndex < column; cellIndex += 1) {- offset += row.cells[cellIndex]?.text.length || 1;+ const cell = row.cells[cellIndex];+ offset += cell ? terminalCellText(cell).length : 1;}return offset;}@@ -265,7 +275,8 @@ export function terminalLinkAtPositionWithRange(const lastSegment = wrappedLine.segments.at(-1);const lastRow = lastSegment ? rows[lastSegment.bufferLineNumber - 1] : undefined;const continuesBelowViewport = lastRow !== undefined && lastRow.wrapsToNext;- const offset = segment.startIndex + terminalColumnOffset(row, column);+ const hitColumn = row.cells[column]?.wide === GHOSTTY_CELL_WIDE.spacerTail ? column - 1 : column;+ const offset = segment.startIndex + terminalColumnOffset(row, hitColumn);for (const match of extractTerminalLinks(wrappedLine.text)) {if (offset >= match.start && offset < match.end) {if (match.end === wrappedLine.text.length && continuesBelowViewport) return null;@@ -278,6 +289,7 @@ export function terminalLinkAtPositionWithRange(const startRow = startSegment ? rows[startSegment.bufferLineNumber - 1] : undefined;const endRow = endSegment ? rows[endSegment.bufferLineNumber - 1] : undefined;if (!startSegment || !endSegment || !startRow || !endRow) return null;+ const endColumn = terminalColumnAtOffset(endRow, match.end - 1 - endSegment.startIndex);return {text: match.text,range: {@@ -286,7 +298,10 @@ export function terminalLinkAtPositionWithRange(y: startSegment.bufferLineNumber - 1,},end: {- x: terminalColumnAtOffset(endRow, match.end - 1 - endSegment.startIndex),+ x:+ endRow.cells[endColumn + 1]?.wide === GHOSTTY_CELL_WIDE.spacerTail+ ? endColumn + 1+ : endColumn,y: endSegment.bufferLineNumber - 1,},},