@noisybrood/cli 0.0.0-stage → 0.1.0
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/LICENSE +202 -0
- package/README.md +74 -2
- package/dist/brood/api/src/fields.js +335 -0
- package/dist/brood/api/src/index.js +528 -0
- package/dist/brood/api/src/values.js +59 -0
- package/dist/brood/figures/index.js +10 -0
- package/dist/brood/tokens/dark.css +86 -0
- package/dist/brood/tokens/src/contrast.js +109 -0
- package/dist/brood/tokens/src/wren.js +58 -0
- package/dist/brood.js +11 -0
- package/dist/client.js +100 -0
- package/dist/commands.js +417 -0
- package/dist/io.js +50 -0
- package/dist/main.js +193 -0
- package/dist/out.js +53 -0
- package/dist/sign-in.js +129 -0
- package/dist/store.js +83 -0
- package/dist/version.js +16 -0
- package/dist/wren.js +153 -0
- package/package.json +37 -4
package/dist/out.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a terminal may be shown colour: it is one, nothing asked it not
|
|
3
|
+
* to, and NO_COLOR is not set, whatever its value, as no-color.org says.
|
|
4
|
+
*/
|
|
5
|
+
export const colorOn = (io, noColor) => io.tty && !noColor && io.env.NO_COLOR === undefined;
|
|
6
|
+
export const styleOf = (io, flags) => ({
|
|
7
|
+
json: flags.json,
|
|
8
|
+
color: colorOn(io, flags.noColor),
|
|
9
|
+
});
|
|
10
|
+
export const dim = (style, text) => style.color ? `\u001b[2m${text}\u001b[22m` : text;
|
|
11
|
+
/** How wide a cell shows: its characters, without the codes that dim it. */
|
|
12
|
+
const width = (cell) => [...cell.replace(/\u001b\[\d+m/g, "")].length;
|
|
13
|
+
/**
|
|
14
|
+
* Rows as columns, each as wide as its widest cell; the last is left as it
|
|
15
|
+
* is. The cells are already clean: the answer was cleaned as it arrived.
|
|
16
|
+
*/
|
|
17
|
+
export function rows(io, cells, line = io.out) {
|
|
18
|
+
const widths = [];
|
|
19
|
+
for (const row of cells)
|
|
20
|
+
row.forEach((cell, i) => {
|
|
21
|
+
widths[i] = Math.max(widths[i] ?? 0, width(cell));
|
|
22
|
+
});
|
|
23
|
+
for (const row of cells)
|
|
24
|
+
line(row
|
|
25
|
+
.map((cell, i) => i === row.length - 1
|
|
26
|
+
? cell
|
|
27
|
+
: cell + " ".repeat((widths[i] ?? 0) - width(cell)))
|
|
28
|
+
.join(" ")
|
|
29
|
+
.trimEnd());
|
|
30
|
+
}
|
|
31
|
+
/** A day, from a timestamp the API gave. */
|
|
32
|
+
export const day = (at) => (at ? at.slice(0, 10) : "");
|
|
33
|
+
/**
|
|
34
|
+
* Before an answer, what the person must know to trust it (DD21 §8): every
|
|
35
|
+
* mailbox Brood could not read whole. A disconnected one is said too, so its
|
|
36
|
+
* silence is not read as quiet, though it leaves the answer complete.
|
|
37
|
+
*/
|
|
38
|
+
export function state(io, style, envelope) {
|
|
39
|
+
// Each mailbox the answer did not read whole, as the API judges it, a
|
|
40
|
+
// current one too (#1369).
|
|
41
|
+
const unread = envelope.accounts.filter((a) => !a.whole);
|
|
42
|
+
for (const a of unread)
|
|
43
|
+
io.err(dim(style, `Mailbox ${a.id}: ${a.sync.phase === "current" ? "current, but not read whole for this answer" : a.sync.phase}${a.sync.last_synced_at ? `, last reached ${day(a.sync.last_synced_at)}` : ""}.`));
|
|
44
|
+
if (!envelope.complete)
|
|
45
|
+
io.err(dim(style, "This answer is not complete: Brood has not read every connected mailbox whole."));
|
|
46
|
+
}
|
|
47
|
+
/** Where a listing carries on, said after it. */
|
|
48
|
+
export function more(io, style, envelope) {
|
|
49
|
+
if (envelope.next)
|
|
50
|
+
io.err(dim(style, `More: run it again with --cursor ${envelope.next}`));
|
|
51
|
+
}
|
|
52
|
+
/** The answer as the API gave it, cleaned, for a program to read. */
|
|
53
|
+
export const json = (io, envelope) => io.out(JSON.stringify(envelope, null, 2));
|
package/dist/sign-in.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { SIGN_IN } from "./brood/api/src/index.js";
|
|
2
|
+
import { clean, isRecord, Refusal, SessionEnded } from "./io.js";
|
|
3
|
+
import { readCredentials, removeCredentials, writeCredentials, } from "./store.js";
|
|
4
|
+
import { colorOn } from "./out.js";
|
|
5
|
+
import { depthOf, wrenIn } from "./wren.js";
|
|
6
|
+
/**
|
|
7
|
+
* Signing in with Clerk's device authorization grant (DD10 §6, DD21 §4): a
|
|
8
|
+
* code shown here and approved in a browser, so no secret is ever typed into
|
|
9
|
+
* a terminal. The access token lasts a day; the refresh token beside it gets
|
|
10
|
+
* the next, and Clerk gives a new one each time it is used.
|
|
11
|
+
*/
|
|
12
|
+
const CLIENT_ID = SIGN_IN.clients.cli;
|
|
13
|
+
const TOKEN_URL = `${SIGN_IN.issuer}/oauth/token`;
|
|
14
|
+
const DEVICE_URL = `${SIGN_IN.issuer}/oauth/device_authorization`;
|
|
15
|
+
const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
|
|
16
|
+
/** Refreshed this long before it expires, so a call is not made on a token about to lapse. */
|
|
17
|
+
const EARLY_MS = 60_000;
|
|
18
|
+
async function post(io, url, fields) {
|
|
19
|
+
let response;
|
|
20
|
+
try {
|
|
21
|
+
response = await io.fetch(url, {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
24
|
+
body: new URLSearchParams(fields).toString(),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new Refusal("Could not reach the sign-in service. Check the connection and try again.");
|
|
29
|
+
}
|
|
30
|
+
let body;
|
|
31
|
+
try {
|
|
32
|
+
body = await response.json();
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
body = null;
|
|
36
|
+
}
|
|
37
|
+
return { status: response.status, body: isRecord(body) ? body : {} };
|
|
38
|
+
}
|
|
39
|
+
/** A token answer as Credentials, or null when it is not one. */
|
|
40
|
+
function credentialsOf(body, now) {
|
|
41
|
+
const { access_token, refresh_token, expires_in, scope } = body;
|
|
42
|
+
if (typeof access_token !== "string" ||
|
|
43
|
+
typeof refresh_token !== "string" ||
|
|
44
|
+
typeof expires_in !== "number" ||
|
|
45
|
+
typeof scope !== "string")
|
|
46
|
+
return null;
|
|
47
|
+
return {
|
|
48
|
+
access_token,
|
|
49
|
+
refresh_token,
|
|
50
|
+
expires_at: now + expires_in * 1000,
|
|
51
|
+
scope,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const acts = (scope) => scope.split(" ").includes("act");
|
|
55
|
+
export async function login(io, readOnly, noColor = false) {
|
|
56
|
+
if (await readCredentials(io.env))
|
|
57
|
+
throw new Refusal("Already signed in. Run brood logout first, which ends this session.");
|
|
58
|
+
const scope = readOnly ? "read offline_access" : "read act offline_access";
|
|
59
|
+
const started = await post(io, DEVICE_URL, { client_id: CLIENT_ID, scope });
|
|
60
|
+
const { device_code, user_code, verification_uri, expires_in, interval } = started.body;
|
|
61
|
+
if (typeof device_code !== "string" ||
|
|
62
|
+
typeof user_code !== "string" ||
|
|
63
|
+
typeof verification_uri !== "string" ||
|
|
64
|
+
typeof expires_in !== "number")
|
|
65
|
+
throw new Refusal("The sign-in service would not start a sign-in. Try again shortly.");
|
|
66
|
+
io.err(`Open ${clean(verification_uri)} and enter the code ${clean(user_code)}.`);
|
|
67
|
+
io.err("Waiting for you to approve it there.");
|
|
68
|
+
let wait = (typeof interval === "number" ? interval : 5) * 1000;
|
|
69
|
+
const deadline = io.now() + expires_in * 1000;
|
|
70
|
+
while (io.now() < deadline) {
|
|
71
|
+
await io.sleep(wait);
|
|
72
|
+
const answer = await post(io, TOKEN_URL, {
|
|
73
|
+
grant_type: DEVICE_GRANT,
|
|
74
|
+
device_code,
|
|
75
|
+
client_id: CLIENT_ID,
|
|
76
|
+
});
|
|
77
|
+
const error = answer.body.error;
|
|
78
|
+
if (error === "authorization_pending")
|
|
79
|
+
continue;
|
|
80
|
+
// RFC 8628 §3.5: a client told to slow down waits five seconds longer from then on.
|
|
81
|
+
if (error === "slow_down") {
|
|
82
|
+
wait += 5000;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (error === "access_denied")
|
|
86
|
+
throw new Refusal("The sign-in was declined.");
|
|
87
|
+
if (error === "expired_token")
|
|
88
|
+
break;
|
|
89
|
+
const credentials = credentialsOf(answer.body, io.now());
|
|
90
|
+
if (!credentials)
|
|
91
|
+
throw new Refusal("The sign-in service did not finish the sign-in. Run brood login again.");
|
|
92
|
+
await writeCredentials(io.env, credentials);
|
|
93
|
+
const color = colorOn(io, noColor);
|
|
94
|
+
for (const line of wrenIn(color ? depthOf(io.env) : "none"))
|
|
95
|
+
io.err(line);
|
|
96
|
+
io.err("");
|
|
97
|
+
io.err(acts(credentials.scope)
|
|
98
|
+
? "Signed in. brood can read your index, and hush and unhush senders."
|
|
99
|
+
: "Signed in. brood can read your index, and change nothing.");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
throw new Refusal("The code expired before it was approved. Run brood login again.");
|
|
103
|
+
}
|
|
104
|
+
/** The session, refreshed first if its access token is about to expire. */
|
|
105
|
+
export async function freshCredentials(io) {
|
|
106
|
+
const stored = await readCredentials(io.env);
|
|
107
|
+
if (!stored)
|
|
108
|
+
throw new Refusal("Not signed in. Run brood login.");
|
|
109
|
+
return stored.expires_at - EARLY_MS > io.now() ? stored : refresh(io, stored);
|
|
110
|
+
}
|
|
111
|
+
/** A new access token from the refresh token, kept with the new refresh token Clerk gives. */
|
|
112
|
+
export async function refresh(io, stored) {
|
|
113
|
+
const answer = await post(io, TOKEN_URL, {
|
|
114
|
+
grant_type: "refresh_token",
|
|
115
|
+
refresh_token: stored.refresh_token,
|
|
116
|
+
client_id: CLIENT_ID,
|
|
117
|
+
});
|
|
118
|
+
const credentials = credentialsOf(answer.body, io.now());
|
|
119
|
+
if (credentials) {
|
|
120
|
+
await writeCredentials(io.env, credentials);
|
|
121
|
+
return credentials;
|
|
122
|
+
}
|
|
123
|
+
if (answer.body.error === "invalid_grant") {
|
|
124
|
+
// The session is over at Clerk, so the file holds nothing that works.
|
|
125
|
+
await removeCredentials(io.env);
|
|
126
|
+
throw new SessionEnded("This session has ended. Run brood login to sign in again.");
|
|
127
|
+
}
|
|
128
|
+
throw new Refusal("The sign-in service would not renew this session. Try again shortly.");
|
|
129
|
+
}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { mkdir, readdir, readFile, rename, rm, stat, writeFile, } from "node:fs/promises";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import { Refusal } from "./io.js";
|
|
6
|
+
export const credentialsPath = (env) => join(env.XDG_CONFIG_HOME || join(env.HOME || homedir(), ".config"), "brood", "credentials.json");
|
|
7
|
+
const isCredentials = (v) => typeof v === "object" &&
|
|
8
|
+
v !== null &&
|
|
9
|
+
"access_token" in v &&
|
|
10
|
+
typeof v.access_token === "string" &&
|
|
11
|
+
"refresh_token" in v &&
|
|
12
|
+
typeof v.refresh_token === "string" &&
|
|
13
|
+
"expires_at" in v &&
|
|
14
|
+
typeof v.expires_at === "number" &&
|
|
15
|
+
"scope" in v &&
|
|
16
|
+
typeof v.scope === "string";
|
|
17
|
+
/** The stored session, or null when there is none. */
|
|
18
|
+
export async function readCredentials(env) {
|
|
19
|
+
const path = credentialsPath(env);
|
|
20
|
+
let mode;
|
|
21
|
+
try {
|
|
22
|
+
mode = (await stat(path)).mode;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof Error && "code" in error && error.code === "ENOENT")
|
|
26
|
+
return null;
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
// As ssh does with a key: a file others can read is refused, not used.
|
|
30
|
+
// Not on Windows, where Node reports every file as readable by all: there
|
|
31
|
+
// the file sits in the person's own profile, under its ACL (DD10 §6).
|
|
32
|
+
if (process.platform !== "win32" && mode & 0o077)
|
|
33
|
+
throw new Refusal(`${path} can be read by others. Delete it and run brood login again.`);
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = JSON.parse(await readFile(path, "utf8"));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
parsed = null;
|
|
40
|
+
}
|
|
41
|
+
if (!isCredentials(parsed))
|
|
42
|
+
throw new Refusal(`${path} is not a session brood wrote. Delete it and run brood login again.`);
|
|
43
|
+
return parsed;
|
|
44
|
+
}
|
|
45
|
+
/** Writes the session where only its owner can read it, whole or not at all. */
|
|
46
|
+
export async function writeCredentials(env, credentials) {
|
|
47
|
+
const path = credentialsPath(env);
|
|
48
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
49
|
+
// A name no earlier run left behind, created afresh ("wx"), so the mode
|
|
50
|
+
// it is given is the mode it has; a reused draft keeps its old bits.
|
|
51
|
+
const draft = `${path}.${randomUUID()}.tmp`;
|
|
52
|
+
try {
|
|
53
|
+
await writeFile(draft, JSON.stringify(credentials), {
|
|
54
|
+
mode: 0o600,
|
|
55
|
+
flag: "wx",
|
|
56
|
+
});
|
|
57
|
+
await rename(draft, path);
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
// Gone once renamed; a draft a failed write left is removed with it.
|
|
61
|
+
await rm(draft, { force: true });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Forgets the session: the file, and any draft of it a run that stopped
|
|
66
|
+
* between writing and renaming left behind, which holds a token too.
|
|
67
|
+
*/
|
|
68
|
+
export async function removeCredentials(env) {
|
|
69
|
+
const path = credentialsPath(env);
|
|
70
|
+
const draft = `${basename(path)}.`;
|
|
71
|
+
let names = [];
|
|
72
|
+
try {
|
|
73
|
+
names = await readdir(dirname(path));
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (!(error instanceof Error && "code" in error && error.code === "ENOENT"))
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
for (const name of names)
|
|
80
|
+
if (name.startsWith(draft) && name.endsWith(".tmp"))
|
|
81
|
+
await rm(join(dirname(path), name), { force: true });
|
|
82
|
+
await rm(path, { force: true });
|
|
83
|
+
}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
/**
|
|
3
|
+
* The CLI's version, from its own manifest, so the two cannot differ. Read
|
|
4
|
+
* beside the module rather than imported: the published package compiles
|
|
5
|
+
* src/ to dist/, and a JSON import from above src/ would sit outside what
|
|
6
|
+
* is compiled; one level up is the manifest from either.
|
|
7
|
+
*/
|
|
8
|
+
export const VERSION = (() => {
|
|
9
|
+
const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
10
|
+
if (typeof manifest !== "object" ||
|
|
11
|
+
manifest === null ||
|
|
12
|
+
!("version" in manifest) ||
|
|
13
|
+
typeof manifest.version !== "string")
|
|
14
|
+
throw new Error("the CLI's package.json names no version");
|
|
15
|
+
return manifest.version;
|
|
16
|
+
})();
|
package/dist/wren.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { readColourTokens } from "./brood/tokens/src/contrast.js";
|
|
4
|
+
import { BODY, DETAIL_WEIGHT, EYE, WING } from "./brood/tokens/src/wren.js";
|
|
5
|
+
/** A path of M, L and C commands, as the wren's are, as polylines. */
|
|
6
|
+
function polylines(d) {
|
|
7
|
+
const tokens = d.match(/[MLCZ]|-?\d*\.?\d+/g) ?? [];
|
|
8
|
+
const lines = [];
|
|
9
|
+
let line = [];
|
|
10
|
+
let at = [0, 0];
|
|
11
|
+
let command = "";
|
|
12
|
+
const num = () => Number(tokens.shift());
|
|
13
|
+
while (tokens.length) {
|
|
14
|
+
if (/[MLCZ]/.test(tokens[0]))
|
|
15
|
+
command = tokens.shift();
|
|
16
|
+
if (command === "M") {
|
|
17
|
+
if (line.length)
|
|
18
|
+
lines.push(line);
|
|
19
|
+
at = [num(), num()];
|
|
20
|
+
line = [at];
|
|
21
|
+
}
|
|
22
|
+
else if (command === "L") {
|
|
23
|
+
at = [num(), num()];
|
|
24
|
+
line.push(at);
|
|
25
|
+
}
|
|
26
|
+
else if (command === "C") {
|
|
27
|
+
const c1 = [num(), num()];
|
|
28
|
+
const c2 = [num(), num()];
|
|
29
|
+
const end = [num(), num()];
|
|
30
|
+
const from = at;
|
|
31
|
+
for (let i = 1; i <= 16; i++) {
|
|
32
|
+
const t = i / 16;
|
|
33
|
+
const u = 1 - t;
|
|
34
|
+
line.push([
|
|
35
|
+
u ** 3 * from[0] +
|
|
36
|
+
3 * u * u * t * c1[0] +
|
|
37
|
+
3 * u * t * t * c2[0] +
|
|
38
|
+
t ** 3 * end[0],
|
|
39
|
+
u ** 3 * from[1] +
|
|
40
|
+
3 * u * u * t * c1[1] +
|
|
41
|
+
3 * u * t * t * c2[1] +
|
|
42
|
+
t ** 3 * end[1],
|
|
43
|
+
]);
|
|
44
|
+
}
|
|
45
|
+
at = end;
|
|
46
|
+
}
|
|
47
|
+
else if (command === "Z") {
|
|
48
|
+
if (line[0])
|
|
49
|
+
line.push(line[0]);
|
|
50
|
+
command = "";
|
|
51
|
+
}
|
|
52
|
+
else
|
|
53
|
+
throw new Error(`the wren's path has a command this does not draw: ${command}`);
|
|
54
|
+
}
|
|
55
|
+
if (line.length)
|
|
56
|
+
lines.push(line);
|
|
57
|
+
return lines;
|
|
58
|
+
}
|
|
59
|
+
const body = polylines(BODY)[0];
|
|
60
|
+
const wing = polylines(WING)[0];
|
|
61
|
+
/** Even-odd: whether a point is inside the closed outline. */
|
|
62
|
+
function inside(p, outline) {
|
|
63
|
+
let within = false;
|
|
64
|
+
for (let i = 0, j = outline.length - 1; i < outline.length; j = i++) {
|
|
65
|
+
const [xi, yi] = outline[i];
|
|
66
|
+
const [xj, yj] = outline[j];
|
|
67
|
+
if (yi > p[1] !== yj > p[1] &&
|
|
68
|
+
p[0] < ((xj - xi) * (p[1] - yi)) / (yj - yi) + xi)
|
|
69
|
+
within = !within;
|
|
70
|
+
}
|
|
71
|
+
return within;
|
|
72
|
+
}
|
|
73
|
+
/** How far a point is from an open polyline. */
|
|
74
|
+
function distance(p, line) {
|
|
75
|
+
let best = Infinity;
|
|
76
|
+
for (let i = 1; i < line.length; i++) {
|
|
77
|
+
const [ax, ay] = line[i - 1];
|
|
78
|
+
const [bx, by] = line[i];
|
|
79
|
+
const dx = bx - ax;
|
|
80
|
+
const dy = by - ay;
|
|
81
|
+
const t = Math.max(0, Math.min(1, ((p[0] - ax) * dx + (p[1] - ay) * dy) / (dx * dx + dy * dy)));
|
|
82
|
+
best = Math.min(best, Math.hypot(p[0] - (ax + t * dx), p[1] - (ay + t * dy)));
|
|
83
|
+
}
|
|
84
|
+
return best;
|
|
85
|
+
}
|
|
86
|
+
/** Whether the wren is inked at a point: in the body, and not in the eye or the wing. */
|
|
87
|
+
const inked = (p) => inside(p, body) &&
|
|
88
|
+
Math.hypot(p[0] - EYE.cx, p[1] - EYE.cy) > EYE.r &&
|
|
89
|
+
distance(p, wing) > DETAIL_WEIGHT / 2;
|
|
90
|
+
/** From an empty cell to a full one. */
|
|
91
|
+
const RAMP = " .:-=+*#%@";
|
|
92
|
+
/** The wren, `columns` characters wide, as lines, trimmed to the bird. */
|
|
93
|
+
export function wrenArt(columns = 32) {
|
|
94
|
+
const xs = body.map((p) => p[0]);
|
|
95
|
+
const ys = body.map((p) => p[1]);
|
|
96
|
+
const [left, right] = [Math.min(...xs), Math.max(...xs)];
|
|
97
|
+
const [top, bottom] = [Math.min(...ys), Math.max(...ys)];
|
|
98
|
+
const cell = (right - left) / columns;
|
|
99
|
+
const rows = Math.ceil((bottom - top) / (cell * 2));
|
|
100
|
+
const SAMPLES = 4;
|
|
101
|
+
const lines = [];
|
|
102
|
+
for (let r = 0; r < rows; r++) {
|
|
103
|
+
let line = "";
|
|
104
|
+
for (let c = 0; c < columns; c++) {
|
|
105
|
+
let hits = 0;
|
|
106
|
+
for (let sy = 0; sy < SAMPLES * 2; sy++)
|
|
107
|
+
for (let sx = 0; sx < SAMPLES; sx++)
|
|
108
|
+
if (inked([
|
|
109
|
+
left + (c + (sx + 0.5) / SAMPLES) * cell,
|
|
110
|
+
top + (r * 2 + (sy + 0.5) / SAMPLES) * cell,
|
|
111
|
+
]))
|
|
112
|
+
hits++;
|
|
113
|
+
line +=
|
|
114
|
+
RAMP[Math.round((hits / (SAMPLES * SAMPLES * 2)) * (RAMP.length - 1))] ?? " ";
|
|
115
|
+
}
|
|
116
|
+
lines.push(line.trimEnd());
|
|
117
|
+
}
|
|
118
|
+
return lines;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Barn, as the dark scheme sets it, read from the tokens when brood runs.
|
|
122
|
+
* A terminal does not say whether its ground is light or dark, and this one
|
|
123
|
+
* holds 3:1 on white, on black and on Ink, where the light scheme's falls
|
|
124
|
+
* short on a dark ground. It is the screen's one drop of Barn.
|
|
125
|
+
*/
|
|
126
|
+
function barn() {
|
|
127
|
+
const css = readFileSync(fileURLToPath(import.meta.resolve("./brood/tokens/dark.css")), "utf8");
|
|
128
|
+
const value = readColourTokens(css).find((t) => t.name === "barn")?.value;
|
|
129
|
+
const hex = value?.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i);
|
|
130
|
+
if (!hex)
|
|
131
|
+
throw new Error("dark.css sets no --color-barn as six hex digits");
|
|
132
|
+
return [parseInt(hex[1], 16), parseInt(hex[2], 16), parseInt(hex[3], 16)];
|
|
133
|
+
}
|
|
134
|
+
/** The levels of xterm's 256-colour cube, for a terminal with no true colour. */
|
|
135
|
+
const CUBE = [0, 95, 135, 175, 215, 255];
|
|
136
|
+
const nearest = (c) => CUBE.reduce((best, level, i) => Math.abs(level - c) < Math.abs(CUBE[best] - c) ? i : best, 0);
|
|
137
|
+
/** How much colour a terminal shows, as it says in its environment. */
|
|
138
|
+
export const depthOf = (env) => env.COLORTERM === "truecolor" || env.COLORTERM === "24bit"
|
|
139
|
+
? "true"
|
|
140
|
+
: env.TERM?.includes("256color")
|
|
141
|
+
? "256"
|
|
142
|
+
: "none";
|
|
143
|
+
/** The wren's lines, inked in Barn as deep as the terminal shows it. */
|
|
144
|
+
export function wrenIn(depth, columns) {
|
|
145
|
+
const art = wrenArt(columns);
|
|
146
|
+
if (depth === "none")
|
|
147
|
+
return art;
|
|
148
|
+
const [r, g, b] = barn();
|
|
149
|
+
const ink = depth === "true"
|
|
150
|
+
? `\u001b[38;2;${r};${g};${b}m`
|
|
151
|
+
: `\u001b[38;5;${16 + 36 * nearest(r) + 6 * nearest(g) + nearest(b)}m`;
|
|
152
|
+
return art.map((line) => (line ? `${ink}${line}\u001b[39m` : line));
|
|
153
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@noisybrood/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "brood, the command line over Noisy Brood's index of who writes to you. It never reads a message.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"brood": "dist/brood.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=24"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"cli",
|
|
18
|
+
"email",
|
|
19
|
+
"gmail",
|
|
20
|
+
"metadata",
|
|
21
|
+
"privacy"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public",
|
|
25
|
+
"provenance": true
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p . && node -e \"require('node:fs').cpSync('src/brood/tokens/dark.css','dist/brood/tokens/dark.css')\"",
|
|
29
|
+
"test": "node --test src/*.test.ts"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/node": "26.6.5",
|
|
33
|
+
"typescript": "6.0.3"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/Noisy-Brood/brood-cli.git"
|
|
38
|
+
}
|
|
39
|
+
}
|