@tokenoftrust/cli 6.15.1 → 6.15.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/submit.mjs +565 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/cli",
|
|
3
|
-
"version": "6.15.
|
|
3
|
+
"version": "6.15.2",
|
|
4
4
|
"description": "Token of Trust developer CLI — clone a tenant store, run it locally with save→reload, and submit it for preview. Installs the `tot` command.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Token of Trust",
|
package/src/commands/submit.mjs
CHANGED
|
@@ -118,8 +118,8 @@ export function parseArgs(argv) {
|
|
|
118
118
|
// `ref: null` — an explicit `--ref` always wins; otherwise the push target is
|
|
119
119
|
// derived per-run as YOUR OWN isolated candidate ref (resolvePushRef, below),
|
|
120
120
|
// never a fixed shared default.
|
|
121
|
-
/** @type {{ mcp: string|null, identity: string|null, ref: string|null, skipValidate: boolean, skipFreshness: boolean, noWait: boolean, watch: boolean, noOpen: boolean, noCommit: boolean, message: string|null, summary: string|null, summaryFile: string|null, strategy: string|null, json: boolean, forkCandidate: boolean, help: boolean }} */
|
|
122
|
-
const a = { mcp: null, identity: null, ref: null, skipValidate: false, skipFreshness: false, noWait: false, watch: false, noOpen: false, noCommit: false, message: null, summary: null, summaryFile: null, strategy: null, json: false, forkCandidate: false, help: false };
|
|
121
|
+
/** @type {{ mcp: string|null, identity: string|null, ref: string|null, skipValidate: boolean, skipFreshness: boolean, noWait: boolean, watch: boolean, noOpen: boolean, noQr: boolean, noCommit: boolean, message: string|null, summary: string|null, summaryFile: string|null, strategy: string|null, json: boolean, forkCandidate: boolean, help: boolean }} */
|
|
122
|
+
const a = { mcp: null, identity: null, ref: null, skipValidate: false, skipFreshness: false, noWait: false, watch: false, noOpen: false, noQr: false, noCommit: false, message: null, summary: null, summaryFile: null, strategy: null, json: false, forkCandidate: false, help: false };
|
|
123
123
|
for (let i = 0; i < argv.length; i++) {
|
|
124
124
|
const t = argv[i];
|
|
125
125
|
if (t === "--mcp") a.mcp = argv[++i];
|
|
@@ -136,6 +136,7 @@ export function parseArgs(argv) {
|
|
|
136
136
|
else if (t === "--no-wait") a.noWait = true;
|
|
137
137
|
else if (t === "--watch") a.watch = true;
|
|
138
138
|
else if (t === "--no-open") a.noOpen = true;
|
|
139
|
+
else if (t === "--no-qr") a.noQr = true;
|
|
139
140
|
else if (t === "--fork-candidate") a.forkCandidate = true;
|
|
140
141
|
else if (t === "--help" || t === "-h") a.help = true;
|
|
141
142
|
}
|
|
@@ -177,6 +178,7 @@ export function renderUsage(verb = "preview") {
|
|
|
177
178
|
(mutually exclusive with --summary)
|
|
178
179
|
tot ${verb} --no-wait push and exit without polling for the reconcile result
|
|
179
180
|
tot ${verb} --no-open don't open the preview URL in the browser on success
|
|
181
|
+
tot ${verb} --no-qr don't print a terminal QR code for the preview URL
|
|
180
182
|
tot ${verb} --json machine-readable result on stdout (candidate id, PR,
|
|
181
183
|
head SHA, preview URL, reconcile/compliance evidence)
|
|
182
184
|
— implies --no-open, no spinner
|
|
@@ -1334,13 +1336,14 @@ function emitJson(args, payload) {
|
|
|
1334
1336
|
* one structured result object at the end (see buildJsonResult) — stderr
|
|
1335
1337
|
* diagnostics (fail(), `~ …` progress lines) still print either way.
|
|
1336
1338
|
* @param {string[]} argv @param {any} ctx
|
|
1337
|
-
* @param {{ verb?: string, env?: NodeJS.ProcessEnv,
|
|
1339
|
+
* @param {{ verb?: string, env?: NodeJS.ProcessEnv, isTTY?: boolean,
|
|
1338
1340
|
* createClient?: typeof createMcpClient, establish?: typeof establishSession,
|
|
1339
1341
|
* checkout?: typeof checkoutTenant }} [opts]
|
|
1340
1342
|
*/
|
|
1341
1343
|
export async function run(argv, ctx, {
|
|
1342
1344
|
verb = "preview",
|
|
1343
1345
|
env = process.env,
|
|
1346
|
+
isTTY = Boolean(process.stdout?.isTTY),
|
|
1344
1347
|
createClient = createMcpClient,
|
|
1345
1348
|
establish = establishSession,
|
|
1346
1349
|
checkout = checkoutTenant,
|
|
@@ -1792,6 +1795,9 @@ export async function run(argv, ctx, {
|
|
|
1792
1795
|
: null;
|
|
1793
1796
|
if (freshPrUrl && !args.json) {
|
|
1794
1797
|
console.log(`\n ▸ Your fresh preview will appear at:\n ${freshPrUrl}\n (building on the current store — this link goes live once reconcile completes)`);
|
|
1798
|
+
if (shouldPrintQr({ noQr: args.noQr, json: args.json, isTTY })) {
|
|
1799
|
+
console.log(formatTerminalQrBlock(freshPrUrl));
|
|
1800
|
+
}
|
|
1795
1801
|
}
|
|
1796
1802
|
emitJson(args, buildJsonResult({
|
|
1797
1803
|
ok: true, ref, commit, changeId,
|
|
@@ -1880,8 +1886,12 @@ export async function run(argv, ctx, {
|
|
|
1880
1886
|
// — automation doesn't want a browser popping up.
|
|
1881
1887
|
// `commit: statusSha` — the diagnostic blocks name the sha that was actually polled,
|
|
1882
1888
|
// so a "never dispatched" hint points at a sha `preview_status` knows about.
|
|
1889
|
+
const showQr = shouldPrintQr({ noQr: args.noQr, json: args.json, isTTY });
|
|
1883
1890
|
const noChanges = changedEntries.length === 0;
|
|
1884
|
-
reportStatus(status, tenant, { open: !args.noOpen && !args.json, quiet: args.json, commit: statusSha, ref, verb, noChanges });
|
|
1891
|
+
reportStatus(status, tenant, { open: !args.noOpen && !args.json, quiet: args.json, qr: showQr, commit: statusSha, ref, verb, noChanges });
|
|
1892
|
+
if (showQr && !status?.previewUrl && previewPrUrl) {
|
|
1893
|
+
console.log(formatTerminalQrBlock(previewPrUrl));
|
|
1894
|
+
}
|
|
1885
1895
|
const ok = previewSubmitSucceeded(status);
|
|
1886
1896
|
const degradation = invocationDegradedResult();
|
|
1887
1897
|
if (degradation.degraded && !args.json) {
|
|
@@ -2158,13 +2168,15 @@ export function describeReadbackError(e) {
|
|
|
2158
2168
|
* @param {string} tenant
|
|
2159
2169
|
* @returns {string[]}
|
|
2160
2170
|
*/
|
|
2161
|
-
export function formatShareableUrlBlock(s, tenant) {
|
|
2171
|
+
export function formatShareableUrlBlock(s, tenant, { qr = false } = {}) {
|
|
2162
2172
|
if (s?.previewUrl) {
|
|
2163
|
-
|
|
2173
|
+
const lines = [
|
|
2164
2174
|
`\n ✓ Preview ready — share this with your reviewer:`,
|
|
2165
2175
|
` ${s.previewUrl}`,
|
|
2166
2176
|
` ℹ your reviewer needs store access (a member/staff of ${tenant}) to view it — it's not a public link.`,
|
|
2167
2177
|
];
|
|
2178
|
+
if (qr) lines.push(formatTerminalQrBlock(s.previewUrl));
|
|
2179
|
+
return lines;
|
|
2168
2180
|
}
|
|
2169
2181
|
if (s?.status === "reconciled") {
|
|
2170
2182
|
return [`\n ~ reconciled, but no shareable preview URL yet — it'll show up here once available.`];
|
|
@@ -2280,9 +2292,9 @@ export function formatShipLine(s) {
|
|
|
2280
2292
|
|
|
2281
2293
|
/**
|
|
2282
2294
|
* @param {any} s @param {string} tenant
|
|
2283
|
-
* @param {{ open?: boolean, quiet?: boolean, commit?: string|null, ref?: string|null, verb?: string, noChanges?: boolean }} [opts]
|
|
2295
|
+
* @param {{ open?: boolean, quiet?: boolean, qr?: boolean, commit?: string|null, ref?: string|null, verb?: string, noChanges?: boolean }} [opts]
|
|
2284
2296
|
*/
|
|
2285
|
-
function reportStatus(s, tenant, { open = true, quiet = false, commit = null, ref = null, verb = "preview", noChanges = false } = {}) {
|
|
2297
|
+
function reportStatus(s, tenant, { open = true, quiet = false, qr = false, commit = null, ref = null, verb = "preview", noChanges = false } = {}) {
|
|
2286
2298
|
if (!quiet) for (const line of formatVerdictBlock(s?.verdict, s?.supportBundleText)) console.log(line);
|
|
2287
2299
|
if (!s || s.status === "unknown") {
|
|
2288
2300
|
if (!quiet) {
|
|
@@ -2335,9 +2347,553 @@ function reportStatus(s, tenant, { open = true, quiet = false, commit = null, re
|
|
|
2335
2347
|
}
|
|
2336
2348
|
const shipLine = formatShipLine(s);
|
|
2337
2349
|
if (shipLine) console.log(shipLine);
|
|
2338
|
-
for (const line of formatShareableUrlBlock(s, tenant)) console.log(line);
|
|
2350
|
+
for (const line of formatShareableUrlBlock(s, tenant, { qr })) console.log(line);
|
|
2339
2351
|
}
|
|
2340
2352
|
if (s.previewUrl && open && s.status === "reconciled" && openBrowser(s.previewUrl)) {
|
|
2341
2353
|
if (!quiet) console.log(" (opened in your browser)");
|
|
2342
2354
|
}
|
|
2343
2355
|
}
|
|
2356
|
+
|
|
2357
|
+
/**
|
|
2358
|
+
* Decide whether to emit a terminal QR code: skipped under `--no-qr`, `--json`,
|
|
2359
|
+
* or non-TTY stdout so CI logs and JSON output stay clean.
|
|
2360
|
+
* @param {{ noQr?: boolean, json?: boolean, isTTY?: boolean }} opts
|
|
2361
|
+
* @returns {boolean}
|
|
2362
|
+
*/
|
|
2363
|
+
export function shouldPrintQr({ noQr = false, json = false, isTTY = false } = {}) {
|
|
2364
|
+
return Boolean(!noQr && !json && isTTY);
|
|
2365
|
+
}
|
|
2366
|
+
|
|
2367
|
+
// Local ISO 18004 QR encoder + half-block terminal renderer (MIT Kazuhiko Arase tables).
|
|
2368
|
+
// Zero external dependencies or API calls: preview URLs never leave the user's machine.
|
|
2369
|
+
const QR_EC_CODE = { L: 1, M: 0, Q: 3, H: 2 };
|
|
2370
|
+
const QR_EC_INDEX = { 1: 0, 0: 1, 3: 2, 2: 3 };
|
|
2371
|
+
const QR_G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | 1;
|
|
2372
|
+
const QR_G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | 1;
|
|
2373
|
+
const QR_G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
|
|
2374
|
+
const QR_EXP_TABLE = new Array(256).fill(0);
|
|
2375
|
+
const QR_LOG_TABLE = new Array(256).fill(0);
|
|
2376
|
+
for (let i = 0; i < 8; i++) QR_EXP_TABLE[i] = 1 << i;
|
|
2377
|
+
for (let i = 8; i < 256; i++) {
|
|
2378
|
+
QR_EXP_TABLE[i] = QR_EXP_TABLE[i - 4] ^ QR_EXP_TABLE[i - 5] ^ QR_EXP_TABLE[i - 6] ^ QR_EXP_TABLE[i - 8];
|
|
2379
|
+
}
|
|
2380
|
+
for (let i = 0; i < 255; i++) QR_LOG_TABLE[QR_EXP_TABLE[i]] = i;
|
|
2381
|
+
|
|
2382
|
+
const qrExp = (n) => QR_EXP_TABLE[((n % 255) + 255) % 255];
|
|
2383
|
+
const qrLog = (n) => {
|
|
2384
|
+
if (n < 1) throw new Error(`qrLog(${n})`);
|
|
2385
|
+
return QR_LOG_TABLE[n];
|
|
2386
|
+
};
|
|
2387
|
+
|
|
2388
|
+
const QR_ALIGNMENT_POSITIONS = [
|
|
2389
|
+
[],
|
|
2390
|
+
[6, 18], [6, 22], [6, 26], [6, 30], [6, 34], [6, 22, 38], [6, 24, 42], [6, 26, 46], [6, 28, 50],
|
|
2391
|
+
[6, 30, 54], [6, 32, 58], [6, 34, 62], [6, 26, 46, 66], [6, 26, 48, 70], [6, 26, 50, 74], [6, 30, 54, 78],
|
|
2392
|
+
[6, 30, 56, 82], [6, 30, 58, 86], [6, 34, 62, 90], [6, 28, 50, 72, 94], [6, 26, 50, 74, 98], [6, 30, 54, 78, 102],
|
|
2393
|
+
[6, 28, 54, 80, 106], [6, 32, 58, 84, 110], [6, 30, 58, 86, 114], [6, 34, 62, 90, 118], [6, 26, 50, 74, 98, 122],
|
|
2394
|
+
[6, 30, 54, 78, 102, 126], [6, 26, 52, 78, 104, 130], [6, 30, 56, 82, 108, 134], [6, 34, 60, 86, 112, 138],
|
|
2395
|
+
[6, 30, 58, 86, 114, 142], [6, 34, 62, 90, 118, 146], [6, 30, 54, 78, 102, 126, 150], [6, 24, 50, 76, 102, 128, 154],
|
|
2396
|
+
[6, 28, 54, 80, 106, 132, 158], [6, 32, 58, 84, 110, 136, 162], [6, 26, 54, 82, 110, 138, 166], [6, 30, 58, 86, 114, 142, 170],
|
|
2397
|
+
];
|
|
2398
|
+
|
|
2399
|
+
const QR_RS_BLOCK_TABLE = [
|
|
2400
|
+
[1, 26, 19], [1, 26, 16], [1, 26, 13], [1, 26, 9],
|
|
2401
|
+
[1, 44, 34], [1, 44, 28], [1, 44, 22], [1, 44, 16],
|
|
2402
|
+
[1, 70, 55], [1, 70, 44], [2, 35, 17], [2, 35, 13],
|
|
2403
|
+
[1, 100, 80], [2, 50, 32], [2, 50, 24], [4, 25, 9],
|
|
2404
|
+
[1, 134, 108], [2, 67, 43], [2, 33, 15, 2, 34, 16], [2, 33, 11, 2, 34, 12],
|
|
2405
|
+
[2, 86, 68], [4, 43, 27], [4, 43, 19], [4, 43, 15],
|
|
2406
|
+
[2, 98, 78], [4, 49, 31], [2, 32, 14, 4, 33, 15], [4, 39, 13, 1, 40, 14],
|
|
2407
|
+
[2, 121, 97], [2, 60, 38, 2, 61, 39], [4, 40, 18, 2, 41, 19], [4, 40, 14, 2, 41, 15],
|
|
2408
|
+
[2, 146, 116], [3, 58, 36, 2, 59, 37], [4, 36, 16, 4, 37, 17], [4, 36, 12, 4, 37, 13],
|
|
2409
|
+
[2, 86, 68, 2, 87, 69], [4, 69, 43, 1, 70, 44], [6, 43, 19, 2, 44, 20], [6, 43, 15, 2, 44, 16],
|
|
2410
|
+
[4, 101, 81], [1, 80, 50, 4, 81, 51], [4, 50, 22, 4, 51, 23], [3, 36, 12, 8, 37, 13],
|
|
2411
|
+
[2, 116, 92, 2, 117, 93], [6, 58, 36, 2, 59, 37], [4, 46, 20, 6, 47, 21], [7, 42, 14, 4, 43, 15],
|
|
2412
|
+
[4, 133, 107], [8, 59, 37, 1, 60, 38], [8, 44, 20, 4, 45, 21], [12, 33, 11, 4, 34, 12],
|
|
2413
|
+
[3, 145, 115, 1, 146, 116], [4, 64, 40, 5, 65, 41], [11, 36, 16, 5, 37, 17], [11, 36, 12, 5, 37, 13],
|
|
2414
|
+
[5, 109, 87, 1, 110, 88], [5, 65, 41, 5, 66, 42], [5, 54, 24, 7, 55, 25], [11, 36, 12],
|
|
2415
|
+
[5, 122, 98, 1, 123, 99], [7, 73, 45, 3, 74, 46], [15, 43, 19, 2, 44, 20], [3, 45, 15, 13, 46, 16],
|
|
2416
|
+
[1, 135, 107, 5, 136, 108], [10, 74, 46, 1, 75, 47], [1, 50, 22, 15, 51, 23], [2, 42, 14, 17, 43, 15],
|
|
2417
|
+
[5, 150, 120, 1, 151, 121], [9, 69, 43, 4, 70, 44], [17, 50, 22, 1, 51, 23], [2, 42, 14, 19, 43, 15],
|
|
2418
|
+
[3, 141, 113, 4, 142, 114], [3, 70, 44, 11, 71, 45], [17, 47, 21, 4, 48, 22], [9, 39, 13, 16, 40, 14],
|
|
2419
|
+
[3, 135, 107, 5, 136, 108], [3, 67, 41, 13, 68, 42], [15, 54, 24, 5, 55, 25], [15, 43, 15, 10, 44, 16],
|
|
2420
|
+
[4, 144, 116, 4, 145, 117], [17, 68, 42], [17, 50, 22, 6, 51, 23], [19, 46, 16, 6, 47, 17],
|
|
2421
|
+
[2, 139, 111, 7, 140, 112], [17, 74, 46], [7, 54, 24, 16, 55, 25], [34, 37, 13],
|
|
2422
|
+
[4, 151, 121, 5, 152, 122], [4, 75, 47, 14, 76, 48], [11, 54, 24, 14, 55, 25], [16, 45, 15, 14, 46, 16],
|
|
2423
|
+
[6, 147, 117, 4, 148, 118], [6, 73, 45, 14, 74, 46], [11, 54, 24, 16, 55, 25], [30, 46, 16, 2, 47, 17],
|
|
2424
|
+
[8, 132, 106, 4, 133, 107], [8, 75, 47, 13, 76, 48], [7, 54, 24, 22, 55, 25], [22, 45, 15, 13, 46, 16],
|
|
2425
|
+
[10, 142, 114, 2, 143, 115], [19, 74, 46, 4, 75, 47], [28, 50, 22, 6, 51, 23], [33, 46, 16, 4, 47, 17],
|
|
2426
|
+
[8, 152, 122, 4, 153, 123], [22, 73, 45, 3, 74, 46], [8, 53, 23, 26, 54, 24], [12, 45, 15, 28, 46, 16],
|
|
2427
|
+
[3, 147, 117, 10, 148, 118], [3, 73, 45, 23, 74, 46], [4, 54, 24, 31, 55, 25], [11, 45, 15, 31, 46, 16],
|
|
2428
|
+
[7, 146, 116, 7, 147, 117], [21, 73, 45, 7, 74, 46], [1, 53, 23, 37, 54, 24], [19, 45, 15, 26, 46, 16],
|
|
2429
|
+
[5, 145, 115, 10, 146, 116], [19, 75, 47, 10, 76, 48], [15, 54, 24, 25, 55, 25], [23, 45, 15, 25, 46, 16],
|
|
2430
|
+
[13, 145, 115, 3, 146, 116], [2, 74, 46, 29, 75, 47], [42, 54, 24, 1, 55, 25], [23, 45, 15, 28, 46, 16],
|
|
2431
|
+
[17, 145, 115], [10, 74, 46, 23, 75, 47], [10, 54, 24, 35, 55, 25], [19, 45, 15, 35, 46, 16],
|
|
2432
|
+
[17, 145, 115, 1, 146, 116], [14, 74, 46, 21, 75, 47], [29, 54, 24, 19, 55, 25], [11, 45, 15, 46, 46, 16],
|
|
2433
|
+
[13, 145, 115, 6, 146, 116], [14, 74, 46, 23, 75, 47], [44, 54, 24, 7, 55, 25], [59, 46, 16, 1, 47, 17],
|
|
2434
|
+
[12, 151, 121, 7, 152, 122], [12, 75, 47, 26, 76, 48], [39, 54, 24, 14, 55, 25], [22, 45, 15, 41, 46, 16],
|
|
2435
|
+
[6, 151, 121, 14, 152, 122], [6, 75, 47, 34, 76, 48], [46, 54, 24, 10, 55, 25], [2, 45, 15, 64, 46, 16],
|
|
2436
|
+
[17, 152, 122, 4, 153, 123], [29, 74, 46, 14, 75, 47], [49, 54, 24, 10, 55, 25], [24, 45, 15, 46, 46, 16],
|
|
2437
|
+
[4, 152, 122, 18, 153, 123], [13, 74, 46, 32, 75, 47], [48, 54, 24, 14, 55, 25], [42, 45, 15, 32, 46, 16],
|
|
2438
|
+
[20, 147, 117, 4, 148, 118], [40, 75, 47, 7, 76, 48], [43, 54, 24, 22, 55, 25], [10, 45, 15, 67, 46, 16],
|
|
2439
|
+
[19, 148, 118, 6, 149, 119], [18, 75, 47, 31, 76, 48], [34, 54, 24, 34, 55, 25], [20, 45, 15, 61, 46, 16],
|
|
2440
|
+
];
|
|
2441
|
+
|
|
2442
|
+
function getRsBlocks(version, ecCode) {
|
|
2443
|
+
const ecIndex = QR_EC_INDEX[ecCode];
|
|
2444
|
+
if (ecIndex === undefined) throw new Error("Unsupported QR EC level");
|
|
2445
|
+
const spec = QR_RS_BLOCK_TABLE[(version - 1) * 4 + ecIndex];
|
|
2446
|
+
if (!spec) throw new Error(`Unsupported QR version ${version}`);
|
|
2447
|
+
const blocks = [];
|
|
2448
|
+
for (let i = 0; i < spec.length; i += 3) {
|
|
2449
|
+
for (let j = 0; j < spec[i]; j++) blocks.push({ totalCount: spec[i + 1], dataCount: spec[i + 2] });
|
|
2450
|
+
}
|
|
2451
|
+
return blocks;
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
const bchDigit = (data) => {
|
|
2455
|
+
let d = 0;
|
|
2456
|
+
let v = data >>> 0;
|
|
2457
|
+
while (v !== 0) { d++; v >>>= 1; }
|
|
2458
|
+
return d;
|
|
2459
|
+
};
|
|
2460
|
+
const bchTypeInfo = (data) => {
|
|
2461
|
+
let d = data << 10;
|
|
2462
|
+
const gDigit = bchDigit(QR_G15);
|
|
2463
|
+
while (bchDigit(d) - gDigit >= 0) d ^= QR_G15 << (bchDigit(d) - gDigit);
|
|
2464
|
+
return ((data << 10) | d) ^ QR_G15_MASK;
|
|
2465
|
+
};
|
|
2466
|
+
const bchTypeNumber = (version) => {
|
|
2467
|
+
let d = version << 12;
|
|
2468
|
+
const gDigit = bchDigit(QR_G18);
|
|
2469
|
+
while (bchDigit(d) - gDigit >= 0) d ^= QR_G18 << (bchDigit(d) - gDigit);
|
|
2470
|
+
return (version << 12) | d;
|
|
2471
|
+
};
|
|
2472
|
+
|
|
2473
|
+
function qrMask(pattern, r, c) {
|
|
2474
|
+
switch (pattern) {
|
|
2475
|
+
case 0: return (r + c) % 2 === 0;
|
|
2476
|
+
case 1: return r % 2 === 0;
|
|
2477
|
+
case 2: return c % 3 === 0;
|
|
2478
|
+
case 3: return (r + c) % 3 === 0;
|
|
2479
|
+
case 4: return (Math.floor(r / 2) + Math.floor(c / 3)) % 2 === 0;
|
|
2480
|
+
case 5: return ((r * c) % 2) + ((r * c) % 3) === 0;
|
|
2481
|
+
case 6: return (((r * c) % 2) + ((r * c) % 3)) % 2 === 0;
|
|
2482
|
+
case 7: return (((r * c) % 3) + ((r + c) % 2)) % 2 === 0;
|
|
2483
|
+
default: throw new Error(`Invalid QR mask ${pattern}`);
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
function polyMultiply(a, b) {
|
|
2488
|
+
const out = new Array(a.length + b.length - 1).fill(0);
|
|
2489
|
+
for (let i = 0; i < a.length; i++) {
|
|
2490
|
+
if (a[i] === 0) continue;
|
|
2491
|
+
for (let j = 0; j < b.length; j++) {
|
|
2492
|
+
if (b[j] === 0) continue;
|
|
2493
|
+
out[i + j] ^= qrExp(qrLog(a[i]) + qrLog(b[j]));
|
|
2494
|
+
}
|
|
2495
|
+
}
|
|
2496
|
+
return out;
|
|
2497
|
+
}
|
|
2498
|
+
|
|
2499
|
+
function polyMod(dividend, divisor) {
|
|
2500
|
+
const work = dividend.slice();
|
|
2501
|
+
let offset = 0;
|
|
2502
|
+
while (work.length - offset >= divisor.length) {
|
|
2503
|
+
while (offset < work.length && work[offset] === 0) offset++;
|
|
2504
|
+
if (work.length - offset < divisor.length) break;
|
|
2505
|
+
const ratio = qrLog(work[offset]) - qrLog(divisor[0]);
|
|
2506
|
+
for (let i = 0; i < divisor.length; i++) {
|
|
2507
|
+
if (divisor[i] !== 0) work[offset + i] ^= qrExp(qrLog(divisor[i]) + ratio);
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
return work.slice(offset);
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
function rsGeneratorPoly(ecCount) {
|
|
2514
|
+
let poly = [1];
|
|
2515
|
+
for (let i = 0; i < ecCount; i++) poly = polyMultiply(poly, [1, qrExp(i)]);
|
|
2516
|
+
return poly;
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
function qrLostPoint(modules) {
|
|
2520
|
+
const n = modules.length;
|
|
2521
|
+
let lost = 0;
|
|
2522
|
+
for (let r = 0; r < n; r++) {
|
|
2523
|
+
for (let c = 0; c < n; c++) {
|
|
2524
|
+
const dark = modules[r][c];
|
|
2525
|
+
let same = 0;
|
|
2526
|
+
for (let dr = -1; dr <= 1; dr++) {
|
|
2527
|
+
if (r + dr < 0 || r + dr >= n) continue;
|
|
2528
|
+
for (let dc = -1; dc <= 1; dc++) {
|
|
2529
|
+
if (c + dc < 0 || c + dc >= n || (dr === 0 && dc === 0)) continue;
|
|
2530
|
+
if (modules[r + dr][c + dc] === dark) same++;
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
if (same > 5) lost += 3 + same - 5;
|
|
2534
|
+
}
|
|
2535
|
+
}
|
|
2536
|
+
for (let r = 0; r < n - 1; r++) {
|
|
2537
|
+
for (let c = 0; c < n - 1; c++) {
|
|
2538
|
+
const count =
|
|
2539
|
+
(modules[r][c] ? 1 : 0) + (modules[r + 1][c] ? 1 : 0) +
|
|
2540
|
+
(modules[r][c + 1] ? 1 : 0) + (modules[r + 1][c + 1] ? 1 : 0);
|
|
2541
|
+
if (count === 0 || count === 4) lost += 3;
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
for (let r = 0; r < n; r++) {
|
|
2545
|
+
for (let c = 0; c < n - 6; c++) {
|
|
2546
|
+
if (modules[r][c] && !modules[r][c + 1] && modules[r][c + 2] && modules[r][c + 3] && modules[r][c + 4] && !modules[r][c + 5] && modules[r][c + 6]) {
|
|
2547
|
+
lost += 40;
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
for (let c = 0; c < n; c++) {
|
|
2552
|
+
for (let r = 0; r < n - 6; r++) {
|
|
2553
|
+
if (modules[r][c] && !modules[r + 1][c] && modules[r + 2][c] && modules[r + 3][c] && modules[r + 4][c] && !modules[r + 5][c] && modules[r + 6][c]) {
|
|
2554
|
+
lost += 40;
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
}
|
|
2558
|
+
let darkCount = 0;
|
|
2559
|
+
for (let r = 0; r < n; r++) {
|
|
2560
|
+
for (let c = 0; c < n; c++) if (modules[r][c]) darkCount++;
|
|
2561
|
+
}
|
|
2562
|
+
return lost + (Math.abs((100 * darkCount) / n / n - 50) / 5) * 10;
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
function setupFunctionPatterns(version, ecCode, maskPattern) {
|
|
2566
|
+
const n = version * 4 + 17;
|
|
2567
|
+
const grid = Array.from({ length: n }, () => new Array(n).fill(null));
|
|
2568
|
+
const placeProbe = (row, col) => {
|
|
2569
|
+
for (let r = -1; r <= 7; r++) {
|
|
2570
|
+
if (row + r < 0 || row + r >= n) continue;
|
|
2571
|
+
for (let c = -1; c <= 7; c++) {
|
|
2572
|
+
if (col + c < 0 || col + c >= n) continue;
|
|
2573
|
+
grid[row + r][col + c] =
|
|
2574
|
+
(0 <= r && r <= 6 && (c === 0 || c === 6)) ||
|
|
2575
|
+
(0 <= c && c <= 6 && (r === 0 || r === 6)) ||
|
|
2576
|
+
(2 <= r && r <= 4 && 2 <= c && c <= 4);
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
};
|
|
2580
|
+
placeProbe(0, 0);
|
|
2581
|
+
placeProbe(n - 7, 0);
|
|
2582
|
+
placeProbe(0, n - 7);
|
|
2583
|
+
const pos = QR_ALIGNMENT_POSITIONS[version - 1] ?? [];
|
|
2584
|
+
for (let i = 0; i < pos.length; i++) {
|
|
2585
|
+
for (let j = 0; j < pos.length; j++) {
|
|
2586
|
+
const row = pos[i];
|
|
2587
|
+
const col = pos[j];
|
|
2588
|
+
if (grid[row][col] !== null) continue;
|
|
2589
|
+
for (let r = -2; r <= 2; r++) {
|
|
2590
|
+
for (let c = -2; c <= 2; c++) {
|
|
2591
|
+
grid[row + r][col + c] = Math.abs(r) === 2 || Math.abs(c) === 2 || (r === 0 && c === 0);
|
|
2592
|
+
}
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
for (let r = 8; r < n - 8; r++) if (grid[r][6] === null) grid[r][6] = r % 2 === 0;
|
|
2597
|
+
for (let c = 8; c < n - 8; c++) if (grid[6][c] === null) grid[6][c] = c % 2 === 0;
|
|
2598
|
+
const fmtBits = bchTypeInfo((ecCode << 3) | maskPattern);
|
|
2599
|
+
for (let i = 0; i < 15; i++) {
|
|
2600
|
+
const bit = ((fmtBits >> i) & 1) === 1;
|
|
2601
|
+
grid[i < 6 ? i : i < 8 ? i + 1 : n - 15 + i][8] = bit;
|
|
2602
|
+
grid[8][i < 8 ? n - i - 1 : i < 9 ? 15 - i : 15 - i - 1] = bit;
|
|
2603
|
+
}
|
|
2604
|
+
grid[n - 8][8] = true;
|
|
2605
|
+
if (version >= 7) {
|
|
2606
|
+
const verBits = bchTypeNumber(version);
|
|
2607
|
+
for (let i = 0; i < 18; i++) {
|
|
2608
|
+
const bit = ((verBits >> i) & 1) === 1;
|
|
2609
|
+
grid[Math.floor(i / 3)][(i % 3) + n - 11] = bit;
|
|
2610
|
+
grid[(i % 3) + n - 11][Math.floor(i / 3)] = bit;
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
return grid;
|
|
2614
|
+
}
|
|
2615
|
+
|
|
2616
|
+
/**
|
|
2617
|
+
* Encode a UTF-8 string into an ISO 18004 QR boolean matrix (`true` = dark module).
|
|
2618
|
+
* @param {string} text
|
|
2619
|
+
* @param {"L"|"M"|"Q"|"H"} [ecLevel="M"]
|
|
2620
|
+
* @returns {boolean[][]}
|
|
2621
|
+
*/
|
|
2622
|
+
export function encodeQrMatrix(text, ecLevel = "M") {
|
|
2623
|
+
const bytes = new TextEncoder().encode(text);
|
|
2624
|
+
const ecCode = QR_EC_CODE[ecLevel];
|
|
2625
|
+
let chosenVersion = 0;
|
|
2626
|
+
/** @type {{ totalCount: number, dataCount: number }[]} */
|
|
2627
|
+
let blocks = [];
|
|
2628
|
+
let totalDataCount = 0;
|
|
2629
|
+
for (let version = 1; version <= 40; version++) {
|
|
2630
|
+
const candidateBlocks = getRsBlocks(version, ecCode);
|
|
2631
|
+
const cap = candidateBlocks.reduce((sum, b) => sum + b.dataCount, 0);
|
|
2632
|
+
if (4 + (version < 10 ? 8 : 16) + bytes.length * 8 <= cap * 8) {
|
|
2633
|
+
chosenVersion = version;
|
|
2634
|
+
blocks = candidateBlocks;
|
|
2635
|
+
totalDataCount = cap;
|
|
2636
|
+
break;
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
if (!chosenVersion) throw new Error("QR payload exceeds version 40 capacity");
|
|
2640
|
+
|
|
2641
|
+
const buffer = [];
|
|
2642
|
+
let bitLength = 0;
|
|
2643
|
+
const pushBits = (val, len) => {
|
|
2644
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
2645
|
+
const idx = Math.floor(bitLength / 8);
|
|
2646
|
+
if (buffer.length <= idx) buffer.push(0);
|
|
2647
|
+
if (((val >>> i) & 1) === 1) buffer[idx] |= 0x80 >>> (bitLength % 8);
|
|
2648
|
+
bitLength++;
|
|
2649
|
+
}
|
|
2650
|
+
};
|
|
2651
|
+
pushBits(4, 4);
|
|
2652
|
+
pushBits(bytes.length, chosenVersion < 10 ? 8 : 16);
|
|
2653
|
+
for (const b of bytes) pushBits(b, 8);
|
|
2654
|
+
pushBits(0, Math.min(4, totalDataCount * 8 - bitLength));
|
|
2655
|
+
while (bitLength % 8 !== 0) pushBits(0, 1);
|
|
2656
|
+
while (bitLength < totalDataCount * 8) {
|
|
2657
|
+
pushBits(0xec, 8);
|
|
2658
|
+
if (bitLength < totalDataCount * 8) pushBits(0x11, 8);
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
let offset = 0;
|
|
2662
|
+
let maxDc = 0;
|
|
2663
|
+
let maxEc = 0;
|
|
2664
|
+
const dcBlocks = [];
|
|
2665
|
+
const ecBlocks = [];
|
|
2666
|
+
for (const block of blocks) {
|
|
2667
|
+
const ecCount = block.totalCount - block.dataCount;
|
|
2668
|
+
maxDc = Math.max(maxDc, block.dataCount);
|
|
2669
|
+
maxEc = Math.max(maxEc, ecCount);
|
|
2670
|
+
const dc = new Array(block.dataCount);
|
|
2671
|
+
for (let i = 0; i < block.dataCount; i++) dc[i] = 0xff & buffer[offset + i];
|
|
2672
|
+
offset += block.dataCount;
|
|
2673
|
+
const rem = polyMod(dc.concat(new Array(ecCount).fill(0)), rsGeneratorPoly(ecCount));
|
|
2674
|
+
const ec = new Array(ecCount).fill(0);
|
|
2675
|
+
for (let i = 0; i < rem.length; i++) ec[ecCount - rem.length + i] = rem[i];
|
|
2676
|
+
dcBlocks.push(dc);
|
|
2677
|
+
ecBlocks.push(ec);
|
|
2678
|
+
}
|
|
2679
|
+
const codewords = [];
|
|
2680
|
+
for (let i = 0; i < maxDc; i++) for (const dc of dcBlocks) if (i < dc.length) codewords.push(dc[i]);
|
|
2681
|
+
for (let i = 0; i < maxEc; i++) for (const ec of ecBlocks) if (i < ec.length) codewords.push(ec[i]);
|
|
2682
|
+
|
|
2683
|
+
const buildMatrix = (maskPattern) => {
|
|
2684
|
+
const grid = setupFunctionPatterns(chosenVersion, ecCode, maskPattern);
|
|
2685
|
+
const n = grid.length;
|
|
2686
|
+
let inc = -1;
|
|
2687
|
+
let row = n - 1;
|
|
2688
|
+
let bitIdx = 7;
|
|
2689
|
+
let byteIdx = 0;
|
|
2690
|
+
for (let col = n - 1; col > 0; col -= 2) {
|
|
2691
|
+
if (col === 6) col--;
|
|
2692
|
+
while (true) {
|
|
2693
|
+
for (let c = 0; c < 2; c++) {
|
|
2694
|
+
if (grid[row][col - c] === null) {
|
|
2695
|
+
let dark = byteIdx < codewords.length ? ((codewords[byteIdx] >>> bitIdx) & 1) === 1 : false;
|
|
2696
|
+
if (qrMask(maskPattern, row, col - c)) dark = !dark;
|
|
2697
|
+
grid[row][col - c] = dark;
|
|
2698
|
+
bitIdx--;
|
|
2699
|
+
if (bitIdx === -1) { byteIdx++; bitIdx = 7; }
|
|
2700
|
+
}
|
|
2701
|
+
}
|
|
2702
|
+
row += inc;
|
|
2703
|
+
if (row < 0 || row >= n) { row -= inc; inc = -inc; break; }
|
|
2704
|
+
}
|
|
2705
|
+
}
|
|
2706
|
+
return grid;
|
|
2707
|
+
};
|
|
2708
|
+
let bestScore = Infinity;
|
|
2709
|
+
let bestMatrix = null;
|
|
2710
|
+
for (let pattern = 0; pattern < 8; pattern++) {
|
|
2711
|
+
const candidate = buildMatrix(pattern);
|
|
2712
|
+
const score = qrLostPoint(candidate);
|
|
2713
|
+
if (score < bestScore) { bestScore = score; bestMatrix = candidate; }
|
|
2714
|
+
}
|
|
2715
|
+
return bestMatrix ?? buildMatrix(0);
|
|
2716
|
+
}
|
|
2717
|
+
|
|
2718
|
+
/**
|
|
2719
|
+
* Decode an ISO 18004 QR boolean matrix directly from its modules.
|
|
2720
|
+
* @param {boolean[][]} modules
|
|
2721
|
+
* @returns {string}
|
|
2722
|
+
*/
|
|
2723
|
+
export function decodeQrMatrix(modules) {
|
|
2724
|
+
const n = modules.length;
|
|
2725
|
+
if (n < 21 || (n - 17) % 4 !== 0 || modules.some((r) => r.length !== n)) {
|
|
2726
|
+
throw new Error(`Invalid QR matrix dimensions: ${n}`);
|
|
2727
|
+
}
|
|
2728
|
+
const version = (n - 17) / 4;
|
|
2729
|
+
if (!modules[n - 8][8]) throw new Error("Missing fixed QR dark module");
|
|
2730
|
+
let fmtA = 0;
|
|
2731
|
+
let fmtB = 0;
|
|
2732
|
+
for (let i = 0; i < 15; i++) {
|
|
2733
|
+
if (modules[i < 6 ? i : i < 8 ? i + 1 : n - 15 + i][8]) fmtA |= 1 << i;
|
|
2734
|
+
if (modules[8][i < 8 ? n - i - 1 : i < 9 ? 15 - i : 15 - i - 1]) fmtB |= 1 << i;
|
|
2735
|
+
}
|
|
2736
|
+
const popcount = (v) => {
|
|
2737
|
+
let x = v;
|
|
2738
|
+
let c = 0;
|
|
2739
|
+
while (x !== 0) { x &= x - 1; c++; }
|
|
2740
|
+
return c;
|
|
2741
|
+
};
|
|
2742
|
+
let bestFmt = -1;
|
|
2743
|
+
let bestDist = Infinity;
|
|
2744
|
+
for (let k = 0; k < 32; k++) {
|
|
2745
|
+
const encoded = bchTypeInfo(k);
|
|
2746
|
+
const dist = Math.min(popcount(encoded ^ fmtA), popcount(encoded ^ fmtB));
|
|
2747
|
+
if (dist < bestDist) { bestDist = dist; bestFmt = k; }
|
|
2748
|
+
}
|
|
2749
|
+
if (bestFmt < 0 || bestDist > 3) throw new Error("Unreadable QR format info");
|
|
2750
|
+
const ecCode = (bestFmt >> 3) & 3;
|
|
2751
|
+
const maskPattern = bestFmt & 7;
|
|
2752
|
+
|
|
2753
|
+
const reserved = setupFunctionPatterns(version, ecCode, maskPattern);
|
|
2754
|
+
const blocks = getRsBlocks(version, ecCode);
|
|
2755
|
+
const totalCodewords = blocks.reduce((sum, b) => sum + b.totalCount, 0);
|
|
2756
|
+
const totalDataBytes = blocks.reduce((sum, b) => sum + b.dataCount, 0);
|
|
2757
|
+
const codewords = new Array(totalCodewords).fill(0);
|
|
2758
|
+
let inc = -1;
|
|
2759
|
+
let row = n - 1;
|
|
2760
|
+
let bitIdx = 7;
|
|
2761
|
+
let byteIdx = 0;
|
|
2762
|
+
for (let col = n - 1; col > 0; col -= 2) {
|
|
2763
|
+
if (col === 6) col--;
|
|
2764
|
+
while (true) {
|
|
2765
|
+
for (let c = 0; c < 2; c++) {
|
|
2766
|
+
if (reserved[row][col - c] === null && byteIdx < totalCodewords) {
|
|
2767
|
+
if (Boolean(modules[row][col - c]) !== qrMask(maskPattern, row, col - c)) {
|
|
2768
|
+
codewords[byteIdx] |= 1 << bitIdx;
|
|
2769
|
+
}
|
|
2770
|
+
bitIdx--;
|
|
2771
|
+
if (bitIdx === -1) { byteIdx++; bitIdx = 7; }
|
|
2772
|
+
}
|
|
2773
|
+
}
|
|
2774
|
+
row += inc;
|
|
2775
|
+
if (row < 0 || row >= n) { row -= inc; inc = -inc; break; }
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
const dcBlocks = blocks.map((b) => new Array(b.dataCount).fill(0));
|
|
2779
|
+
const ecBlocks = blocks.map((b) => new Array(b.totalCount - b.dataCount).fill(0));
|
|
2780
|
+
const maxDc = Math.max(...blocks.map((b) => b.dataCount));
|
|
2781
|
+
const maxEc = Math.max(...blocks.map((b) => b.totalCount - b.dataCount));
|
|
2782
|
+
let idx = 0;
|
|
2783
|
+
for (let i = 0; i < maxDc; i++) for (let r = 0; r < blocks.length; r++) if (i < blocks[r].dataCount) dcBlocks[r][i] = codewords[idx++];
|
|
2784
|
+
for (let i = 0; i < maxEc; i++) {
|
|
2785
|
+
for (let r = 0; r < blocks.length; r++) {
|
|
2786
|
+
if (i < blocks[r].totalCount - blocks[r].dataCount) ecBlocks[r][i] = codewords[idx++];
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
for (let r = 0; r < blocks.length; r++) {
|
|
2790
|
+
const rem = polyMod(dcBlocks[r].concat(ecBlocks[r]), rsGeneratorPoly(ecBlocks[r].length));
|
|
2791
|
+
if (rem.some((v) => v !== 0)) throw new Error(`QR Reed-Solomon check failed on block ${r}`);
|
|
2792
|
+
}
|
|
2793
|
+
const stream = new Uint8Array(totalDataBytes);
|
|
2794
|
+
let pos = 0;
|
|
2795
|
+
for (const dc of dcBlocks) for (const b of dc) stream[pos++] = b;
|
|
2796
|
+
let bitPos = 0;
|
|
2797
|
+
const readBits = (len) => {
|
|
2798
|
+
let val = 0;
|
|
2799
|
+
for (let i = 0; i < len; i++) {
|
|
2800
|
+
val = (val << 1) | ((stream[Math.floor(bitPos / 8)] >>> (7 - (bitPos % 8))) & 1);
|
|
2801
|
+
bitPos++;
|
|
2802
|
+
}
|
|
2803
|
+
return val;
|
|
2804
|
+
};
|
|
2805
|
+
const mode = readBits(4);
|
|
2806
|
+
if (mode !== 4) throw new Error(`Unsupported QR mode ${mode}`);
|
|
2807
|
+
const byteCount = readBits(version < 10 ? 8 : 16);
|
|
2808
|
+
const out = new Uint8Array(byteCount);
|
|
2809
|
+
for (let i = 0; i < byteCount; i++) out[i] = readBits(8);
|
|
2810
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(out);
|
|
2811
|
+
}
|
|
2812
|
+
|
|
2813
|
+
/**
|
|
2814
|
+
* Render a compact UTF-8 half-block terminal QR code that fits within an
|
|
2815
|
+
* 80-column terminal with high-contrast ANSI black modules on bright white background.
|
|
2816
|
+
* @param {string} url
|
|
2817
|
+
* @param {{ ecLevel?: "L"|"M"|"Q"|"H", quietZone?: number, indent?: string }} [opts]
|
|
2818
|
+
* @returns {string}
|
|
2819
|
+
*/
|
|
2820
|
+
export function renderTerminalQr(url, opts = {}) {
|
|
2821
|
+
const modules = encodeQrMatrix(url, opts.ecLevel ?? "M");
|
|
2822
|
+
const n = modules.length;
|
|
2823
|
+
let indent = opts.indent ?? " ";
|
|
2824
|
+
let quietZone = opts.quietZone;
|
|
2825
|
+
if (quietZone == null) {
|
|
2826
|
+
quietZone = n + 8 + indent.length <= 80 ? 4 : Math.max(2, Math.floor((80 - indent.length - n) / 2));
|
|
2827
|
+
}
|
|
2828
|
+
if (n + quietZone * 2 + indent.length > 80) {
|
|
2829
|
+
indent = "";
|
|
2830
|
+
quietZone = Math.max(1, Math.min(quietZone, Math.floor((80 - n) / 2)));
|
|
2831
|
+
}
|
|
2832
|
+
const total = n + quietZone * 2;
|
|
2833
|
+
const lines = [];
|
|
2834
|
+
for (let r = 0; r < total; r += 2) {
|
|
2835
|
+
const topQr = r - quietZone;
|
|
2836
|
+
const botQr = r + 1 - quietZone;
|
|
2837
|
+
const topRow = topQr >= 0 && topQr < n ? modules[topQr] : null;
|
|
2838
|
+
const botRow = botQr >= 0 && botQr < n ? modules[botQr] : null;
|
|
2839
|
+
let body = "";
|
|
2840
|
+
for (let c = 0; c < total; c++) {
|
|
2841
|
+
const qc = c - quietZone;
|
|
2842
|
+
const inCol = qc >= 0 && qc < n;
|
|
2843
|
+
const top = inCol && topRow ? Boolean(topRow[qc]) : false;
|
|
2844
|
+
const bot = inCol && botRow ? Boolean(botRow[qc]) : false;
|
|
2845
|
+
body += top && bot ? "\u2588" : top ? "\u2580" : bot ? "\u2584" : " ";
|
|
2846
|
+
}
|
|
2847
|
+
lines.push(`${indent}\x1b[30;107m${body}\x1b[0m`);
|
|
2848
|
+
}
|
|
2849
|
+
return lines.join("\n");
|
|
2850
|
+
}
|
|
2851
|
+
|
|
2852
|
+
/**
|
|
2853
|
+
* Unpack a rendered terminal QR string back into modules and decode its payload.
|
|
2854
|
+
* @param {string} rendered
|
|
2855
|
+
* @returns {string}
|
|
2856
|
+
*/
|
|
2857
|
+
export function decodeTerminalQr(rendered) {
|
|
2858
|
+
const rawLines = rendered
|
|
2859
|
+
.split("\n")
|
|
2860
|
+
.map((line) => line.replace(/\x1b\[[0-9;]*m/g, ""))
|
|
2861
|
+
.filter((line) => /^ *[\u2588\u2580\u2584 ]+$/.test(line) && line.length > 0);
|
|
2862
|
+
if (rawLines.length === 0) throw new Error("No QR rows found in terminal output");
|
|
2863
|
+
const grid = [];
|
|
2864
|
+
for (const line of rawLines) {
|
|
2865
|
+
const upper = [];
|
|
2866
|
+
const lower = [];
|
|
2867
|
+
for (const ch of line) {
|
|
2868
|
+
if (ch === "\u2588") { upper.push(true); lower.push(true); }
|
|
2869
|
+
else if (ch === "\u2580") { upper.push(true); lower.push(false); }
|
|
2870
|
+
else if (ch === "\u2584") { upper.push(false); lower.push(true); }
|
|
2871
|
+
else { upper.push(false); lower.push(false); }
|
|
2872
|
+
}
|
|
2873
|
+
grid.push(upper, lower);
|
|
2874
|
+
}
|
|
2875
|
+
let minR = Infinity;
|
|
2876
|
+
let maxR = -1;
|
|
2877
|
+
let minC = Infinity;
|
|
2878
|
+
let maxC = -1;
|
|
2879
|
+
for (let r = 0; r < grid.length; r++) {
|
|
2880
|
+
for (let c = 0; c < grid[r].length; c++) {
|
|
2881
|
+
if (grid[r][c]) {
|
|
2882
|
+
if (r < minR) minR = r;
|
|
2883
|
+
if (r > maxR) maxR = r;
|
|
2884
|
+
if (c < minC) minC = c;
|
|
2885
|
+
if (c > maxC) maxC = c;
|
|
2886
|
+
}
|
|
2887
|
+
}
|
|
2888
|
+
}
|
|
2889
|
+
const h = maxR - minR + 1;
|
|
2890
|
+
const w = maxC - minC + 1;
|
|
2891
|
+
if (w <= 0 || w !== h) throw new Error(`Non-square terminal QR bounds: ${w}x${h}`);
|
|
2892
|
+
const modules = [];
|
|
2893
|
+
for (let r = minR; r <= maxR; r++) modules.push(grid[r].slice(minC, maxC + 1));
|
|
2894
|
+
return decodeQrMatrix(modules);
|
|
2895
|
+
}
|
|
2896
|
+
|
|
2897
|
+
function formatTerminalQrBlock(url, opts = {}) {
|
|
2898
|
+
return ` Open on your phone (point your camera here):\n${renderTerminalQr(url, opts)}`;
|
|
2899
|
+
}
|