Files
pterodactyl-panel/resources/scripts/components/server/console/WrappedUrlProvider.ts
T
Sky Mulley 266544edfc Interactable Terminal Improvements (#7296)
Fixes some internal Discord discussions.

Most of this works around gaps in ghostty-web 0.4.0's selection and link
handling. Several fixes reach into its private internals, so the
dependency is pinned to `0.4.0` now instead of free spinning.

Ghostty seems to be an overall regression as a library over XTerm, below
are some internal hook ins to provide vital functionality back to the
console that v1 originally had.

### Selection
- Double-click selects the word on the line you clicked. Ghostty's
`getWordAtCell` reads the live screen row whatever the scroll position,
so once you'd scrolled up it picked a word from a different line.
- Triple-click now selects and copies the whole line, including every
row it wraps onto.
- Copying no longer puts a line break where a line only wrapped.
Ghostty's `getSelection` puts `\n` between every row, which broke copied
URLs and commands. The fix covers Ctrl/Cmd+C, drag-copy, right-click
copy and triple-click. If a row wrapped at a space, the space is kept.

### Links
- URLs that wrap across rows are detected as one link, and hovering any
part of them underlines the whole URL.
- Only `http:` and `https:` links can be opened. Console output comes
from the game server, so a `javascript:`, `file:` or `ssh:` link in the
output is ignored rather than passed to `window.open`.

### Focus
- The page no longer scrolls when the console takes focus. Ghostty
focuses the console on open and its hidden textarea on every press; both
now focus with `preventScroll`.
2026-10-16 14:11:59 -04:00

57 lines
1.9 KiB
TypeScript

import type { IBufferCellPosition, ILink, ILinkProvider } from 'ghostty-web';
import { type WrappedRowSource, wrappedRowsAround } from '@/components/server/console/wrappedRows';
const URL_REGEX = /https?:\/\/[\w\-.~:/?#@!$&*+,;=%]+/gi;
const TRAILING_PUNCTUATION = /[.,;!?)\]]+$/;
const MIN_URL_LENGTH = 9;
export class WrappedUrlProvider implements ILinkProvider {
constructor(private readonly terminal: WrappedRowSource) {}
provideLinks(y: number, callback: (links: ILink[] | undefined) => void) {
const { text, positions } = this.joinedRowsAround(y);
const links: ILink[] = [];
for (const match of text.matchAll(URL_REGEX)) {
const url = match[0].replace(TRAILING_PUNCTUATION, '');
if (url.length < MIN_URL_LENGTH) {
continue;
}
const start = positions[match.index];
const end = positions[match.index + url.length - 1];
if (start.y <= y && end.y >= y) {
links.push({ text: url, range: { start, end }, activate: () => {} });
}
}
callback(links.length ? links : undefined);
}
private joinedRowsAround(y: number) {
const buffer = this.terminal.buffer.active;
const { first, last } = wrappedRowsAround(this.terminal, y);
let text = '';
const positions: IBufferCellPosition[] = [];
for (let row = first; row <= last; row++) {
const line = buffer.getLine(row);
for (let x = 0; line && x < line.length; x++) {
const codepoint = line.getCell(x)?.getCodepoint() ?? 0;
const char = codepoint < 32 ? ' ' : String.fromCodePoint(codepoint);
text += char;
for (let unit = 0; unit < char.length; unit++) {
positions.push({ x, y: row });
}
}
}
return { text, positions };
}
}