Skip to content

Commit d152335

Browse files
only21milcodexmaria-rcks
authored
fix(web): keep wide characters in terminal links (pingdotgg#24013)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 6ca95bd commit d152335

3 files changed

Lines changed: 57 additions & 5 deletions

File tree

‎apps/web/src/terminal/ghostty/core.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ export class GhosttyTerminalCore {
10671067
}
10681068
}
10691069
let wide = 0;
1070-
if (text.length === 0 && cells.at(-1)?.text.length) {
1070+
// A wrap spacer in the final column can follow a spacer tail.
1071+
if (text.length === 0 && (cells.at(-1)?.text.length || cells.length === cols - 1)) {
10711072
this.assertSuccess(
10721073
"ghostty_render_state_row_cells_get(raw)",
10731074
this.runtime.call(

‎apps/web/src/terminal/ghostty/surface.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,42 @@ const cell = (text: string): GhosttyCell => ({
512512
});
513513

514514
describe("terminalLinkAtPositionWithRange", () => {
515+
it.each([
516+
{ text: "https://example.com/日本", cols: 80, end: { x: 23, y: 0 } },
517+
{ text: "src/日本/main.ts:3:1", cols: 80, end: { x: 19, y: 0 } },
518+
{ text: "https://example.com/🙂", cols: 80, end: { x: 21, y: 0 } },
519+
{ text: "https://example.com/日本", cols: 23, end: { x: 1, y: 1 } },
520+
])("keeps every cell of $text clickable at $cols columns", async ({ text, cols, end }) => {
521+
const core = await GhosttyTerminalCore.create(
522+
cols,
523+
3,
524+
8,
525+
16,
526+
{
527+
foreground: { r: 255, g: 255, b: 255 },
528+
background: { r: 0, g: 0, b: 0 },
529+
cursor: { r: 255, g: 255, b: 255 },
530+
},
531+
() => {},
532+
);
533+
try {
534+
core.write(text + " next");
535+
const rows = core.snapshot().rowData;
536+
for (let y = 0; y <= end.y; y += 1) {
537+
const lastColumn = y === end.y ? end.x : cols - 2;
538+
for (let x = 0; x <= lastColumn; x += 1) {
539+
expect(terminalLinkAtPositionWithRange(rows, y, x)).toEqual({
540+
text,
541+
range: { start: { x: 0, y: 0 }, end },
542+
});
543+
}
544+
}
545+
expect(terminalLinkAtPositionWithRange(rows, end.y, end.x + 1)).toBeNull();
546+
} finally {
547+
core.dispose();
548+
}
549+
});
550+
515551
it("activates a wrapped quoted Windows path from its spaces and position suffix", () => {
516552
const row = (text: string, isWrapContinuation: boolean): GhosttyRow => ({
517553
cells: Array.from(text, (character) => cell(character)),

‎apps/web/src/terminal/ghostty/surface.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { isMacPlatform } from "../../lib/utils";
22
import { SELECTION_MULTI_CLICK_INTERVAL_MS } from "../../lib/selectionActions";
33
import { collectWrappedTerminalLinkLine, extractTerminalLinks } from "../../terminal-links";
44
import {
5+
GHOSTTY_CELL_WIDE,
56
GhosttyTerminalCore,
7+
type GhosttyCell,
68
type GhosttyScrollbar,
79
type GhosttySnapshot,
810
type GhosttyTheme,
@@ -201,15 +203,23 @@ function terminalGridCellAt(options: {
201203
};
202204
}
203205

206+
function terminalCellText(cell: GhosttyCell): string {
207+
if (cell.wide === GHOSTTY_CELL_WIDE.spacerTail || cell.wide === GHOSTTY_CELL_WIDE.spacerHead) {
208+
return "";
209+
}
210+
return cell.text || " ";
211+
}
212+
204213
function terminalRowText(row: GhosttySnapshot["rowData"][number], trimRight: boolean): string {
205-
const text = row.cells.map((cell) => cell.text || " ").join("");
214+
const text = row.cells.map(terminalCellText).join("");
206215
return trimRight ? text.trimEnd() : text;
207216
}
208217

209218
function terminalColumnOffset(row: GhosttySnapshot["rowData"][number], column: number): number {
210219
let offset = 0;
211220
for (let cellIndex = 0; cellIndex < column; cellIndex += 1) {
212-
offset += row.cells[cellIndex]?.text.length || 1;
221+
const cell = row.cells[cellIndex];
222+
offset += cell ? terminalCellText(cell).length : 1;
213223
}
214224
return offset;
215225
}
@@ -265,7 +275,8 @@ export function terminalLinkAtPositionWithRange(
265275
const lastSegment = wrappedLine.segments.at(-1);
266276
const lastRow = lastSegment ? rows[lastSegment.bufferLineNumber - 1] : undefined;
267277
const continuesBelowViewport = lastRow !== undefined && lastRow.wrapsToNext;
268-
const offset = segment.startIndex + terminalColumnOffset(row, column);
278+
const hitColumn = row.cells[column]?.wide === GHOSTTY_CELL_WIDE.spacerTail ? column - 1 : column;
279+
const offset = segment.startIndex + terminalColumnOffset(row, hitColumn);
269280
for (const match of extractTerminalLinks(wrappedLine.text)) {
270281
if (offset >= match.start && offset < match.end) {
271282
if (match.end === wrappedLine.text.length && continuesBelowViewport) return null;
@@ -278,6 +289,7 @@ export function terminalLinkAtPositionWithRange(
278289
const startRow = startSegment ? rows[startSegment.bufferLineNumber - 1] : undefined;
279290
const endRow = endSegment ? rows[endSegment.bufferLineNumber - 1] : undefined;
280291
if (!startSegment || !endSegment || !startRow || !endRow) return null;
292+
const endColumn = terminalColumnAtOffset(endRow, match.end - 1 - endSegment.startIndex);
281293
return {
282294
text: match.text,
283295
range: {
@@ -286,7 +298,10 @@ export function terminalLinkAtPositionWithRange(
286298
y: startSegment.bufferLineNumber - 1,
287299
},
288300
end: {
289-
x: terminalColumnAtOffset(endRow, match.end - 1 - endSegment.startIndex),
301+
x:
302+
endRow.cells[endColumn + 1]?.wide === GHOSTTY_CELL_WIDE.spacerTail
303+
? endColumn + 1
304+
: endColumn,
290305
y: endSegment.bufferLineNumber - 1,
291306
},
292307
},

0 commit comments

Comments
 (0)