@kud/cli-shot 0.1.0 → 0.1.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/README.md CHANGED
@@ -56,8 +56,10 @@ cli-shot --out shots --no-mock -- pcloud # real data (careful)
56
56
  | `-s, --screen <name>` | shoot one screen instead of every screen |
57
57
  | `--list` | print the screens the command offers, and stop |
58
58
  | `--cols` / `--rows` | terminal size (default 110×32) |
59
- | `--settle <ms>` | silence that counts as finished drawing (default 800) |
59
+ | `--settle <ms>` | how long the screen must hold still (default 350) |
60
+ | `--jobs <n>` | screens shot at once; defaults to the core count |
60
61
  | `--keys <sequence>` | keystrokes sent once the screen has drawn |
62
+ | `--font <family>` | font to render with; defaults to a Nerd Font |
61
63
  | `--no-mock` | drive real data instead of fixtures |
62
64
 
63
65
  ## The contract
@@ -1,3 +1,4 @@
1
+ import { cpus } from 'os';
1
2
  import { dirname, join } from 'path';
2
3
  import { mkdirSync, existsSync, accessSync, constants } from 'fs';
3
4
  import { createRequire } from 'module';
@@ -8,6 +9,8 @@ import { execFileSync } from 'child_process';
8
9
 
9
10
  // src/index.ts
10
11
  var { Terminal } = headless;
