Files
paseo/packages/cli/src/output/pairing.ts
T
Mohamed Boudra afcd972dd0 Keep terminal pairing QR codes scannable (#2381)
* 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
2026-07-24 18:47:23 +08:00

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`;
}