@kud/cli-shot 0.1.0 → 0.1.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 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,7 @@ import { execFileSync } from 'child_process';
8
9
 
9
10
  // src/index.ts
10
11
  var { Terminal } = headless;
12
+ var TICK_MS = 60;
11
13
  var explainSpawnFailure = (error, command) => {
12
14
  const message = error instanceof Error ? error.message : String(error);
13
15
  const helper = helperPath();
@@ -39,7 +41,7 @@ var isExecutable = (path) => {
39
41
  var capture = (command, args = [], {
40
42
  cols = 110,
41
43
  rows = 32,
42
- settle = 800,
44
+ settle = 350,
43
45
  timeout = 15e3,
44
46
  env,
45
47
  keys
@@ -60,13 +62,13 @@ var capture = (command, args = [], {
60
62
  return;
61
63
  }
62
64
  let raw = 0;
63
- let idle;
64
65
  let sentKeys = false;
65
66
  let finished = false;
67
+ let previous = "";
66
68
  const finish = () => {
67
69
  if (finished) return;
68
70
  finished = true;
69
- clearTimeout(idle);
71
+ clearInterval(poll);
70
72
  clearTimeout(hard);
71
73
  try {
72
74
  pty.kill();
@@ -74,38 +76,42 @@ var capture = (command, args = [], {
74
76
  }
75
77
  term.write("", () => resolve(serializer.serialize()));
76
78
  };
77
- const drawn = () => {
79
+ const grid = () => {
78
80
  const buffer = term.buffer.active;
81
+ const lines = [];
79
82
  for (let y = 0; y < buffer.length; y++) {
80
- if ((buffer.getLine(y)?.translateToString(true) ?? "").trim())
81
- return true;
83
+ lines.push(buffer.getLine(y)?.translateToString(true) ?? "");
82
84
  }
83
- return false;
85
+ return lines.join("\n");
84
86
  };
85
- const onSettled = () => {
86
- if (!drawn()) {
87
- idle = setTimeout(onSettled, settle);
87
+ let stableFor = 0;
88
+ const poll = setInterval(() => {
89
+ const current = grid();
90
+ if (current !== previous || !current.trim()) {
91
+ previous = current;
92
+ stableFor = 0;
88
93
  return;
89
94
  }
95
+ stableFor += TICK_MS;
96
+ if (stableFor < settle) return;
90
97
  if (keys && !sentKeys) {
91
98
  sentKeys = true;
92
99
  pty.write(keys);
93
- idle = setTimeout(onSettled, settle);
100
+ previous = "";
101
+ stableFor = 0;
94
102
  return;
95
103
  }
96
104
  finish();
97
- };
105
+ }, TICK_MS);
98
106
  const hard = setTimeout(finish, timeout);
99
107
  pty.onData((data) => {
100
108
  raw += data.length;
101
109
  term.write(data);
102
- clearTimeout(idle);
103
- idle = setTimeout(onSettled, settle);
104
110
  });
105
111
  pty.onExit(({ exitCode }) => {
106
- clearTimeout(idle);
107
112
  if (exitCode !== 0 && raw === 0) {
108
113
  finished = true;
114
+ clearInterval(poll);
109
115
  clearTimeout(hard);
110
116
  reject(
111
117
  explainSpawnFailure(
@@ -115,7 +121,7 @@ var capture = (command, args = [], {
115
121
  );
116
122
  return;
117
123
  }
118
- idle = setTimeout(onSettled, 0);
124
+ finish();
119
125
  });
120
126
  });
121
127
  var listScreens = (command, args = []) => {
@@ -124,8 +130,17 @@ var listScreens = (command, args = []) => {
124
130
  });
125
131
  return printed.split("\n").map((line) => line.trim()).filter(Boolean);
126
132
  };
127
- var toImage = (ansi, output, { freezeArgs = [] } = {}) => {
128
- const args = ["--language", "ansi", "--output", output, ...freezeArgs];
133
+ var DEFAULT_FONT = "JetBrainsMono Nerd Font Mono";
134
+ var toImage = (ansi, output, { font = DEFAULT_FONT, freezeArgs = [] } = {}) => {
135
+ const args = [
136
+ "--language",
137
+ "ansi",
138
+ "--output",
139
+ output,
140
+ "--font.family",
141
+ font,
142
+ ...freezeArgs
143
+ ];
129
144
  mkdirSync(dirname(output), { recursive: true });
130
145
  try {
131
146
  execFileSync("freeze", args, { input: ansi });
@@ -148,13 +163,27 @@ var shootScreen = async (screen, { command, args = [], out, mock = true, ...rest
148
163
  const ansi = await capture(command, screenArgs(args, screen, mock), rest);
149
164
  return toImage(ansi, join(out, `${screen}.png`), rest);
150
165
  };
166
+ var defaultConcurrency = () => Math.max(2, Math.min(8, cpus().length));
167
+ var pool = async (items, limit, run) => {
168
+ const results = new Array(items.length);
169
+ let next = 0;
170
+ const worker = async () => {
171
+ for (let i = next++; i < items.length; i = next++) {
172
+ results[i] = await run(items[i], i);
173
+ }
174
+ };
175
+ await Promise.all(
176
+ Array.from({ length: Math.min(limit, items.length) }, worker)
177
+ );
178
+ return results;
179
+ };
151
180
  var shootAll = async (options) => {
152
181
  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;
182
+ return pool(
183
+ screens,
184
+ options.concurrency ?? defaultConcurrency(),
185
+ (screen) => shootScreen(screen, options)
186
+ );
158
187
  };
159
188
 
160
- export { capture, listScreens, shootAll, shootScreen, toImage };
189
+ 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-ZWMKVITE.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-ZWMKVITE.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.1",
4
4
  "type": "module",
5
5
  "description": "Automatic screenshots of interactive CLIs — pty capture, terminal emulation, PNG out",
6
6
  "license": "MIT",