A pairing offer remains valid across daemon restarts, so remove the incorrect rotation guidance and warn users where the durable credential is displayed.
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
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\nTreat this pairing link like a password. Anyone with it can access this daemon.\n`;
|
|
}
|