@pmndre/pine 0.0.0-stage → 0.22.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/LICENSE +21 -0
- package/README.md +226 -2
- package/bin/pine.js +27 -0
- package/docs/SKILL.md +234 -0
- package/docs/references/agents.md +454 -0
- package/docs/references/coding.md +311 -0
- package/docs/references/computer.md +212 -0
- package/docs/references/configuration.md +310 -0
- package/docs/references/events.md +267 -0
- package/docs/references/mcp.md +420 -0
- package/docs/references/operations.md +394 -0
- package/docs/references/overview.md +195 -0
- package/docs/references/security.md +896 -0
- package/docs/references/setup.md +385 -0
- package/docs/references/sources.md +772 -0
- package/docs/references/stack.md +121 -0
- package/docs/references/tui.md +575 -0
- package/examples/home/.env.example +7 -0
- package/examples/home/USER.md +4 -0
- package/examples/home/agents/main/agent.json +4 -0
- package/examples/home/agents/main/workspace/AGENTS.md +14 -0
- package/examples/home/agents/main/workspace/MEMORY.md +4 -0
- package/examples/home/agents/main/workspace/SOUL.md +4 -0
- package/examples/home/models.json +62 -0
- package/examples/home/pine.json +34 -0
- package/examples/home/skills/README.txt +7 -0
- package/examples/launchd/com.pine.daemon.plist +32 -0
- package/examples/systemd/pine.service +22 -0
- package/mcp/browser.d.ts +99 -0
- package/mcp/browser.js +496 -0
- package/mcp/commands.d.ts +20 -0
- package/mcp/commands.js +88 -0
- package/mcp/computer.d.ts +139 -0
- package/mcp/computer.js +698 -0
- package/mcp/computer.swift +989 -0
- package/mcp/lib.d.ts +23 -0
- package/mcp/lib.js +52 -0
- package/mcp/web.d.ts +6 -0
- package/mcp/web.js +140 -0
- package/package.json +32 -4
- package/src/access.d.ts +112 -0
- package/src/access.js +438 -0
- package/src/actions.d.ts +75 -0
- package/src/actions.js +167 -0
- package/src/admin.d.ts +167 -0
- package/src/admin.js +625 -0
- package/src/agent-admin.d.ts +24 -0
- package/src/agent-admin.js +55 -0
- package/src/agents.d.ts +540 -0
- package/src/agents.js +2776 -0
- package/src/background.d.ts +29 -0
- package/src/background.js +100 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +777 -0
- package/src/computer.d.ts +30 -0
- package/src/computer.js +32 -0
- package/src/config.d.ts +396 -0
- package/src/config.js +549 -0
- package/src/dev/claude-model.d.ts +37 -0
- package/src/dev/claude-model.js +204 -0
- package/src/env-file.d.ts +4 -0
- package/src/env-file.js +40 -0
- package/src/goals.d.ts +133 -0
- package/src/goals.js +316 -0
- package/src/http.d.ts +46 -0
- package/src/http.js +509 -0
- package/src/limiter.d.ts +32 -0
- package/src/limiter.js +84 -0
- package/src/login.d.ts +23 -0
- package/src/login.js +130 -0
- package/src/manual.d.ts +61 -0
- package/src/manual.js +443 -0
- package/src/mcp-config.d.ts +49 -0
- package/src/mcp-config.js +182 -0
- package/src/models.d.ts +90 -0
- package/src/models.js +274 -0
- package/src/net.d.ts +50 -0
- package/src/net.js +150 -0
- package/src/pine.d.ts +32 -0
- package/src/pine.js +134 -0
- package/src/programs.d.ts +123 -0
- package/src/programs.js +412 -0
- package/src/router.d.ts +56 -0
- package/src/router.js +164 -0
- package/src/sandbox-fs.d.ts +34 -0
- package/src/sandbox-fs.js +186 -0
- package/src/sandbox-linux.d.ts +96 -0
- package/src/sandbox-linux.js +297 -0
- package/src/sandbox-setup.d.ts +70 -0
- package/src/sandbox-setup.js +216 -0
- package/src/security.d.ts +238 -0
- package/src/security.js +660 -0
- package/src/skills.d.ts +67 -0
- package/src/skills.js +163 -0
- package/src/snapshots.d.ts +68 -0
- package/src/snapshots.js +306 -0
- package/src/source-types.d.ts +61 -0
- package/src/source-types.js +1 -0
- package/src/sources/command.d.ts +4 -0
- package/src/sources/command.js +77 -0
- package/src/sources/folder.d.ts +6 -0
- package/src/sources/folder.js +116 -0
- package/src/sources/ftp.d.ts +4 -0
- package/src/sources/ftp.js +307 -0
- package/src/sources/http.d.ts +4 -0
- package/src/sources/http.js +201 -0
- package/src/sources/mqtt.d.ts +18 -0
- package/src/sources/mqtt.js +97 -0
- package/src/sources/telegram.d.ts +22 -0
- package/src/sources/telegram.js +283 -0
- package/src/sources/webhook.d.ts +4 -0
- package/src/sources/webhook.js +52 -0
- package/src/sources.d.ts +344 -0
- package/src/sources.js +966 -0
- package/src/store.d.ts +99 -0
- package/src/store.js +277 -0
- package/src/timers.d.ts +83 -0
- package/src/timers.js +318 -0
- package/src/tools.d.ts +11 -0
- package/src/tools.js +1297 -0
- package/src/tui/app.d.ts +305 -0
- package/src/tui/app.js +3195 -0
- package/src/tui/client.d.ts +236 -0
- package/src/tui/client.js +131 -0
- package/src/tui/code-pane.d.ts +68 -0
- package/src/tui/code-pane.js +535 -0
- package/src/tui/highlight.d.ts +8 -0
- package/src/tui/highlight.js +86 -0
- package/src/tui/picture.d.ts +206 -0
- package/src/tui/picture.js +801 -0
- package/src/tui/run.d.ts +8 -0
- package/src/tui/run.js +97 -0
- package/src/tui/theme.d.ts +32 -0
- package/src/tui/theme.js +128 -0
- package/src/tui/undo.d.ts +5 -0
- package/src/tui/undo.js +50 -0
- package/src/tui/voice.d.ts +49 -0
- package/src/tui/voice.js +155 -0
- package/src/tui/voice.swift +223 -0
- package/src/types.d.ts +139 -0
- package/src/types.js +1 -0
- package/src/undo.d.ts +224 -0
- package/src/undo.js +465 -0
- package/src/update.d.ts +35 -0
- package/src/update.js +130 -0
- package/src/util.d.ts +65 -0
- package/src/util.js +171 -0
- package/src/watching.d.ts +20 -0
- package/src/watching.js +68 -0
- package/src/workspace-view.d.ts +72 -0
- package/src/workspace-view.js +221 -0
|
@@ -0,0 +1,801 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import zlib from "node:zlib";
|
|
6
|
+
import { allocateImageId, getCapabilities, getCellDimensions, getImageDimensions, Image, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
7
|
+
import { helperArgv } from "../security.js";
|
|
8
|
+
import { c } from "./theme.js";
|
|
9
|
+
const SIPS = "/usr/bin/sips";
|
|
10
|
+
const hasSips = process.platform === "darwin" && fs.existsSync(SIPS);
|
|
11
|
+
const TMP = fs.realpathSync(os.tmpdir());
|
|
12
|
+
/**
|
|
13
|
+
* Start a converter (sips, ffmpeg) on bytes that can come from anywhere: it gets no network and
|
|
14
|
+
* may write only in the temp folder (sips keeps scratch files of its own there).
|
|
15
|
+
*/
|
|
16
|
+
function confined(command, args, stdio, cwd = TMP, group = false) {
|
|
17
|
+
const jail = helperArgv([TMP]);
|
|
18
|
+
// `group`: a process group of its own, so that it can be held and let go as a whole (see Sound).
|
|
19
|
+
const how = { cwd, stdio: ["ignore", stdio, "ignore"], detached: group };
|
|
20
|
+
// Where there is no sandbox at all, the converter still runs: a picture not shown helps nobody.
|
|
21
|
+
if (!jail)
|
|
22
|
+
return spawn(command, args, how);
|
|
23
|
+
return spawn(jail[0], [...jail.slice(1), command, ...args], how);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Write a copy of a picture as png or bmp, at most `limit` pixels on its longer side: with
|
|
27
|
+
* macOS's sips, and with ffmpeg where there is no sips (Linux).
|
|
28
|
+
*/
|
|
29
|
+
export function scaled(dir, source, limit, format, tool = converter()) {
|
|
30
|
+
const target = path.join(dir, `out.${format}`);
|
|
31
|
+
if (tool === "sips")
|
|
32
|
+
return finished(confined(SIPS, ["-s", "format", format, "-Z", String(limit), source, "--out", target], "ignore", dir), 15_000);
|
|
33
|
+
if (tool !== "ffmpeg" || !FFMPEG)
|
|
34
|
+
return Promise.resolve(false);
|
|
35
|
+
// bgr24: the bmp then has no alpha and no colour table, which is all `parseBmp` reads.
|
|
36
|
+
return finished(confined(FFMPEG, ["-loglevel", "error", "-y", "-i", source, "-frames:v", "1", "-vf", `scale=${limit}:${limit}:force_original_aspect_ratio=decrease`, ...(format === "bmp" ? ["-pix_fmt", "bgr24"] : []), target], "ignore", dir), 15_000);
|
|
37
|
+
}
|
|
38
|
+
const converter = () => (hasSips ? "sips" : FFMPEG ? "ffmpeg" : undefined);
|
|
39
|
+
/** Read an uncompressed 24 or 32 bit BMP, the simplest format sips and ffmpeg write. Transparent parts are shown on dark grey. */
|
|
40
|
+
export function parseBmp(b) {
|
|
41
|
+
if (b.length < 54 || b.toString("latin1", 0, 2) !== "BM")
|
|
42
|
+
return undefined;
|
|
43
|
+
const offset = b.readUInt32LE(10);
|
|
44
|
+
const width = b.readInt32LE(18);
|
|
45
|
+
const rawHeight = b.readInt32LE(22);
|
|
46
|
+
const height = Math.abs(rawHeight);
|
|
47
|
+
const bpp = b.readUInt16LE(28);
|
|
48
|
+
const compression = b.readUInt32LE(30);
|
|
49
|
+
if (width <= 0 || height <= 0 || width > 4096 || height > 4096 || (bpp !== 24 && bpp !== 32) || (compression !== 0 && compression !== 3))
|
|
50
|
+
return undefined;
|
|
51
|
+
const bytes = bpp / 8;
|
|
52
|
+
const stride = Math.ceil((width * bytes) / 4) * 4;
|
|
53
|
+
if (b.length < offset + stride * height)
|
|
54
|
+
return undefined;
|
|
55
|
+
// Where each channel sits in a pixel. With "bitfields" the header says; otherwise it is blue, green, red(, alpha).
|
|
56
|
+
const at = (mask, fallback) => (mask === 0xff ? 0 : mask === 0xff00 ? 1 : mask === 0xff0000 ? 2 : mask === 0xff000000 ? 3 : fallback);
|
|
57
|
+
const fields = compression === 3 && b.length >= 70;
|
|
58
|
+
const r = fields ? at(b.readUInt32LE(54), 2) : 2;
|
|
59
|
+
const g = fields ? at(b.readUInt32LE(58), 1) : 1;
|
|
60
|
+
const bl = fields ? at(b.readUInt32LE(62), 0) : 0;
|
|
61
|
+
const a = bpp === 32 ? (fields ? at(b.readUInt32LE(66), 3) : 3) : -1;
|
|
62
|
+
const rgb = new Uint8Array(width * height * 3);
|
|
63
|
+
for (let y = 0; y < height; y++) {
|
|
64
|
+
const row = offset + (rawHeight < 0 ? y : height - 1 - y) * stride;
|
|
65
|
+
for (let x = 0; x < width; x++) {
|
|
66
|
+
const p = row + x * bytes;
|
|
67
|
+
const alpha = a >= 0 ? b[p + a] / 255 : 1;
|
|
68
|
+
const o = (y * width + x) * 3;
|
|
69
|
+
rgb[o] = Math.round(b[p + r] * alpha + 30 * (1 - alpha));
|
|
70
|
+
rgb[o + 1] = Math.round(b[p + g] * alpha + 30 * (1 - alpha));
|
|
71
|
+
rgb[o + 2] = Math.round(b[p + bl] * alpha + 30 * (1 - alpha));
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { width, height, rgb };
|
|
75
|
+
}
|
|
76
|
+
let queue = Promise.resolve();
|
|
77
|
+
/**
|
|
78
|
+
* Make a picture ready to draw. Returns what could be made: without a converter (no sips, no
|
|
79
|
+
* ffmpeg) a png still shows on a Kitty-protocol terminal, and anything else is shown as a line of text.
|
|
80
|
+
*/
|
|
81
|
+
export function prepare(bytes, mime, name) {
|
|
82
|
+
// One at a time: a page full of pictures should not start a dozen converters at once.
|
|
83
|
+
const job = queue.then(() => convert(bytes, mime, name));
|
|
84
|
+
queue = job.catch(() => { });
|
|
85
|
+
return job;
|
|
86
|
+
}
|
|
87
|
+
async function convert(bytes, mime, name) {
|
|
88
|
+
const dims = getImageDimensions(bytes.subarray(0, 512 * 1024).toString("base64"), mime) ?? { widthPx: 0, heightPx: 0 };
|
|
89
|
+
const out = { name, widthPx: dims.widthPx, heightPx: dims.heightPx };
|
|
90
|
+
if (!converter()) {
|
|
91
|
+
if (mime === "image/png")
|
|
92
|
+
out.png = bytes.toString("base64");
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
const dir = fs.mkdtempSync(path.join(TMP, "pine-pic-"));
|
|
96
|
+
try {
|
|
97
|
+
const source = path.join(dir, "in");
|
|
98
|
+
fs.writeFileSync(source, bytes, { mode: 0o600 });
|
|
99
|
+
const kitty = getCapabilities().images === "kitty";
|
|
100
|
+
// A converter scales to the size it is given, also upwards; a small picture stays as small as it is.
|
|
101
|
+
const longest = Math.max(dims.widthPx, dims.heightPx) || 1200;
|
|
102
|
+
const fit = (limit) => Math.min(limit, longest);
|
|
103
|
+
if (kitty && (await scaled(dir, source, fit(1200), "png")))
|
|
104
|
+
out.png = fs.readFileSync(path.join(dir, "out.png")).toString("base64");
|
|
105
|
+
// The small copy is made in both cases: it is cheap, and it is what shows if the real picture cannot.
|
|
106
|
+
if (await scaled(dir, source, fit(240), "bmp"))
|
|
107
|
+
out.pixels = parseBmp(fs.readFileSync(path.join(dir, "out.bmp")));
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// shown as text
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
/** Average colour of a rectangle of the source, for scaling down without speckle. */
|
|
118
|
+
function sample(p, x0, y0, x1, y1) {
|
|
119
|
+
const ax = Math.max(0, Math.min(p.width - 1, Math.floor(x0)));
|
|
120
|
+
const bx = Math.max(ax + 1, Math.min(p.width, Math.ceil(x1)));
|
|
121
|
+
const ay = Math.max(0, Math.min(p.height - 1, Math.floor(y0)));
|
|
122
|
+
const by = Math.max(ay + 1, Math.min(p.height, Math.ceil(y1)));
|
|
123
|
+
let r = 0;
|
|
124
|
+
let g = 0;
|
|
125
|
+
let b = 0;
|
|
126
|
+
for (let y = ay; y < by; y++) {
|
|
127
|
+
for (let x = ax; x < bx; x++) {
|
|
128
|
+
const o = (y * p.width + x) * 3;
|
|
129
|
+
r += p.rgb[o];
|
|
130
|
+
g += p.rgb[o + 1];
|
|
131
|
+
b += p.rgb[o + 2];
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const n = (bx - ax) * (by - ay);
|
|
135
|
+
return [Math.round(r / n), Math.round(g / n), Math.round(b / n)];
|
|
136
|
+
}
|
|
137
|
+
const cube = (v) => (v < 48 ? 0 : v < 115 ? 1 : Math.min(5, Math.floor((v - 35) / 40)));
|
|
138
|
+
/** Draw a picture with half blocks, at most `maxCols` cells wide and `maxRows` lines high. */
|
|
139
|
+
export function halfBlocks(p, maxCols, maxRows, trueColor = true) {
|
|
140
|
+
// A character cell is about twice as high as wide, so half a cell is about a square pixel.
|
|
141
|
+
let cols = Math.max(1, Math.min(maxCols, p.width));
|
|
142
|
+
let rows = Math.max(1, Math.ceil((cols * p.height) / p.width / 2));
|
|
143
|
+
if (rows > maxRows) {
|
|
144
|
+
rows = Math.max(1, maxRows);
|
|
145
|
+
cols = Math.max(1, Math.min(maxCols, Math.round((rows * 2 * p.width) / p.height)));
|
|
146
|
+
}
|
|
147
|
+
const sx = p.width / cols;
|
|
148
|
+
const sy = p.height / (rows * 2);
|
|
149
|
+
const paint = ([r, g, b], back) => (trueColor ? `\x1b[${back ? 48 : 38};2;${r};${g};${b}m` : `\x1b[${back ? 48 : 38};5;${16 + 36 * cube(r) + 6 * cube(g) + cube(b)}m`);
|
|
150
|
+
const lines = [];
|
|
151
|
+
for (let y = 0; y < rows; y++) {
|
|
152
|
+
let line = " ";
|
|
153
|
+
for (let x = 0; x < cols; x++)
|
|
154
|
+
line += `${paint(sample(p, x * sx, y * 2 * sy, (x + 1) * sx, (y * 2 + 1) * sy), false)}${paint(sample(p, x * sx, (y * 2 + 1) * sy, (x + 1) * sx, (y * 2 + 2) * sy), true)}▀`;
|
|
155
|
+
lines.push(`${line}\x1b[0m`);
|
|
156
|
+
}
|
|
157
|
+
return lines;
|
|
158
|
+
}
|
|
159
|
+
const drawn = new Map();
|
|
160
|
+
function drawsAsText(id, owner) {
|
|
161
|
+
if (id === undefined || drawn.get(id)?.deref() === owner)
|
|
162
|
+
return;
|
|
163
|
+
if (drawn.size > 400)
|
|
164
|
+
for (const [key, ref] of drawn)
|
|
165
|
+
if (!ref.deref())
|
|
166
|
+
drawn.delete(key);
|
|
167
|
+
drawn.set(id, new WeakRef(owner));
|
|
168
|
+
}
|
|
169
|
+
/** Half blocks in the room of a real picture: at most `cols` cells wide, exactly `rows` lines. */
|
|
170
|
+
function inPlaceOf(pixels, cols, rows) {
|
|
171
|
+
const lines = pixels && rows > 0 ? halfBlocks(pixels, Math.max(1, cols - 1), rows, getCapabilities().trueColor) : [];
|
|
172
|
+
while (lines.length < rows)
|
|
173
|
+
lines.push("");
|
|
174
|
+
return lines;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The lines of a screen, with every real picture that `covered` says something lies over drawn as
|
|
178
|
+
* text instead. `covered` gets the picture's place: its first line and column, its width and height.
|
|
179
|
+
* Returns the same array when no picture is covered.
|
|
180
|
+
*/
|
|
181
|
+
export function picturesAsText(lines, covered) {
|
|
182
|
+
let out = lines;
|
|
183
|
+
for (let row = 0; row < lines.length; row++) {
|
|
184
|
+
if (!lines[row].includes("\x1b_G"))
|
|
185
|
+
continue;
|
|
186
|
+
const controls = /\x1b_G([^;]*);/.exec(lines[row])?.[1];
|
|
187
|
+
if (controls === undefined)
|
|
188
|
+
continue;
|
|
189
|
+
const value = (key) => {
|
|
190
|
+
const m = new RegExp(`(?:^|,)${key}=(\\d+)`).exec(controls);
|
|
191
|
+
return m ? Number(m[1]) : undefined;
|
|
192
|
+
};
|
|
193
|
+
const cols = value("c") ?? 1;
|
|
194
|
+
const rows = value("r") ?? 1;
|
|
195
|
+
if (!covered(row, 0, cols, rows))
|
|
196
|
+
continue;
|
|
197
|
+
const whole = drawn.get(value("i") ?? 0)?.deref()?.blocks(value("i") ?? 0, cols) ?? [];
|
|
198
|
+
// A picture that the top of the view cuts off shows its lower lines, any other its upper ones.
|
|
199
|
+
const text = (value("y") ?? 0) > 0 ? whole.slice(-rows) : whole.slice(0, rows);
|
|
200
|
+
if (out === lines)
|
|
201
|
+
out = [...lines];
|
|
202
|
+
for (let i = 0; i < rows && row + i < out.length; i++)
|
|
203
|
+
out[row + i] = text[i] ?? "";
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
/** One picture in the conversation: a thumbnail that a click enlarges and a second click shrinks again. */
|
|
208
|
+
export class Picture {
|
|
209
|
+
p;
|
|
210
|
+
large = false;
|
|
211
|
+
images = new Map();
|
|
212
|
+
cache;
|
|
213
|
+
constructor(prepared) {
|
|
214
|
+
this.p = prepared;
|
|
215
|
+
}
|
|
216
|
+
invalidate() {
|
|
217
|
+
this.cache = undefined;
|
|
218
|
+
for (const image of this.images.values())
|
|
219
|
+
image.invalidate();
|
|
220
|
+
}
|
|
221
|
+
handleMouse(e) {
|
|
222
|
+
if (e.button !== "left" || (e.type !== "press" && e.type !== "click" && e.type !== "release"))
|
|
223
|
+
return undefined;
|
|
224
|
+
if (e.type === "click") {
|
|
225
|
+
this.large = !this.large;
|
|
226
|
+
this.invalidate();
|
|
227
|
+
}
|
|
228
|
+
return { handled: true, render: true };
|
|
229
|
+
}
|
|
230
|
+
/** The drawing itself, without a caption: the real picture where the terminal can show one, half blocks otherwise, nothing if neither works. */
|
|
231
|
+
body(width, large = this.large) {
|
|
232
|
+
const caps = getCapabilities();
|
|
233
|
+
const maxCols = Math.max(4, Math.min(width - 2, large ? 140 : 44));
|
|
234
|
+
const maxRows = large ? 48 : 14;
|
|
235
|
+
if (caps.images === "kitty" && this.p.png) {
|
|
236
|
+
let image = this.images.get(large);
|
|
237
|
+
if (!image) {
|
|
238
|
+
image = new Image(this.p.png, "image/png", { fallbackColor: c.gray }, { maxWidthCells: maxCols, maxHeightCells: maxRows, filename: this.p.name });
|
|
239
|
+
this.images.set(large, image);
|
|
240
|
+
}
|
|
241
|
+
const lines = image.render(width);
|
|
242
|
+
drawsAsText(image.getImageId(), this);
|
|
243
|
+
return lines;
|
|
244
|
+
}
|
|
245
|
+
return this.p.pixels ? halfBlocks(this.p.pixels, maxCols, maxRows, caps.trueColor) : [];
|
|
246
|
+
}
|
|
247
|
+
blocks(id, cols) {
|
|
248
|
+
const image = [...this.images.values()].find((i) => i.getImageId() === id);
|
|
249
|
+
return inPlaceOf(this.p.pixels, cols, image?.cachedLines?.length ?? 0);
|
|
250
|
+
}
|
|
251
|
+
render(width) {
|
|
252
|
+
const caps = getCapabilities();
|
|
253
|
+
const key = `${width}:${this.large}:${caps.images}`;
|
|
254
|
+
if (this.cache?.key === key)
|
|
255
|
+
return this.cache.lines;
|
|
256
|
+
const size = this.p.widthPx ? ` · ${this.p.widthPx}×${this.p.heightPx}` : "";
|
|
257
|
+
const body = this.body(width);
|
|
258
|
+
const caption = body.length ? `${this.p.name}${size} · click to ${this.large ? "shrink" : "enlarge"}` : `[picture: ${this.p.name}${size}; it cannot be drawn in this terminal]`;
|
|
259
|
+
const lines = [...body, truncateToWidth(c.gray(` ${caption}`), width)];
|
|
260
|
+
this.cache = { key, lines };
|
|
261
|
+
return lines;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
/** A place in the conversation that is filled in later, so a picture lands where it belongs however long it takes to load. */
|
|
265
|
+
export class Slot {
|
|
266
|
+
inner;
|
|
267
|
+
invalidate() {
|
|
268
|
+
this.inner?.invalidate();
|
|
269
|
+
}
|
|
270
|
+
handleMouse(e) {
|
|
271
|
+
return this.inner?.handleMouse?.(e);
|
|
272
|
+
}
|
|
273
|
+
render(width) {
|
|
274
|
+
return this.inner ? this.inner.render(width) : [];
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// ---- video ---------------------------------------------------------------------------------------
|
|
278
|
+
// A video shows its first moment as a picture. A click plays it in place, drawn with half blocks
|
|
279
|
+
// (which every terminal can do, frame after frame) and without sound; "open in player" hands the
|
|
280
|
+
// file to the system's player for the real thing. Decoding is ffmpeg's job; without ffmpeg there is
|
|
281
|
+
// a still from Quick Look and the player.
|
|
282
|
+
const findTool = (name) => [...(process.env.PATH ?? "").split(":"), "/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"].map((d) => path.join(d, name)).find((f) => d_exists(f));
|
|
283
|
+
function d_exists(f) {
|
|
284
|
+
try {
|
|
285
|
+
return fs.statSync(f).isFile();
|
|
286
|
+
}
|
|
287
|
+
catch {
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const FFMPEG = findTool("ffmpeg");
|
|
292
|
+
const FFPROBE = findTool("ffprobe");
|
|
293
|
+
/** Whether videos can be played in the conversation (ffmpeg is installed). */
|
|
294
|
+
export const canPlayVideo = Boolean(FFMPEG);
|
|
295
|
+
const finished = (child, ms) => new Promise((resolve) => {
|
|
296
|
+
const timer = setTimeout(() => child.kill("SIGKILL"), ms);
|
|
297
|
+
child.on("error", () => (clearTimeout(timer), resolve(false)));
|
|
298
|
+
child.on("close", (code) => (clearTimeout(timer), resolve(code === 0)));
|
|
299
|
+
});
|
|
300
|
+
/** Get a video file ready to show: a still from its start, and how long it runs. */
|
|
301
|
+
export async function prepareVideo(file, name) {
|
|
302
|
+
const dir = fs.mkdtempSync(path.join(TMP, "pine-pic-"));
|
|
303
|
+
let poster = { name, widthPx: 0, heightPx: 0 };
|
|
304
|
+
let seconds;
|
|
305
|
+
try {
|
|
306
|
+
const still = path.join(dir, "poster.png");
|
|
307
|
+
if (FFMPEG)
|
|
308
|
+
await finished(confined(FFMPEG, ["-loglevel", "error", "-y", "-i", file, "-frames:v", "1", "-vf", "scale='min(1200,iw)':-2", still], "ignore"), 20_000);
|
|
309
|
+
if (!d_exists(still) && process.platform === "darwin") {
|
|
310
|
+
// Quick Look writes "<file name>.png" into the folder it is given.
|
|
311
|
+
await finished(confined("/usr/bin/qlmanage", ["-t", "-s", "1200", "-o", dir, file], "ignore"), 20_000);
|
|
312
|
+
const made = fs.readdirSync(dir).find((f) => f.endsWith(".png"));
|
|
313
|
+
if (made)
|
|
314
|
+
fs.renameSync(path.join(dir, made), still);
|
|
315
|
+
}
|
|
316
|
+
if (d_exists(still))
|
|
317
|
+
poster = await prepare(fs.readFileSync(still), "image/png", name);
|
|
318
|
+
if (FFPROBE) {
|
|
319
|
+
const probe = confined(FFPROBE, ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", file], "pipe");
|
|
320
|
+
let out = "";
|
|
321
|
+
probe.stdout?.on("data", (d) => (out += d));
|
|
322
|
+
if (await finished(probe, 10_000))
|
|
323
|
+
seconds = Number.parseFloat(out) || undefined;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
// shown without a still
|
|
328
|
+
}
|
|
329
|
+
finally {
|
|
330
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
331
|
+
}
|
|
332
|
+
return { file, name, seconds, poster };
|
|
333
|
+
}
|
|
334
|
+
/** A png from raw pixels (3 bytes each, rows from the top): what the Kitty graphics protocol takes. Fast, lightly compressed. */
|
|
335
|
+
export function pngFromRgb(rgb, width, height) {
|
|
336
|
+
const stride = width * 3;
|
|
337
|
+
const raw = Buffer.alloc((stride + 1) * height);
|
|
338
|
+
for (let y = 0; y < height; y++)
|
|
339
|
+
raw.set(rgb.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
|
|
340
|
+
const chunk = (type, data) => {
|
|
341
|
+
const body = Buffer.concat([Buffer.from(type, "latin1"), data]);
|
|
342
|
+
const out = Buffer.alloc(body.length + 8);
|
|
343
|
+
out.writeUInt32BE(data.length, 0);
|
|
344
|
+
body.copy(out, 4);
|
|
345
|
+
out.writeUInt32BE(zlib.crc32(body) >>> 0, body.length + 4);
|
|
346
|
+
return out;
|
|
347
|
+
};
|
|
348
|
+
const head = Buffer.alloc(13);
|
|
349
|
+
head.writeUInt32BE(width, 0);
|
|
350
|
+
head.writeUInt32BE(height, 4);
|
|
351
|
+
head.set([8, 2, 0, 0, 0], 8);
|
|
352
|
+
return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", head), chunk("IDAT", zlib.deflateSync(raw, { level: 1 })), chunk("IEND", Buffer.alloc(0))]);
|
|
353
|
+
}
|
|
354
|
+
const clock = (seconds) => `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`;
|
|
355
|
+
const FPS = 10;
|
|
356
|
+
let playing;
|
|
357
|
+
/**
|
|
358
|
+
* One video in the conversation. It plays at the size it is shown in. The picture itself is the
|
|
359
|
+
* size switch (click to enlarge, click to shrink), the first word of the caption is play and pause.
|
|
360
|
+
*
|
|
361
|
+
* Frames are drawn the way pictures are: as real images where the terminal has the Kitty graphics
|
|
362
|
+
* protocol (each frame replaces the last under one image id), as half blocks everywhere else.
|
|
363
|
+
*/
|
|
364
|
+
export class Video {
|
|
365
|
+
v;
|
|
366
|
+
still;
|
|
367
|
+
redraw;
|
|
368
|
+
open;
|
|
369
|
+
child;
|
|
370
|
+
ticker;
|
|
371
|
+
frame;
|
|
372
|
+
/** The current frame as a real image, on terminals that can show one. */
|
|
373
|
+
shown;
|
|
374
|
+
imageId = allocateImageId();
|
|
375
|
+
paused = false;
|
|
376
|
+
large = false;
|
|
377
|
+
/** Seconds of the video shown so far. */
|
|
378
|
+
position = 0;
|
|
379
|
+
width = 80;
|
|
380
|
+
captionRow = -1;
|
|
381
|
+
/** The lines the frame takes on screen. */
|
|
382
|
+
shownRows = 0;
|
|
383
|
+
playTo = -1;
|
|
384
|
+
openFrom = -1;
|
|
385
|
+
constructor(video, redraw, open) {
|
|
386
|
+
this.v = video;
|
|
387
|
+
this.still = new Picture(video.poster);
|
|
388
|
+
this.redraw = redraw;
|
|
389
|
+
this.open = open;
|
|
390
|
+
}
|
|
391
|
+
invalidate() {
|
|
392
|
+
this.still.invalidate();
|
|
393
|
+
}
|
|
394
|
+
get state() {
|
|
395
|
+
return !this.child ? "idle" : this.paused ? "paused" : "playing";
|
|
396
|
+
}
|
|
397
|
+
/** The cells a frame fills: the same room the still takes at the current size, in the video's own shape. */
|
|
398
|
+
size() {
|
|
399
|
+
const maxCols = Math.max(8, Math.min(this.width - 2, this.large ? 140 : 44));
|
|
400
|
+
const maxRows = this.large ? 48 : 14;
|
|
401
|
+
const shape = this.v.poster.widthPx && this.v.poster.heightPx ? this.v.poster.heightPx / this.v.poster.widthPx : 9 / 16;
|
|
402
|
+
const rows = Math.max(1, Math.ceil((maxCols * shape) / 2));
|
|
403
|
+
return rows <= maxRows ? { cols: maxCols, rows } : { cols: Math.max(8, Math.min(maxCols, Math.round((maxRows * 2) / shape))), rows: maxRows };
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* The pixels to decode a frame at. For half blocks: one pixel per half cell. For real images: as
|
|
407
|
+
* many pixels as those cells have on screen, up to a width that a terminal takes ten times a second.
|
|
408
|
+
*/
|
|
409
|
+
pixels(real) {
|
|
410
|
+
const { cols, rows } = this.size();
|
|
411
|
+
if (!real)
|
|
412
|
+
return { cols, rows, width: cols, height: rows * 2 };
|
|
413
|
+
const cell = getCellDimensions();
|
|
414
|
+
const shape = this.v.poster.widthPx && this.v.poster.heightPx ? this.v.poster.heightPx / this.v.poster.widthPx : 9 / 16;
|
|
415
|
+
const even = (n) => Math.max(2, Math.round(n / 2) * 2);
|
|
416
|
+
const width = even(Math.min(cols * cell.widthPx, this.large ? 720 : 480, this.v.poster.widthPx || 720));
|
|
417
|
+
return { cols, rows, width, height: even(width * shape) };
|
|
418
|
+
}
|
|
419
|
+
/** Play, or go on after a pause. At the end it starts over. */
|
|
420
|
+
play() {
|
|
421
|
+
if (!FFMPEG)
|
|
422
|
+
return;
|
|
423
|
+
if (this.child) {
|
|
424
|
+
this.paused = false;
|
|
425
|
+
return this.redraw();
|
|
426
|
+
}
|
|
427
|
+
if (playing && playing !== this)
|
|
428
|
+
playing.stop();
|
|
429
|
+
playing = this;
|
|
430
|
+
this.start(0, false);
|
|
431
|
+
}
|
|
432
|
+
pause() {
|
|
433
|
+
if (!this.child || this.paused)
|
|
434
|
+
return;
|
|
435
|
+
this.paused = true;
|
|
436
|
+
this.redraw();
|
|
437
|
+
}
|
|
438
|
+
/** Decode from `from` seconds on, at the current size. `hold`: show one frame and stay paused there. */
|
|
439
|
+
start(from, hold) {
|
|
440
|
+
this.child?.kill("SIGKILL");
|
|
441
|
+
clearInterval(this.ticker);
|
|
442
|
+
const real = getCapabilities().images === "kitty";
|
|
443
|
+
const { cols, rows, width, height } = this.pixels(real);
|
|
444
|
+
const size = width * height * 3;
|
|
445
|
+
// ffmpeg decodes as fast as it can; the pace is kept here. A few frames are held ready, and
|
|
446
|
+
// ffmpeg waits (its output pipe fills up) while there are enough, which is also what a pause is.
|
|
447
|
+
const child = confined(FFMPEG, ["-loglevel", "error", ...(from > 0 ? ["-ss", from.toFixed(2)] : []), "-i", this.v.file, "-an", "-vf", `fps=${FPS},scale=${width}:${height}`, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"], "pipe");
|
|
448
|
+
this.child = child;
|
|
449
|
+
this.position = from;
|
|
450
|
+
this.paused = hold;
|
|
451
|
+
let wanted = hold ? 1 : 0;
|
|
452
|
+
const ready = [];
|
|
453
|
+
let pending = Buffer.alloc(0);
|
|
454
|
+
let ended = false;
|
|
455
|
+
child.stdout?.on("data", (d) => {
|
|
456
|
+
pending = pending.length ? Buffer.concat([pending, d]) : d;
|
|
457
|
+
while (pending.length >= size) {
|
|
458
|
+
ready.push(new Uint8Array(pending.subarray(0, size)));
|
|
459
|
+
pending = pending.subarray(size);
|
|
460
|
+
}
|
|
461
|
+
if (ready.length > 8)
|
|
462
|
+
child.stdout?.pause();
|
|
463
|
+
});
|
|
464
|
+
child.on("error", () => (ended = true));
|
|
465
|
+
child.on("close", () => (ended = true));
|
|
466
|
+
const ticker = setInterval(() => {
|
|
467
|
+
if (this.child !== child)
|
|
468
|
+
return clearInterval(ticker);
|
|
469
|
+
if (this.paused && !wanted)
|
|
470
|
+
return;
|
|
471
|
+
const rgb = ready.shift();
|
|
472
|
+
if (rgb) {
|
|
473
|
+
this.frame = { width, height, rgb };
|
|
474
|
+
// The same image id every time: the terminal replaces the picture in place.
|
|
475
|
+
this.shown = real ? new Image(pngFromRgb(rgb, width, height).toString("base64"), "image/png", { fallbackColor: c.gray }, { maxWidthCells: cols, maxHeightCells: rows, imageId: this.imageId }, { widthPx: width, heightPx: height }) : undefined;
|
|
476
|
+
if (wanted)
|
|
477
|
+
wanted--;
|
|
478
|
+
else
|
|
479
|
+
this.position += 1 / FPS;
|
|
480
|
+
this.redraw();
|
|
481
|
+
}
|
|
482
|
+
else if (ended)
|
|
483
|
+
this.stop();
|
|
484
|
+
if (ready.length < 4)
|
|
485
|
+
child.stdout?.resume();
|
|
486
|
+
}, 1000 / FPS);
|
|
487
|
+
ticker.unref?.();
|
|
488
|
+
this.ticker = ticker;
|
|
489
|
+
this.redraw();
|
|
490
|
+
}
|
|
491
|
+
/** Back to the still. */
|
|
492
|
+
stop() {
|
|
493
|
+
const child = this.child;
|
|
494
|
+
clearInterval(this.ticker);
|
|
495
|
+
this.child = undefined;
|
|
496
|
+
this.frame = undefined;
|
|
497
|
+
this.shown = undefined;
|
|
498
|
+
this.paused = false;
|
|
499
|
+
this.position = 0;
|
|
500
|
+
if (playing === this)
|
|
501
|
+
playing = undefined;
|
|
502
|
+
child?.kill("SIGKILL");
|
|
503
|
+
this.redraw();
|
|
504
|
+
}
|
|
505
|
+
resize() {
|
|
506
|
+
this.large = !this.large;
|
|
507
|
+
this.still.invalidate();
|
|
508
|
+
// A video that is running or paused carries on where it is, at the new size.
|
|
509
|
+
if (this.child)
|
|
510
|
+
this.start(this.position, this.paused);
|
|
511
|
+
else
|
|
512
|
+
this.redraw();
|
|
513
|
+
}
|
|
514
|
+
handleMouse(e) {
|
|
515
|
+
if (e.button !== "left" || (e.type !== "press" && e.type !== "click" && e.type !== "release"))
|
|
516
|
+
return undefined;
|
|
517
|
+
if (e.type === "click") {
|
|
518
|
+
if (e.y !== this.captionRow)
|
|
519
|
+
this.resize();
|
|
520
|
+
else if (this.openFrom >= 0 && e.x >= this.openFrom)
|
|
521
|
+
this.open(this.v.file);
|
|
522
|
+
else if (e.x <= this.playTo)
|
|
523
|
+
this.state === "playing" ? this.pause() : this.play();
|
|
524
|
+
}
|
|
525
|
+
return { handled: true, render: true };
|
|
526
|
+
}
|
|
527
|
+
blocks(_id, cols) {
|
|
528
|
+
return inPlaceOf(this.frame, cols, this.shownRows);
|
|
529
|
+
}
|
|
530
|
+
render(width) {
|
|
531
|
+
this.width = width;
|
|
532
|
+
if (this.shown)
|
|
533
|
+
drawsAsText(this.imageId, this);
|
|
534
|
+
const body = this.shown ? this.shown.render(width) : this.frame ? halfBlocks(this.frame, this.frame.width, this.frame.height / 2, getCapabilities().trueColor) : this.still.body(width, this.large);
|
|
535
|
+
const control = !FFMPEG ? "" : this.state === "playing" ? "‖ pause" : "▶ play";
|
|
536
|
+
const total = this.v.seconds ? clock(this.v.seconds) : "";
|
|
537
|
+
const time = this.child ? `${clock(this.position)}${total ? ` / ${total}` : ""} · no sound` : total;
|
|
538
|
+
const left = ` ${control ? `${c.accent(control)} ` : ""}${c.gray(`${control ? "· " : ""}${this.v.name}${time ? ` · ${time}` : ""} · click the picture to ${this.large ? "shrink" : "enlarge"}`)}`;
|
|
539
|
+
this.captionRow = body.length;
|
|
540
|
+
this.shownRows = body.length;
|
|
541
|
+
this.playTo = control ? 1 + visibleWidth(control) : -1;
|
|
542
|
+
this.openFrom = visibleWidth(left) + 3;
|
|
543
|
+
return [...body, truncateToWidth(`${left} ${c.accent("▸ open in player")}`, width)];
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/** Stop whatever video is playing, for example when the UI closes. */
|
|
547
|
+
export function stopVideos() {
|
|
548
|
+
playing?.stop();
|
|
549
|
+
}
|
|
550
|
+
/** macOS: the system's own audio player, driven by a script of the system's own scripting. Nothing to install. */
|
|
551
|
+
const MAC_PLAYER = `ObjC.import("AVFoundation"); ObjC.import("stdlib");
|
|
552
|
+
function run(argv) {
|
|
553
|
+
const player = $.AVAudioPlayer.alloc.initWithContentsOfURLError($.NSURL.fileURLWithPath(argv[1]), null);
|
|
554
|
+
if (!player || player.isNil()) $.exit(1);
|
|
555
|
+
player.currentTime = Number(argv[0]) || 0;
|
|
556
|
+
if (!player.play) $.exit(1);
|
|
557
|
+
$.NSFileHandle.fileHandleWithStandardOutput.writeData($("playing\\n").dataUsingEncoding($.NSUTF8StringEncoding));
|
|
558
|
+
while (player.playing) $.NSRunLoop.currentRunLoop.runUntilDate($.NSDate.dateWithTimeIntervalSinceNow(0.05));
|
|
559
|
+
}`;
|
|
560
|
+
/** What plays sound on this computer. Undefined: nothing here can. */
|
|
561
|
+
export function soundPlayer() {
|
|
562
|
+
const seconds = (s) => s.toFixed(2);
|
|
563
|
+
if (process.platform === "darwin") {
|
|
564
|
+
if (d_exists("/usr/bin/osascript"))
|
|
565
|
+
return { command: ["/usr/bin/osascript", "-l", "JavaScript", "-e", MAC_PLAYER], from: (s) => [seconds(s)] };
|
|
566
|
+
if (d_exists("/usr/bin/afplay"))
|
|
567
|
+
return { command: ["/usr/bin/afplay"] };
|
|
568
|
+
}
|
|
569
|
+
const ffplay = findTool("ffplay");
|
|
570
|
+
if (ffplay)
|
|
571
|
+
return { command: [ffplay, "-nodisp", "-autoexit", "-loglevel", "quiet"], from: (s) => ["-ss", seconds(s)] };
|
|
572
|
+
const mpv = findTool("mpv");
|
|
573
|
+
if (mpv)
|
|
574
|
+
return { command: [mpv, "--no-video", "--really-quiet"], from: (s) => [`--start=${seconds(s)}`] };
|
|
575
|
+
for (const [name, args] of [["pw-play", []], ["paplay", []], ["aplay", ["-q"]]]) {
|
|
576
|
+
const found = findTool(name);
|
|
577
|
+
if (found)
|
|
578
|
+
return { command: [found, ...args] };
|
|
579
|
+
}
|
|
580
|
+
return undefined;
|
|
581
|
+
}
|
|
582
|
+
/** How long a wav file runs, from its header: the size of its data and the bytes it takes per second. */
|
|
583
|
+
function wavSeconds(file) {
|
|
584
|
+
const fd = fs.openSync(file, "r");
|
|
585
|
+
try {
|
|
586
|
+
const head = Buffer.alloc(4096);
|
|
587
|
+
const n = fs.readSync(fd, head, 0, head.length, 0);
|
|
588
|
+
if (n < 44 || head.toString("latin1", 0, 4) !== "RIFF" || head.toString("latin1", 8, 12) !== "WAVE")
|
|
589
|
+
return undefined;
|
|
590
|
+
let rate = 0;
|
|
591
|
+
for (let at = 12; at + 8 <= n;) {
|
|
592
|
+
const id = head.toString("latin1", at, at + 4);
|
|
593
|
+
const size = head.readUInt32LE(at + 4);
|
|
594
|
+
if (id === "fmt " && at + 20 <= n)
|
|
595
|
+
rate = head.readUInt32LE(at + 16);
|
|
596
|
+
// A file that was written as a stream says 0 or the largest number here: then the file's size tells.
|
|
597
|
+
if (id === "data")
|
|
598
|
+
return rate ? Math.min(size || Infinity, fs.fstatSync(fd).size - at - 8) / rate : undefined;
|
|
599
|
+
at += 8 + size + (size % 2);
|
|
600
|
+
}
|
|
601
|
+
return undefined;
|
|
602
|
+
}
|
|
603
|
+
finally {
|
|
604
|
+
fs.closeSync(fd);
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/** Get a sound file ready: how long it runs, where that can be told. */
|
|
608
|
+
export async function prepareSound(file, name) {
|
|
609
|
+
let seconds;
|
|
610
|
+
try {
|
|
611
|
+
seconds = wavSeconds(file);
|
|
612
|
+
const probe = seconds !== undefined ? undefined : FFPROBE ? confined(FFPROBE, ["-v", "error", "-show_entries", "format=duration", "-of", "csv=p=0", file], "pipe") : process.platform === "darwin" && d_exists("/usr/bin/afinfo") ? confined("/usr/bin/afinfo", [file], "pipe") : undefined;
|
|
613
|
+
if (probe) {
|
|
614
|
+
let out = "";
|
|
615
|
+
probe.stdout?.on("data", (d) => (out += d));
|
|
616
|
+
if (await finished(probe, 10_000))
|
|
617
|
+
seconds = Number.parseFloat(/estimated duration: ([\d.]+)/.exec(out)?.[1] ?? out) || undefined;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
catch {
|
|
621
|
+
// played without a length
|
|
622
|
+
}
|
|
623
|
+
return { file, name, seconds: seconds && Number.isFinite(seconds) ? seconds : undefined };
|
|
624
|
+
}
|
|
625
|
+
let sounding;
|
|
626
|
+
/**
|
|
627
|
+
* One sound in the conversation: a line with play and pause, like a video's. A pause ends the
|
|
628
|
+
* player and play starts one from where it was, as a video does with ffmpeg; a player that
|
|
629
|
+
* cannot start in the middle is held instead (see SoundPlayer). At the end the line is back at
|
|
630
|
+
* the start.
|
|
631
|
+
*/
|
|
632
|
+
export class Sound {
|
|
633
|
+
s;
|
|
634
|
+
player;
|
|
635
|
+
redraw;
|
|
636
|
+
open;
|
|
637
|
+
child;
|
|
638
|
+
ticker;
|
|
639
|
+
paused = false;
|
|
640
|
+
/** Milliseconds played before the last pause, and when it went on from there. */
|
|
641
|
+
before = 0;
|
|
642
|
+
since = 0;
|
|
643
|
+
failed = false;
|
|
644
|
+
playTo = -1;
|
|
645
|
+
openFrom = -1;
|
|
646
|
+
constructor(sound, player, redraw, open) {
|
|
647
|
+
this.s = sound;
|
|
648
|
+
this.player = player;
|
|
649
|
+
this.redraw = redraw;
|
|
650
|
+
this.open = open;
|
|
651
|
+
}
|
|
652
|
+
invalidate() { }
|
|
653
|
+
get state() {
|
|
654
|
+
return this.paused ? "paused" : this.child ? "playing" : "idle";
|
|
655
|
+
}
|
|
656
|
+
played() {
|
|
657
|
+
return this.before + (this.child && !this.paused ? Date.now() - this.since : 0);
|
|
658
|
+
}
|
|
659
|
+
/** The player and whatever it started, as one: the sandbox in front of it may be a process of its own. */
|
|
660
|
+
signal(child, signal) {
|
|
661
|
+
try {
|
|
662
|
+
if (child.pid)
|
|
663
|
+
process.kill(-child.pid, signal);
|
|
664
|
+
}
|
|
665
|
+
catch {
|
|
666
|
+
child.kill(signal);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/** Play, or go on after a pause. One sound at a time: another one that is playing stops. */
|
|
670
|
+
play() {
|
|
671
|
+
if (!this.player || this.state === "playing")
|
|
672
|
+
return;
|
|
673
|
+
if (sounding && sounding !== this)
|
|
674
|
+
sounding.stop();
|
|
675
|
+
sounding = this;
|
|
676
|
+
if (this.child) {
|
|
677
|
+
// It was held: let it go on.
|
|
678
|
+
this.signal(this.child, "SIGCONT");
|
|
679
|
+
this.paused = false;
|
|
680
|
+
this.since = Date.now();
|
|
681
|
+
return this.redraw();
|
|
682
|
+
}
|
|
683
|
+
if (!this.paused)
|
|
684
|
+
this.before = 0;
|
|
685
|
+
const child = confined(this.player.command[0], [...this.player.command.slice(1), ...(this.player.from?.(this.before / 1000) ?? []), this.s.file], "pipe", TMP, true);
|
|
686
|
+
this.child = child;
|
|
687
|
+
this.since = Date.now();
|
|
688
|
+
// A player that says when the sound really starts (the macOS one does) sets the clock by it:
|
|
689
|
+
// the position a pause remembers is then the one that was heard.
|
|
690
|
+
child.stdout?.once("data", () => {
|
|
691
|
+
if (this.child === child && !this.paused)
|
|
692
|
+
this.since = Date.now();
|
|
693
|
+
});
|
|
694
|
+
child.stdout?.resume();
|
|
695
|
+
this.paused = false;
|
|
696
|
+
this.failed = false;
|
|
697
|
+
const started = this.before;
|
|
698
|
+
const over = (ok) => {
|
|
699
|
+
if (this.child !== child)
|
|
700
|
+
return;
|
|
701
|
+
// A player that gives up at once could not play this file.
|
|
702
|
+
this.failed = !ok && this.played() - started < 1000;
|
|
703
|
+
this.stop();
|
|
704
|
+
};
|
|
705
|
+
child.on("error", () => over(false));
|
|
706
|
+
child.on("close", (code) => over(code === 0));
|
|
707
|
+
clearInterval(this.ticker);
|
|
708
|
+
const ticker = setInterval(() => this.paused || this.redraw(), 500);
|
|
709
|
+
ticker.unref?.();
|
|
710
|
+
this.ticker = ticker;
|
|
711
|
+
this.redraw();
|
|
712
|
+
}
|
|
713
|
+
pause() {
|
|
714
|
+
if (!this.child || this.paused)
|
|
715
|
+
return;
|
|
716
|
+
this.before = this.played();
|
|
717
|
+
this.paused = true;
|
|
718
|
+
const child = this.child;
|
|
719
|
+
if (this.player?.from) {
|
|
720
|
+
// It can start in the middle: end it, and play starts one from here.
|
|
721
|
+
this.child = undefined;
|
|
722
|
+
this.signal(child, "SIGKILL");
|
|
723
|
+
}
|
|
724
|
+
else
|
|
725
|
+
this.signal(child, "SIGSTOP");
|
|
726
|
+
this.redraw();
|
|
727
|
+
}
|
|
728
|
+
/** Back to the start. */
|
|
729
|
+
stop() {
|
|
730
|
+
const child = this.child;
|
|
731
|
+
clearInterval(this.ticker);
|
|
732
|
+
this.child = undefined;
|
|
733
|
+
this.paused = false;
|
|
734
|
+
this.before = 0;
|
|
735
|
+
if (sounding === this)
|
|
736
|
+
sounding = undefined;
|
|
737
|
+
if (child)
|
|
738
|
+
this.signal(child, "SIGKILL");
|
|
739
|
+
this.redraw();
|
|
740
|
+
}
|
|
741
|
+
handleMouse(e) {
|
|
742
|
+
if (e.button !== "left" || (e.type !== "press" && e.type !== "click" && e.type !== "release"))
|
|
743
|
+
return undefined;
|
|
744
|
+
if (e.type === "click") {
|
|
745
|
+
if (this.openFrom >= 0 && e.x >= this.openFrom)
|
|
746
|
+
this.open(this.s.file);
|
|
747
|
+
else if (e.x <= this.playTo)
|
|
748
|
+
this.state === "playing" ? this.pause() : this.play();
|
|
749
|
+
}
|
|
750
|
+
return { handled: true, render: true };
|
|
751
|
+
}
|
|
752
|
+
render(width) {
|
|
753
|
+
const control = !this.player ? "" : this.state === "playing" ? "‖ pause" : "▶ play";
|
|
754
|
+
const total = this.s.seconds ? clock(this.s.seconds) : "";
|
|
755
|
+
const at = this.s.seconds ? Math.min(this.played() / 1000, this.s.seconds) : this.played() / 1000;
|
|
756
|
+
const time = this.state !== "idle" ? `${clock(at)}${total ? ` / ${total}` : ""}` : total;
|
|
757
|
+
const note = this.failed ? " · could not be played here" : !this.player ? " · no program here that plays sound" : "";
|
|
758
|
+
const left = ` ${control ? `${c.accent(control)} ` : ""}${c.gray(`${control ? "· " : ""}♪ ${this.s.name}${time ? ` · ${time}` : ""}${note}`)}`;
|
|
759
|
+
this.playTo = control ? 1 + visibleWidth(control) : -1;
|
|
760
|
+
this.openFrom = visibleWidth(left) + 3;
|
|
761
|
+
return [truncateToWidth(`${left} ${c.accent("▸ open in player")}`, width)];
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
/** Stop whatever sound is playing, for example when the UI closes. */
|
|
765
|
+
export function stopSounds() {
|
|
766
|
+
sounding?.stop();
|
|
767
|
+
}
|
|
768
|
+
const AUDIO_FILE = /\.(wav|mp3|m4a|ogg|oga|flac|aiff?)$/i;
|
|
769
|
+
export const isAudioFile = (name) => AUDIO_FILE.test(name);
|
|
770
|
+
const VIDEO_FILE = /\.(mp4|m4v|mov|webm|mkv)$/i;
|
|
771
|
+
export const isVideoFile = (name) => VIDEO_FILE.test(name);
|
|
772
|
+
const IMAGE_FILE = /\.(png|jpe?g|gif|webp)$/i;
|
|
773
|
+
export const isImageFile = (name) => IMAGE_FILE.test(name);
|
|
774
|
+
export const mimeOf = (name) => (/\.png$/i.test(name) ? "image/png" : /\.jpe?g$/i.test(name) ? "image/jpeg" : /\.gif$/i.test(name) ? "image/gif" : /\.webp$/i.test(name) ? "image/webp" : "");
|
|
775
|
+
/** The pictures an answer points at with . Code blocks are left alone. */
|
|
776
|
+
export function picturesIn(markdown, max = 6) {
|
|
777
|
+
const text = markdown.replace(/```[\s\S]*?(```|$)/g, "").replace(/`[^`\n]*`/g, "");
|
|
778
|
+
const found = [];
|
|
779
|
+
for (const m of text.matchAll(/!\[([^\]\n]*)\]\(\s*<?([^)\s>]+)>?(?:\s+"[^"\n]*")?\s*\)/g)) {
|
|
780
|
+
let target = m[2];
|
|
781
|
+
if (/^data:/i.test(target))
|
|
782
|
+
continue;
|
|
783
|
+
const remote = /^https?:\/\//i.test(target);
|
|
784
|
+
if (!remote) {
|
|
785
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(target) && !/^file:/i.test(target))
|
|
786
|
+
continue;
|
|
787
|
+
target = target.replace(/^file:\/\//i, "");
|
|
788
|
+
try {
|
|
789
|
+
target = decodeURIComponent(target);
|
|
790
|
+
}
|
|
791
|
+
catch {
|
|
792
|
+
// keep it as written
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
if (!found.some((f) => f.target === target))
|
|
796
|
+
found.push({ caption: m[1].trim(), target, remote });
|
|
797
|
+
if (found.length >= max)
|
|
798
|
+
break;
|
|
799
|
+
}
|
|
800
|
+
return found;
|
|
801
|
+
}
|