mirror of
https://github.com/getpaseo/paseo.git
synced 2026-10-26 03:03:04 +00:00
* fix(pairing): keep terminal QR codes scannable Prompt framing could wrap dense QR codes and alter long pairing links. Print pairing instructions directly, add the standard quiet zone, and suppress codes that cannot fit without auto-wrapping. * fix(pairing): contain terminal QR background
38 lines
996 B
TypeScript
38 lines
996 B
TypeScript
const ANSI_PATTERN = new RegExp(`${String.fromCharCode(0x1b)}\\[[0-9;]*m`, "g");
|
|
|
|
interface PairingInstructions {
|
|
url: string;
|
|
qr: string | null;
|
|
columns?: number;
|
|
}
|
|
|
|
function visibleWidth(value: string): number {
|
|
return Math.max(
|
|
...value
|
|
.replace(ANSI_PATTERN, "")
|
|
.split("\n")
|
|
.map((line) => line.length),
|
|
);
|
|
}
|
|
|
|
function formatQr(qr: string | null, columns: number | undefined): string {
|
|
if (!qr) {
|
|
return "QR code is unavailable. Use the pairing link below.";
|
|
}
|
|
|
|
if (columns === undefined) {
|
|
return "QR code not shown because terminal width could not be detected.";
|
|
}
|
|
|
|
const width = visibleWidth(qr);
|
|
if (columns <= width) {
|
|
return `QR code not shown. Resize the terminal to at least ${width + 1} columns, then run this command again.`;
|
|
}
|
|
|
|
return qr;
|
|
}
|
|
|
|
export function formatPairingInstructions({ url, qr, columns }: PairingInstructions): string {
|
|
return `\nScan to pair:\n${formatQr(qr, columns)}\n\nPairing link:\n${url}\n`;
|
|
}
|