@yanzc/claude-pro 0.6.0 → 0.7.1
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/README.md +372 -370
- package/package.json +1 -1
- package/src/agent.js +18 -13
- package/src/attach.js +132 -0
- package/src/background.js +424 -0
- package/src/commands.js +80 -59
- package/src/connect.js +79 -0
- package/src/history.js +94 -0
- package/src/host.js +445 -66
- package/src/main.js +38 -10
- package/src/peers.js +290 -215
- package/src/print.js +0 -1
- package/src/prompts.js +2 -7
- package/src/remote.js +50 -15
- package/src/runtime.js +2 -6
- package/src/session.js +24 -0
- package/src/supervisor.js +6 -3
- package/src/tools/bash.js +2 -2
- package/src/tools/index.js +2 -3
- package/src/tools/interaction.js +2 -30
- package/src/tools/task.js +1 -1
- package/src/ui/ansi.js +0 -1
- package/src/ui/dialogs.js +48 -0
- package/src/ui/render.js +10 -4
- package/src/ui/repl.js +318 -77
- package/src/ui/vterm.js +62 -0
- package/src/modes.js +0 -154
- package/src/qr.js +0 -206
package/src/ui/vterm.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// The terminal of a background session (claude-pro --headless, started by the computer service): no window, the
|
|
2
|
+
// screen it draws goes to whoever is attached (`claude-pro attach`, see attach.js), and what they type comes in as
|
|
3
|
+
// if typed into a terminal. Nobody attached: the drawing goes nowhere, and the session is used from the web page.
|
|
4
|
+
//
|
|
5
|
+
// `output` stands for process.stdout and `input` for process.stdin as the App uses them (a TTY in raw mode, its
|
|
6
|
+
// size, 'resize'). The size is the one of the client that attached or resized last.
|
|
7
|
+
|
|
8
|
+
import { EventEmitter } from 'node:events';
|
|
9
|
+
import { PassThrough } from 'node:stream';
|
|
10
|
+
|
|
11
|
+
export class VirtualTerminal {
|
|
12
|
+
constructor({ columns = 120, rows = 40 } = {}) {
|
|
13
|
+
this.clients = new Set();
|
|
14
|
+
const out = new EventEmitter();
|
|
15
|
+
out.isTTY = true;
|
|
16
|
+
out.columns = columns;
|
|
17
|
+
out.rows = rows;
|
|
18
|
+
out.write = (data) => {
|
|
19
|
+
for (const client of this.clients) {
|
|
20
|
+
try { client.write(data); } catch { /* gone: its close removes it */ }
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
this.output = out;
|
|
25
|
+
const input = new PassThrough();
|
|
26
|
+
input.isTTY = true;
|
|
27
|
+
input.isRaw = true;
|
|
28
|
+
input.setRawMode = () => input;
|
|
29
|
+
this.input = input;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
resize(columns, rows) {
|
|
33
|
+
const c = Math.max(20, Math.min(1000, Math.floor(columns) || 0));
|
|
34
|
+
const r = Math.max(10, Math.min(500, Math.floor(rows) || 0));
|
|
35
|
+
if (!columns || !rows || (c === this.output.columns && r === this.output.rows)) return;
|
|
36
|
+
this.output.columns = c;
|
|
37
|
+
this.output.rows = r;
|
|
38
|
+
this.output.emit('resize');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// client: {write(data), close()}
|
|
42
|
+
attach(client, { cols, rows } = {}) {
|
|
43
|
+
this.clients.add(client);
|
|
44
|
+
this.resize(cols, rows);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
detach(client) {
|
|
48
|
+
this.clients.delete(client);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Keys from a client, as a terminal would send them.
|
|
52
|
+
type(data) {
|
|
53
|
+
this.input.write(data);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
closeAll() {
|
|
57
|
+
for (const client of [...this.clients]) {
|
|
58
|
+
try { client.close(); } catch { /* gone */ }
|
|
59
|
+
}
|
|
60
|
+
this.clients.clear();
|
|
61
|
+
}
|
|
62
|
+
}
|
package/src/modes.js
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
// Session mode. There are no permission prompts or rules: every tool call runs right away.
|
|
2
|
-
// The only switch is plan mode, an opt-in read-only phase for investigating and proposing a
|
|
3
|
-
// plan before changing anything (shift+tab or --plan turns it on; ExitPlanMode turns it off).
|
|
4
|
-
|
|
5
|
-
export const MODES = ['full', 'plan'];
|
|
6
|
-
|
|
7
|
-
// Split a shell command into simple commands on && || ; | and newlines (quote aware).
|
|
8
|
-
export function splitCommand(cmd) {
|
|
9
|
-
const parts = [];
|
|
10
|
-
let cur = '', quote = null;
|
|
11
|
-
for (let i = 0; i < cmd.length; i++) {
|
|
12
|
-
const ch = cmd[i];
|
|
13
|
-
if (quote) {
|
|
14
|
-
cur += ch;
|
|
15
|
-
if (ch === '\\' && quote === '"' && i + 1 < cmd.length) { cur += cmd[++i]; continue; }
|
|
16
|
-
if (ch === quote) quote = null;
|
|
17
|
-
continue;
|
|
18
|
-
}
|
|
19
|
-
if (ch === '"' || ch === "'") { quote = ch; cur += ch; continue; }
|
|
20
|
-
if (ch === '\\' && i + 1 < cmd.length) { cur += ch + cmd[++i]; continue; }
|
|
21
|
-
const two = cmd.slice(i, i + 2);
|
|
22
|
-
if (two === '&&' || two === '||') { parts.push(cur); cur = ''; i++; continue; }
|
|
23
|
-
if (ch === ';' || ch === '|' || ch === '\n') { parts.push(cur); cur = ''; continue; }
|
|
24
|
-
if (ch === '&' && cmd[i + 1] !== '>' && cmd[i - 1] !== '>') { parts.push(cur); cur = ''; continue; }
|
|
25
|
-
cur += ch;
|
|
26
|
-
}
|
|
27
|
-
parts.push(cur);
|
|
28
|
-
return parts.map((p) => p.trim()).filter(Boolean);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function stripQuotedStrings(s) {
|
|
32
|
-
return s.replace(/'[^']*'/g, "''").replace(/"(?:\\.|[^"\\])*"/g, '""');
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Syntax we can't classify: command substitution, redirection to files, etc.
|
|
36
|
-
function hasUnclassifiableSyntax(cmd) {
|
|
37
|
-
const s = stripQuotedStrings(cmd);
|
|
38
|
-
if (/\$\(|`|<\(|>\(/.test(s)) return true;
|
|
39
|
-
// Redirections other than to /dev/null or fd duplication.
|
|
40
|
-
const redir = s.replace(/\d?>&\d/g, '').replace(/&?>>?\s*\/dev\/null/g, '').replace(/\d?>>?\s*\/dev\/null/g, '');
|
|
41
|
-
return /[<>]/.test(redir);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Commands that read only, whatever their arguments (the ones whose arguments can make them write or run
|
|
45
|
-
// something have their own check in COMMAND_CHECKS below). No pagers: `less +!cmd` runs a command.
|
|
46
|
-
const READ_ONLY_SIMPLE = new Set([
|
|
47
|
-
'ls', 'pwd', 'echo', 'cat', 'head', 'tail', 'wc', 'which', 'whoami', 'stat', 'du', 'df',
|
|
48
|
-
'uname', 'printenv', 'type', 'realpath', 'basename', 'dirname', 'cut', 'tr', 'diff',
|
|
49
|
-
'grep', 'egrep', 'fgrep', 'true', 'false', 'test', 'nproc', 'id', 'cd', 'column', 'jq', 'md5sum', 'sha256sum',
|
|
50
|
-
]);
|
|
51
|
-
const READ_ONLY_PREFIXES = [
|
|
52
|
-
'git status', 'git rev-parse', 'git ls-files', 'git blame', 'git describe', 'git stash list',
|
|
53
|
-
'git config --get', 'git config -l', 'git config --list',
|
|
54
|
-
'node --version', 'node -v', 'npm --version', 'npm -v', 'npm ls', 'npm list', 'npm view', 'npm outdated', 'pnpm --version',
|
|
55
|
-
'yarn --version', 'python --version', 'python3 --version', 'pip list', 'pip show', 'pip --version', 'go version',
|
|
56
|
-
'cargo --version', 'rustc --version', 'java -version', 'dotnet --version', 'docker ps', 'docker images', 'where',
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
// `git branch` only lists with these flags; a name, -d/-D, -m/-M, -c/-C, -u ... would create, delete or rename.
|
|
60
|
-
const GIT_BRANCH_FLAGS = new Set(['-a', '--all', '-r', '--remotes', '-v', '-vv', '--verbose', '-l', '--list', '--show-current', '--no-color', '--color', '--no-column', '--column']);
|
|
61
|
-
const GIT_BRANCH_VALUE_FLAGS = /^--(?:contains|no-contains|merged|no-merged|points-at|sort|format|abbrev)(?:=.*)?$/;
|
|
62
|
-
function gitBranchReads(args) {
|
|
63
|
-
let listing = false;
|
|
64
|
-
for (let i = 0; i < args.length; i++) {
|
|
65
|
-
const a = args[i];
|
|
66
|
-
if (GIT_BRANCH_FLAGS.has(a)) { if (a === '-l' || a === '--list') listing = true; continue; }
|
|
67
|
-
if (GIT_BRANCH_VALUE_FLAGS.test(a)) { if (!a.includes('=') && !/^--abbrev$/.test(a)) i++; continue; }
|
|
68
|
-
if (listing && !a.startsWith('-')) continue; // a pattern to list
|
|
69
|
-
return false;
|
|
70
|
-
}
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// `git tag` likewise: listing is fine, anything that makes or removes a tag is not.
|
|
75
|
-
function gitTagReads(args) {
|
|
76
|
-
if (!args.some((a) => a === '-l' || a === '--list')) return false;
|
|
77
|
-
return args.every((a) => !a.startsWith('-') || /^(?:-l|--list|-n\d*|--sort=.*|--column|--no-column|--contains|--no-contains|--merged|--no-merged|--points-at|--format=.*|--color|--no-color)$/.test(a));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const positional = (args) => args.filter((a) => !a.startsWith('-') && !a.startsWith('+'));
|
|
81
|
-
|
|
82
|
-
// Commands that are read-only only with certain arguments: args => true when this call reads only.
|
|
83
|
-
const COMMAND_CHECKS = {
|
|
84
|
-
// -exec and -delete run or remove things, -fprint* and -fls write files
|
|
85
|
-
find: (args) => !args.some((a) => /^-(?:exec|execdir|ok|okdir|delete|fprint0?|fprintf|fls)$/.test(a)),
|
|
86
|
-
// -o FILE and --output FILE (GNU getopt accepts any unambiguous prefix of it)
|
|
87
|
-
sort: (args) => !args.some((a) => /^-[a-zA-Z]*o/.test(a) || /^--o/.test(a)),
|
|
88
|
-
// `uniq INPUT OUTPUT` writes OUTPUT
|
|
89
|
-
uniq: (args) => positional(args).length <= 1,
|
|
90
|
-
tree: (args) => !args.some((a) => /^-[a-zA-Z]*o/.test(a) || /^--output/.test(a)),
|
|
91
|
-
// setting the clock or the host name changes the machine
|
|
92
|
-
date: (args) => !args.some((a) => /^-[a-zA-Z]*s/.test(a) || /^--set/.test(a)) && positional(args).length === 0,
|
|
93
|
-
hostname: (args) => args.length === 0 || args.every((a) => /^(?:-[fsiIdAy]|--(?:fqdn|short|ip-address|domain|all-ip-addresses))$/.test(a)),
|
|
94
|
-
// --pre runs a command on every file
|
|
95
|
-
rg: (args) => !args.some((a) => /^--(?:pre|hostname-bin)(?:=|$)/.test(a)),
|
|
96
|
-
file: (args) => !args.some((a) => /^-[a-zA-Z]*C/.test(a) || a === '--compile'),
|
|
97
|
-
// `git log --output=FILE` and friends write the result to a file
|
|
98
|
-
git: (args, part) => {
|
|
99
|
-
const sub = args.find((a) => !a.startsWith('-'));
|
|
100
|
-
const rest = args.slice(args.indexOf(sub) + 1);
|
|
101
|
-
if (args[0] !== sub) return null; // options before the subcommand (-C dir, -c key=value): not classified
|
|
102
|
-
if (sub === 'diff' || sub === 'log' || sub === 'show') return !rest.some((a) => /^--output(?:=|$)/.test(a));
|
|
103
|
-
if (sub === 'branch') return gitBranchReads(rest);
|
|
104
|
-
if (sub === 'tag') return gitTagReads(rest);
|
|
105
|
-
if (sub === 'remote') return part === 'git remote' || part === 'git remote -v';
|
|
106
|
-
return null;
|
|
107
|
-
},
|
|
108
|
-
// `go env -w` and `-u` change the Go settings
|
|
109
|
-
go: (args) => (args[0] === 'env' ? !args.some((a) => a === '-w' || a === '-u') : args[0] === 'version'),
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// True when every part of the command only reads (used to let research commands run in plan mode).
|
|
113
|
-
export function isReadOnlyCommand(cmd) {
|
|
114
|
-
if (hasUnclassifiableSyntax(cmd)) return false;
|
|
115
|
-
return splitCommand(cmd).every((part) => {
|
|
116
|
-
const words = part.split(/\s+/);
|
|
117
|
-
const first = words[0];
|
|
118
|
-
if (/^\w+=/.test(first)) return false; // env assignment prefix
|
|
119
|
-
// hasOwn: a command called `constructor` or `toString` must not find a function on the prototype.
|
|
120
|
-
if (Object.hasOwn(COMMAND_CHECKS, first)) {
|
|
121
|
-
const verdict = COMMAND_CHECKS[first](words.slice(1), part);
|
|
122
|
-
if (verdict !== null) return verdict;
|
|
123
|
-
}
|
|
124
|
-
if (READ_ONLY_SIMPLE.has(first)) return true;
|
|
125
|
-
// A command that has an argument check is only ever read-only through that check, except where it
|
|
126
|
-
// is also in the simple list (above); the rest is matched by prefix.
|
|
127
|
-
return READ_ONLY_PREFIXES.some((p) => part === p || part.startsWith(`${p} `));
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export class ModeManager {
|
|
132
|
-
constructor(mode = 'full') {
|
|
133
|
-
this.mode = MODES.includes(mode) ? mode : 'full';
|
|
134
|
-
this.listeners = new Set();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
setMode(mode) {
|
|
138
|
-
if (!MODES.includes(mode)) return;
|
|
139
|
-
this.mode = mode;
|
|
140
|
-
for (const l of this.listeners) l(mode);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
toggle() {
|
|
144
|
-
this.setMode(this.mode === 'plan' ? 'full' : 'plan');
|
|
145
|
-
return this.mode;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// {behavior: 'allow'} or, in plan mode for anything that changes state, {behavior: 'deny', message}.
|
|
149
|
-
check(tool, input = {}) {
|
|
150
|
-
if (this.mode !== 'plan' || tool.readOnly) return { behavior: 'allow' };
|
|
151
|
-
if (tool.name === 'Bash' && isReadOnlyCommand(String(input.command || ''))) return { behavior: 'allow' };
|
|
152
|
-
return { behavior: 'deny', message: 'Plan mode is active: do not make changes or run commands that modify anything yet. Keep researching with read-only tools, then present your plan with ExitPlanMode.' };
|
|
153
|
-
}
|
|
154
|
-
}
|
package/src/qr.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
// QR codes in the terminal: /status prints one of the session's page, so that a phone opens it by pointing its camera
|
|
2
|
-
// at the screen. Byte mode, error correction level L, versions 1-6 (up to 134 bytes, which a URL with a token
|
|
3
|
-
// fits in easily); the mask is chosen with the standard penalty rules.
|
|
4
|
-
|
|
5
|
-
// [EC codewords per block, blocks, data codewords per block] at level L, by version.
|
|
6
|
-
const EC_L = [null, [7, 1, 19], [10, 1, 34], [15, 1, 55], [20, 1, 80], [26, 1, 108], [18, 2, 68]];
|
|
7
|
-
const MAX_VERSION = 6;
|
|
8
|
-
|
|
9
|
-
// GF(256) with the polynomial 0x11d.
|
|
10
|
-
const EXP = new Uint8Array(512);
|
|
11
|
-
const LOG = new Uint8Array(256);
|
|
12
|
-
for (let i = 0, x = 1; i < 255; i++) {
|
|
13
|
-
EXP[i] = x;
|
|
14
|
-
LOG[x] = i;
|
|
15
|
-
x <<= 1;
|
|
16
|
-
if (x & 0x100) x ^= 0x11d;
|
|
17
|
-
}
|
|
18
|
-
for (let i = 255; i < 512; i++) EXP[i] = EXP[i - 255];
|
|
19
|
-
const mul = (a, b) => (a && b ? EXP[LOG[a] + LOG[b]] : 0);
|
|
20
|
-
|
|
21
|
-
function rsRemainder(data, n) {
|
|
22
|
-
let gen = [1]; // (x - a^0)(x - a^1)...(x - a^(n-1)), highest degree first
|
|
23
|
-
for (let i = 0; i < n; i++) {
|
|
24
|
-
const next = new Array(gen.length + 1).fill(0);
|
|
25
|
-
for (let j = 0; j < gen.length; j++) { next[j] ^= gen[j]; next[j + 1] ^= mul(gen[j], EXP[i]); }
|
|
26
|
-
gen = next;
|
|
27
|
-
}
|
|
28
|
-
const rem = new Array(n).fill(0);
|
|
29
|
-
for (const byte of data) {
|
|
30
|
-
const factor = byte ^ rem.shift();
|
|
31
|
-
rem.push(0);
|
|
32
|
-
for (let i = 0; i < n; i++) rem[i] ^= mul(gen[i + 1], factor);
|
|
33
|
-
}
|
|
34
|
-
return rem;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function codewords(bytes, version) {
|
|
38
|
-
const [ecLen, blocks, dataLen] = EC_L[version];
|
|
39
|
-
const capacity = blocks * dataLen;
|
|
40
|
-
const bits = [];
|
|
41
|
-
const put = (value, len) => { for (let i = len - 1; i >= 0; i--) bits.push((value >>> i) & 1); };
|
|
42
|
-
put(0b0100, 4);
|
|
43
|
-
put(bytes.length, 8);
|
|
44
|
-
for (const b of bytes) put(b, 8);
|
|
45
|
-
put(0, Math.min(4, capacity * 8 - bits.length));
|
|
46
|
-
while (bits.length % 8) bits.push(0);
|
|
47
|
-
const data = [];
|
|
48
|
-
for (let i = 0; i < bits.length; i += 8) data.push(bits.slice(i, i + 8).reduce((v, b) => (v << 1) | b, 0));
|
|
49
|
-
for (let pad = 0xec; data.length < capacity; pad ^= 0xec ^ 0x11) data.push(pad);
|
|
50
|
-
const dataBlocks = [];
|
|
51
|
-
const ecBlocks = [];
|
|
52
|
-
for (let b = 0; b < blocks; b++) {
|
|
53
|
-
const d = data.slice(b * dataLen, (b + 1) * dataLen);
|
|
54
|
-
dataBlocks.push(d);
|
|
55
|
-
ecBlocks.push(rsRemainder(d, ecLen));
|
|
56
|
-
}
|
|
57
|
-
const out = [];
|
|
58
|
-
for (let i = 0; i < dataLen; i++) for (const d of dataBlocks) out.push(d[i]);
|
|
59
|
-
for (let i = 0; i < ecLen; i++) for (const e of ecBlocks) out.push(e[i]);
|
|
60
|
-
return out;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const MASKS = [
|
|
64
|
-
(x, y) => (x + y) % 2 === 0,
|
|
65
|
-
(x, y) => y % 2 === 0,
|
|
66
|
-
(x) => x % 3 === 0,
|
|
67
|
-
(x, y) => (x + y) % 3 === 0,
|
|
68
|
-
(x, y) => (Math.floor(x / 3) + Math.floor(y / 2)) % 2 === 0,
|
|
69
|
-
(x, y) => ((x * y) % 2) + ((x * y) % 3) === 0,
|
|
70
|
-
(x, y) => (((x * y) % 2) + ((x * y) % 3)) % 2 === 0,
|
|
71
|
-
(x, y) => (((x + y) % 2) + ((x * y) % 3)) % 2 === 0,
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
function penalty(m) {
|
|
75
|
-
const n = m.length;
|
|
76
|
-
let score = 0;
|
|
77
|
-
let dark = 0;
|
|
78
|
-
const lines = [];
|
|
79
|
-
for (let i = 0; i < n; i++) {
|
|
80
|
-
lines.push(m[i]);
|
|
81
|
-
lines.push(m.map((row) => row[i]));
|
|
82
|
-
}
|
|
83
|
-
for (const line of lines) {
|
|
84
|
-
for (let i = 0; i < n;) {
|
|
85
|
-
let j = i;
|
|
86
|
-
while (j < n && line[j] === line[i]) j++;
|
|
87
|
-
if (j - i >= 5) score += 3 + (j - i - 5);
|
|
88
|
-
i = j;
|
|
89
|
-
}
|
|
90
|
-
const s = line.map((v) => (v ? 1 : 0)).join('');
|
|
91
|
-
for (const p of ['10111010000', '00001011101']) for (let k = s.indexOf(p); k >= 0; k = s.indexOf(p, k + 1)) score += 40;
|
|
92
|
-
}
|
|
93
|
-
for (let y = 0; y < n; y++) {
|
|
94
|
-
for (let x = 0; x < n; x++) {
|
|
95
|
-
if (m[y][x]) dark++;
|
|
96
|
-
if (x < n - 1 && y < n - 1 && m[y][x] === m[y][x + 1] && m[y][x] === m[y + 1][x] && m[y][x] === m[y + 1][x + 1]) score += 3;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return score + 10 * Math.floor(Math.abs((dark * 100) / (n * n) - 50) / 5);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// The modules of the QR code for `text` (rows of booleans, true = dark), or null when it is too long.
|
|
103
|
-
// `mask`: use this mask (0-7) instead of the best one.
|
|
104
|
-
export function qrMatrix(text, { mask: forcedMask } = {}) {
|
|
105
|
-
const bytes = [...Buffer.from(String(text), 'utf8')];
|
|
106
|
-
let version = 1;
|
|
107
|
-
while (version <= MAX_VERSION && EC_L[version][1] * EC_L[version][2] * 8 < 12 + 8 * bytes.length) version++;
|
|
108
|
-
if (version > MAX_VERSION) return null;
|
|
109
|
-
const size = 17 + 4 * version;
|
|
110
|
-
const base = Array.from({ length: size }, () => new Array(size).fill(false));
|
|
111
|
-
const fn = Array.from({ length: size }, () => new Array(size).fill(false));
|
|
112
|
-
const set = (m, x, y, dark) => { m[y][x] = dark; fn[y][x] = true; };
|
|
113
|
-
|
|
114
|
-
for (const [cx, cy] of [[3, 3], [size - 4, 3], [3, size - 4]]) {
|
|
115
|
-
for (let dy = -4; dy <= 4; dy++) {
|
|
116
|
-
for (let dx = -4; dx <= 4; dx++) {
|
|
117
|
-
const x = cx + dx;
|
|
118
|
-
const y = cy + dy;
|
|
119
|
-
if (x < 0 || y < 0 || x >= size || y >= size) continue;
|
|
120
|
-
const d = Math.max(Math.abs(dx), Math.abs(dy));
|
|
121
|
-
set(base, x, y, d !== 2 && d !== 4);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
for (let i = 0; i < size; i++) {
|
|
126
|
-
if (!fn[6][i]) set(base, i, 6, i % 2 === 0);
|
|
127
|
-
if (!fn[i][6]) set(base, 6, i, i % 2 === 0);
|
|
128
|
-
}
|
|
129
|
-
if (version >= 2) {
|
|
130
|
-
const c = size - 7;
|
|
131
|
-
for (let dy = -2; dy <= 2; dy++) for (let dx = -2; dx <= 2; dx++) set(base, c + dx, c + dy, Math.max(Math.abs(dx), Math.abs(dy)) !== 1);
|
|
132
|
-
}
|
|
133
|
-
const drawFormat = (m, mask) => {
|
|
134
|
-
const data = (1 << 3) | mask; // level L = 01
|
|
135
|
-
let rem = data;
|
|
136
|
-
for (let i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
|
|
137
|
-
const bits = ((data << 10) | rem) ^ 0x5412;
|
|
138
|
-
const bit = (i) => ((bits >>> i) & 1) === 1;
|
|
139
|
-
for (let i = 0; i <= 5; i++) set(m, 8, i, bit(i));
|
|
140
|
-
set(m, 8, 7, bit(6));
|
|
141
|
-
set(m, 8, 8, bit(7));
|
|
142
|
-
set(m, 7, 8, bit(8));
|
|
143
|
-
for (let i = 9; i < 15; i++) set(m, 14 - i, 8, bit(i));
|
|
144
|
-
for (let i = 0; i < 8; i++) set(m, size - 1 - i, 8, bit(i));
|
|
145
|
-
for (let i = 8; i < 15; i++) set(m, 8, size - 15 + i, bit(i));
|
|
146
|
-
set(m, 8, size - 8, true);
|
|
147
|
-
};
|
|
148
|
-
drawFormat(base, 0); // reserves the format areas before the data goes in
|
|
149
|
-
|
|
150
|
-
const data = codewords(bytes, version);
|
|
151
|
-
let bitIndex = 0;
|
|
152
|
-
for (let right = size - 1; right >= 1; right -= 2) {
|
|
153
|
-
if (right === 6) right = 5;
|
|
154
|
-
for (let vert = 0; vert < size; vert++) {
|
|
155
|
-
for (let j = 0; j < 2; j++) {
|
|
156
|
-
const x = right - j;
|
|
157
|
-
const upward = ((right + 1) & 2) === 0;
|
|
158
|
-
const y = upward ? size - 1 - vert : vert;
|
|
159
|
-
if (fn[y][x]) continue;
|
|
160
|
-
if (bitIndex < data.length * 8) base[y][x] = ((data[bitIndex >>> 3] >>> (7 - (bitIndex & 7))) & 1) === 1;
|
|
161
|
-
bitIndex++;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
let best = null;
|
|
167
|
-
for (let mask = 0; mask < 8; mask++) {
|
|
168
|
-
if (forcedMask !== undefined && mask !== forcedMask) continue;
|
|
169
|
-
const m = base.map((row, y) => row.map((v, x) => (fn[y][x] ? v : v !== MASKS[mask](x, y))));
|
|
170
|
-
drawFormat(m, mask);
|
|
171
|
-
const score = forcedMask !== undefined ? 0 : penalty(m);
|
|
172
|
-
if (!best || score < best.score) best = { m, score };
|
|
173
|
-
}
|
|
174
|
-
return best.m;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
// The QR code as terminal lines, two rows of modules per line. With colors it is drawn black on white whatever the
|
|
178
|
-
// terminal's theme; without, it assumes a dark background (light modules are the drawn ones). Null when too long.
|
|
179
|
-
export function qrLines(text, { color = true, quiet = 2 } = {}) {
|
|
180
|
-
const m = qrMatrix(text);
|
|
181
|
-
if (!m) return null;
|
|
182
|
-
const n = m.length + quiet * 2;
|
|
183
|
-
const dark = (x, y) => {
|
|
184
|
-
const mx = x - quiet;
|
|
185
|
-
const my = y - quiet;
|
|
186
|
-
return mx >= 0 && my >= 0 && mx < m.length && my < m.length && m[my][mx];
|
|
187
|
-
};
|
|
188
|
-
const lines = [];
|
|
189
|
-
for (let y = 0; y < n; y += 2) {
|
|
190
|
-
let line = '';
|
|
191
|
-
let last = null;
|
|
192
|
-
for (let x = 0; x < n; x++) {
|
|
193
|
-
const top = dark(x, y);
|
|
194
|
-
const bottom = y + 1 < n && dark(x, y + 1);
|
|
195
|
-
if (color) {
|
|
196
|
-
const sgr = `\x1b[${top ? 30 : 97};${bottom ? 40 : 107}m`;
|
|
197
|
-
if (sgr !== last) { line += sgr; last = sgr; }
|
|
198
|
-
line += '▀';
|
|
199
|
-
} else {
|
|
200
|
-
line += !top && !bottom ? '█' : !top ? '▀' : !bottom ? '▄' : ' ';
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
lines.push(color ? `${line}\x1b[0m` : line);
|
|
204
|
-
}
|
|
205
|
-
return lines;
|
|
206
|
-
}
|