12
+ var TICK_MS = 60;
13
+ var HELPER_FAILURE = /execvp\(\d\) failed|posix_spawnp failed/;
11
14
  var explainSpawnFailure = (error, command) => {
12
15
  const message = error instanceof Error ? error.message : String(error);
13
16
  const helper = helperPath();
@@ -39,7 +42,7 @@ var isExecutable = (path) => {
39
42
  var capture = (command, args = [], {
40
43
  cols = 110,
41
44
  rows = 32,
42
- settle = 800,
45
+ settle = 350,
43
46
  timeout = 15e3,
44
47
  env,
45
48
  keys
@@ -60,13 +63,14 @@ var capture = (command, args = [], {
60
63
  return;
61
64
  }
62
65
  let raw = 0;
63
- let idle;
66
+ let rawText = "";
64
67
  let sentKeys = false;
65
68
  let finished = false;
69
+ let previous = "";
66
70
  const finish = () => {
67
71
  if (finished) return;
68
72
  finished = true;
69
- clearTimeout(idle);
73
+ clearInterval(poll);
70
74
  clearTimeout(hard);
71
75
  try {
72
76
  pty.kill();
@@ -74,48 +78,53 @@ var capture = (command, args = [], {
74
78
  }
75
79
  term.write("", () => resolve(serializer.serialize()));
76
80
  };
77
- const drawn = () => {
81
+ const grid = () => {
78
82
  const buffer = term.buffer.active;
83
+ const lines = [];
79
84
  for (let y = 0; y < buffer.length; y++) {
80
- if ((buffer.getLine(y)?.translateToString(true) ?? "").trim())
81
- return true;
85
+ lines.push(buffer.getLine(y)?.translateToString(true) ?? "");
82
86
  }
83
- return false;
87
+ return lines.join("\n");
84
88
  };
85
- const onSettled = () => {
86
- if (!drawn()) {
87
- idle = setTimeout(onSettled, settle);
89
+ let stableFor = 0;
90
+ const poll = setInterval(() => {
91
+ const current = grid();
92
+ if (current !== previous || !current.trim()) {
93
+ previous = current;
94
+ stableFor = 0;
88
95
  return;
89
96
  }
97
+ stableFor += TICK_MS;
98
+ if (stableFor < settle) return;
90
99
  if (keys && !sentKeys) {
91
100
  sentKeys = true;
92
101
  pty.write(keys);
93
- idle = setTimeout(onSettled, settle);
102
+ previous = "";
103
+ stableFor = 0;
94
104
  return;
95
105
  }
96
106
  finish();
97
- };
107
+ }, TICK_MS);
98
108
  const hard = setTimeout(finish, timeout);
99
109
  pty.onData((data) => {
100
110
  raw += data.length;
111
+ if (rawText.length < 2048) rawText += data;
101
112
  term.write(data);
102
- clearTimeout(idle);
103
- idle = setTimeout(onSettled, settle);
104
113
  });
105
114
  pty.onExit(({ exitCode }) => {
106
- clearTimeout(idle);
107
- if (exitCode !== 0 && raw === 0) {
115
+ if (exitCode !== 0 && (raw === 0 || HELPER_FAILURE.test(rawText))) {
108
116
  finished = true;
117
+ clearInterval(poll);
109
118
  clearTimeout(hard);
110
119
  reject(
111
120
  explainSpawnFailure(
112
- new Error(`exited ${exitCode} without drawing anything`),
121
+ new Error(`the command exited ${exitCode} without drawing a screen`),
113
122
  command
114
123
  )
115
124
  );
116
125
  return;
117
126
  }
118
- idle = setTimeout(onSettled, 0);
127
+ finish();
119
128
  });
120
129
  });
121
130
  var listScreens = (command, args = []) => {
@@ -124,8 +133,17 @@ var listScreens = (command, args = []) => {
124
133
  });
125
134
  return printed.split("\n").map((line) => line.trim()).filter(Boolean);
126
135
  };
127
- var toImage = (ansi, output, { freezeArgs = [] } = {}) => {
128
- const args = ["--language", "ansi", "--output", output, ...freezeArgs];
136
+ var DEFAULT_FONT = "JetBrainsMono Nerd Font Mono";
137
+ var toImage = (ansi, output, { font = DEFAULT_FONT, freezeArgs = [] } = {}) => {
138
+ const args = [
139
+ "--language",
140
+ "ansi",
141
+ "--output",
142
+ output,
143
+ "--font.family",
144
+ font,
145
+ ...freezeArgs
146
+ ];
129
147
  mkdirSync(dirname(output), { recursive: true });
130
148
  try {
131
149
  execFileSync("freeze", args, { input: ansi });
@@ -148,13 +166,27 @@ var shootScreen = async (screen, { command, args = [], out, mock = true, ...rest
148
166
  const ansi = await capture(command, screenArgs(args, screen, mock), rest);
149
167
  return toImage(ansi, join(out, `${screen}.png`), rest);
150
168
  };
169
+ var defaultConcurrency = () => Math.max(2, Math.min(8, cpus().length));
170
+ var pool = async (items, limit, run) => {
171
+ const results = new Array(items.length);
172
+ let next = 0;
173
+ const worker = async () => {
174
+ for (let i = next++; i < items.length; i = next++) {
175
+ results[i] = await run(items[i], i);
176
+ }
177
+ };
178
+ await Promise.all(
179
+ Array.from({ length: Math.min(limit, items.length) }, worker)
180
+ );
181
+ return results;
182
+ };
151
183
  var shootAll = async (options) => {
152
184
  const screens = listScreens(options.command, options.args ?? []);
153
- const written = [];
154
- for (const screen of screens) {
155
- written.push(await shootScreen(screen, options));
156
- }
157
- return written;
185
+ return pool(
186
+ screens,
187
+ options.concurrency ?? defaultConcurrency(),
188
+ (screen) => shootScreen(screen, options)
189
+ );
158
190
  };
159
191
 
160
- export { capture, listScreens, shootAll, shootScreen, toImage };
192
+ export { DEFAULT_FONT, capture, listScreens, shootAll, shootScreen, toImage };
package/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { listScreens, shootScreen, shootAll } from './chunk-AE27RX6D.js';
2
+ import { DEFAULT_FONT, listScreens, shootScreen, shootAll } from './chunk-RT6Y4NTY.js';
3
3
  import { Command } from 'commander';
4
4
 
5
5
  var separator = process.argv.indexOf("--");
@@ -8,7 +8,11 @@ var own = separator === -1 ? process.argv : process.argv.slice(0, separator);
8
8
  var program = new Command();
9
9
  program.name("cli-shot").description(
10
10
  "Screenshot an interactive CLI, one image per screen.\n\nThe command to drive follows `--`:\n cli-shot --out assets/screenshots -- pcloud"
11
- ).requiredOption("-o, --out <dir>", "directory to write PNGs into").option("-s, --screen <name>", "shoot one screen instead of every screen").option("--list", "print the screens the command offers, and stop").option("--cols <n>", "terminal columns", "110").option("--rows <n>", "terminal rows", "32").option("--settle <ms>", "silence that counts as finished drawing", "800").option("--keys <sequence>", "keystrokes to send once the screen has drawn").option("--no-mock", "drive real data instead of fixtures").parse(own);
11
+ ).requiredOption("-o, --out <dir>", "directory to write PNGs into").option("-s, --screen <name>", "shoot one screen instead of every screen").option("--list", "print the screens the command offers, and stop").option("--cols <n>", "terminal columns", "110").option("--rows <n>", "terminal rows", "32").option("--settle <ms>", "how long the screen must hold still", "350").option("--keys <sequence>", "keystrokes to send once the screen has drawn").option(
12
+ "--font <family>",
13
+ "font to render with; needs Nerd Font coverage for TUI icons",
14
+ DEFAULT_FONT
15
+ ).option("--jobs <n>", "screens to shoot at once; defaults to core count").option("--no-mock", "drive real data instead of fixtures").parse(own);
12
16
  var options = program.opts();
13
17
  if (driven.length === 0) {
14
18
  console.error(
@@ -25,7 +29,9 @@ var shoot = {
25
29
  cols: Number(options.cols),
26
30
  rows: Number(options.rows),
27
31
  settle: Number(options.settle),
28
- keys: options.keys
32
+ keys: options.keys,
33
+ font: options.font,
34
+ concurrency: options.jobs ? Number(options.jobs) : void 0
29
35
  };
30
36
  if (options.list) {
31
37
  console.log(listScreens(command, args).join("\n"));
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  type CaptureOptions = {
2
2
  cols?: number;
3
3
  rows?: number;
4
- /** Milliseconds of silence that count as "finished drawing". */
4
+ /** How long the rendered screen must hold still to count as finished. */
5
5
  settle?: number;
6
6
  /** Hard limit before the child is killed regardless. */
7
7
  timeout?: number;
@@ -11,11 +11,14 @@ type CaptureOptions = {
11
11
  };
12
12
  declare const capture: (command: string, args?: readonly string[], { cols, rows, settle, timeout, env, keys, }?: CaptureOptions) => Promise<string>;
13
13
 
14
+ declare const DEFAULT_FONT = "JetBrainsMono Nerd Font Mono";
14
15
  type RenderOptions = {
16
+ /** Font family freeze renders with. Needs Nerd Font coverage for TUI icons. */
17
+ font?: string;
15
18
  /** Extra flags passed straight to freeze, e.g. ["--theme", "nord"]. */
16
19
  freezeArgs?: readonly string[];
17
20
  };
18
- declare const toImage: (ansi: string, output: string, { freezeArgs }?: RenderOptions) => string;
21
+ declare const toImage: (ansi: string, output: string, { font, freezeArgs }?: RenderOptions) => string;
19
22
 
20
23
  declare const listScreens: (command: string, args?: readonly string[]) => string[];
21
24
 
@@ -32,8 +35,10 @@ type ShootOptions = CaptureOptions & RenderOptions & {
32
35
  * screenshot outlives the moment it was taken.
33
36
  */
34
37
  mock?: boolean;
38
+ /** How many screens to shoot at once. Defaults to the core count. */
39
+ concurrency?: number;
35
40
  };
36
41
  declare const shootScreen: (screen: string, { command, args, out, mock, ...rest }: ShootOptions) => Promise<string>;
37
42
  declare const shootAll: (options: ShootOptions) => Promise<string[]>;
38
43
 
39
- export { type CaptureOptions, type RenderOptions, type ShootOptions, capture, listScreens, shootAll, shootScreen, toImage };
44
+ export { type CaptureOptions, DEFAULT_FONT, type RenderOptions, type ShootOptions, capture, listScreens, shootAll, shootScreen, toImage };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { capture, listScreens, shootAll, shootScreen, toImage } from './chunk-AE27RX6D.js';
1
+ export { DEFAULT_FONT, capture, listScreens, shootAll, shootScreen, toImage } from './chunk-RT6Y4NTY.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kud/cli-shot",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Automatic screenshots of interactive CLIs — pty capture, terminal emulation, PNG out",
6
6
  "license": "MIT",