Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/web/src/terminal/ghostty/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 36 additions & 0 deletions apps/web/src/terminal/ghostty/surface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
23 changes: 19 additions & 4 deletions apps/web/src/terminal/ghostty/surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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: {
Expand All @@ -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,
},
},
Expand Down
Loading