terminal-png 0.1.4 → 0.1.6

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Poe Platform
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -14,14 +14,22 @@ await renderTerminalPng(ansiText, {
14
14
  });
15
15
  ```
16
16
 
17
- Options:
17
+ ## Configuration Options
18
18
 
19
19
  - `output`: path to the output PNG file
20
20
  - `window`: include terminal window chrome
21
- - `padding`: padding around the rendered terminal content
21
+ - `padding`: non-negative integer padding around the rendered terminal content
22
+
23
+ The ANSI parser supports common SGR styling, cursor positioning and movement, save/restore cursor,
24
+ erase-in-line/display, tabs, double-width characters, and combining marks. Parsed terminal state is
25
+ bounded to 1000 rows by 1000 columns to avoid unbounded renders from hostile control sequences.
22
26
 
23
27
  ## CLI
24
28
 
25
29
  ```sh
26
30
  terminal-png <input.ansi> -o <output.png> [--window] [--padding 20]
27
31
  ```
32
+
33
+ ## Environment Variables
34
+
35
+ This package does not read public environment variables. The comparison script inherits `PATH` only to locate the local CLI under test.
@@ -1,4 +1,7 @@
1
1
  const ESC = "\u001b";
2
+ const TAB_WIDTH = 8;
3
+ const MAX_TERMINAL_ROWS = 1000;
4
+ const MAX_TERMINAL_COLUMNS = 1000;
2
5
  function createDefaultStyle() {
3
6
  return {
4
7
  fg: null,
@@ -295,10 +298,48 @@ function positionParameter(params, index, fallback) {
295
298
  const value = toInteger(params.split(";")[index]);
296
299
  return value === null || value < 1 ? fallback : value;
297
300
  }
301
+ function modeParameter(params, fallback) {
302
+ const value = toInteger(params.split(";")[0]);
303
+ return value === null ? fallback : value;
304
+ }
305
+ function displayWidth(text) {
306
+ if (text === "\t") {
307
+ return TAB_WIDTH;
308
+ }
309
+ const codePoint = text.codePointAt(0);
310
+ if (codePoint === undefined) {
311
+ return 0;
312
+ }
313
+ if ((codePoint >= 0x1100 && codePoint <= 0x115f) ||
314
+ codePoint === 0x2329 ||
315
+ codePoint === 0x232a ||
316
+ (codePoint >= 0x2e80 && codePoint <= 0xa4cf && codePoint !== 0x303f) ||
317
+ (codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
318
+ (codePoint >= 0xf900 && codePoint <= 0xfaff) ||
319
+ (codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
320
+ (codePoint >= 0xfe30 && codePoint <= 0xfe6f) ||
321
+ (codePoint >= 0xff00 && codePoint <= 0xff60) ||
322
+ (codePoint >= 0xffe0 && codePoint <= 0xffe6) ||
323
+ (codePoint >= 0x1f300 && codePoint <= 0x1f64f) ||
324
+ (codePoint >= 0x1f900 && codePoint <= 0x1f9ff) ||
325
+ (codePoint >= 0x20000 && codePoint <= 0x3fffd)) {
326
+ return 2;
327
+ }
328
+ return 1;
329
+ }
330
+ function hasRenderableCell(line) {
331
+ return (line ?? []).some((cell) => cell !== undefined && cell.text.length > 0);
332
+ }
298
333
  function buildRuns(lines, lineBreakStyles) {
299
334
  const runs = [];
300
335
  const defaultStyle = createDefaultStyle();
301
- for (let row = 0; row < lines.length; row += 1) {
336
+ let lastRow = lines.length - 1;
337
+ while (lastRow > 0 &&
338
+ !hasRenderableCell(lines[lastRow]) &&
339
+ lineBreakStyles[lastRow - 1] === undefined) {
340
+ lastRow -= 1;
341
+ }
342
+ for (let row = 0; row <= lastRow; row += 1) {
302
343
  const line = lines[row] ?? [];
303
344
  let lastCell = line.length - 1;
304
345
  while (lastCell >= 0 && line[lastCell] === undefined) {
@@ -308,7 +349,7 @@ function buildRuns(lines, lineBreakStyles) {
308
349
  const cell = line[column] ?? { text: " ", style: defaultStyle };
309
350
  pushRun(runs, cell.style, cell.text);
310
351
  }
311
- if (row < lines.length - 1) {
352
+ if (row < lastRow) {
312
353
  pushRun(runs, lineBreakStyles[row] ?? defaultStyle, "\n");
313
354
  }
314
355
  }
@@ -320,7 +361,21 @@ export function parseAnsi(input) {
320
361
  let style = createDefaultStyle();
321
362
  let row = 0;
322
363
  let column = 0;
364
+ let savedRow = 0;
365
+ let savedColumn = 0;
323
366
  let index = 0;
367
+ const clampRow = (nextRow) => {
368
+ if (nextRow < 0) {
369
+ return 0;
370
+ }
371
+ return Math.min(nextRow, MAX_TERMINAL_ROWS - 1);
372
+ };
373
+ const clampColumn = (nextColumn) => {
374
+ if (nextColumn < 0) {
375
+ return 0;
376
+ }
377
+ return Math.min(nextColumn, MAX_TERMINAL_COLUMNS - 1);
378
+ };
324
379
  const ensureLine = () => {
325
380
  while (lines.length <= row) {
326
381
  lines.push([]);
@@ -328,12 +383,64 @@ export function parseAnsi(input) {
328
383
  };
329
384
  const moveDown = (keepColumn) => {
330
385
  lineBreakStyles[row] = cloneStyle(style);
331
- row += 1;
386
+ row = clampRow(row + 1);
332
387
  if (!keepColumn) {
333
388
  column = 0;
334
389
  }
335
390
  ensureLine();
336
391
  };
392
+ const eraseLine = (mode) => {
393
+ const line = lines[row] ?? [];
394
+ if (mode === 1) {
395
+ for (let cell = 0; cell <= column; cell += 1) {
396
+ delete line[cell];
397
+ }
398
+ return;
399
+ }
400
+ if (mode === 2) {
401
+ lines[row] = [];
402
+ return;
403
+ }
404
+ line.splice(column);
405
+ };
406
+ const eraseDisplay = (mode) => {
407
+ if (mode === 1) {
408
+ for (let lineRow = 0; lineRow < row; lineRow += 1) {
409
+ lines[lineRow] = [];
410
+ }
411
+ eraseLine(1);
412
+ return;
413
+ }
414
+ if (mode === 2 || mode === 3) {
415
+ lines.length = 0;
416
+ lineBreakStyles.length = 0;
417
+ ensureLine();
418
+ return;
419
+ }
420
+ eraseLine(0);
421
+ lines.splice(row + 1);
422
+ lineBreakStyles.splice(row);
423
+ };
424
+ const saveCursor = () => {
425
+ savedRow = row;
426
+ savedColumn = column;
427
+ };
428
+ const restoreCursor = () => {
429
+ row = clampRow(savedRow);
430
+ column = clampColumn(savedColumn);
431
+ ensureLine();
432
+ };
433
+ const writeText = (text) => {
434
+ const width = displayWidth(text);
435
+ if (width === 0) {
436
+ return;
437
+ }
438
+ lines[row][column] = { text, style: cloneStyle(style) };
439
+ for (let offset = 1; offset < width && column + offset < MAX_TERMINAL_COLUMNS; offset += 1) {
440
+ lines[row][column + offset] = { text: "", style: cloneStyle(style) };
441
+ }
442
+ column = clampColumn(column + width);
443
+ };
337
444
  while (index < input.length) {
338
445
  const char = input[index];
339
446
  if (char === "\n") {
@@ -356,10 +463,28 @@ export function parseAnsi(input) {
356
463
  index += 1;
357
464
  continue;
358
465
  }
466
+ if (char === "\t") {
467
+ const nextTabStop = Math.min(Math.floor(column / TAB_WIDTH) * TAB_WIDTH + TAB_WIDTH, MAX_TERMINAL_COLUMNS);
468
+ while (column < nextTabStop) {
469
+ writeText(" ");
470
+ }
471
+ index += 1;
472
+ continue;
473
+ }
359
474
  if (char === ESC && input[index + 1] === "]") {
360
475
  index = parseOscEnd(input, index);
361
476
  continue;
362
477
  }
478
+ if (char === ESC && input[index + 1] === "7") {
479
+ saveCursor();
480
+ index += 2;
481
+ continue;
482
+ }
483
+ if (char === ESC && input[index + 1] === "8") {
484
+ restoreCursor();
485
+ index += 2;
486
+ continue;
487
+ }
363
488
  const csiPrefixLength = char === "\u009b" ? 1 : char === ESC && input[index + 1] === "[" ? 2 : null;
364
489
  if (csiPrefixLength !== null) {
365
490
  const sequence = parseCsi(input, index, csiPrefixLength);
@@ -367,33 +492,53 @@ export function parseAnsi(input) {
367
492
  style = applySgr(style, sequence.params);
368
493
  }
369
494
  else if (sequence.final === "G") {
370
- column = positionParameter(sequence.params, 0, 1) - 1;
495
+ column = clampColumn(positionParameter(sequence.params, 0, 1) - 1);
371
496
  }
372
497
  else if (sequence.final === "C") {
373
- column += positionParameter(sequence.params, 0, 1);
498
+ column = clampColumn(column + positionParameter(sequence.params, 0, 1));
374
499
  }
375
500
  else if (sequence.final === "D") {
376
501
  column = Math.max(0, column - positionParameter(sequence.params, 0, 1));
377
502
  }
378
503
  else if (sequence.final === "A") {
379
- row = Math.max(0, row - positionParameter(sequence.params, 0, 1));
504
+ row = clampRow(row - positionParameter(sequence.params, 0, 1));
380
505
  }
381
506
  else if (sequence.final === "B") {
382
- row += positionParameter(sequence.params, 0, 1);
507
+ row = clampRow(row + positionParameter(sequence.params, 0, 1));
383
508
  ensureLine();
384
509
  }
510
+ else if (sequence.final === "E") {
511
+ row = clampRow(row + positionParameter(sequence.params, 0, 1));
512
+ column = 0;
513
+ ensureLine();
514
+ }
515
+ else if (sequence.final === "F") {
516
+ row = clampRow(row - positionParameter(sequence.params, 0, 1));
517
+ column = 0;
518
+ }
385
519
  else if (sequence.final === "H" || sequence.final === "f") {
386
- row = positionParameter(sequence.params, 0, 1) - 1;
387
- column = positionParameter(sequence.params, 1, 1) - 1;
520
+ row = clampRow(positionParameter(sequence.params, 0, 1) - 1);
521
+ column = clampColumn(positionParameter(sequence.params, 1, 1) - 1);
388
522
  ensureLine();
389
523
  }
524
+ else if (sequence.final === "K") {
525
+ eraseLine(modeParameter(sequence.params, 0));
526
+ }
527
+ else if (sequence.final === "J") {
528
+ eraseDisplay(modeParameter(sequence.params, 0));
529
+ }
530
+ else if (sequence.final === "s") {
531
+ saveCursor();
532
+ }
533
+ else if (sequence.final === "u") {
534
+ restoreCursor();
535
+ }
390
536
  index = sequence.end;
391
537
  continue;
392
538
  }
393
539
  const codePoint = input.codePointAt(index);
394
540
  const text = codePoint === undefined ? "" : String.fromCodePoint(codePoint);
395
- lines[row][column] = { text, style: cloneStyle(style) };
396
- column += 1;
541
+ writeText(text);
397
542
  index += text.length;
398
543
  }
399
544
  return buildRuns(lines, lineBreakStyles);
package/dist/cli.js CHANGED
@@ -7,11 +7,30 @@ const defaultOutput = {
7
7
  stderr: process.stderr
8
8
  };
9
9
  function parsePadding(value) {
10
- const padding = Number(value);
11
- if (!Number.isInteger(padding) || padding < 0) {
12
- throw new InvalidArgumentError("padding must be a non-negative integer");
10
+ if (!isDecimalInteger(value)) {
11
+ throw new InvalidArgumentError("padding must be a non-negative decimal integer");
13
12
  }
14
- return padding;
13
+ return Number.parseInt(value, 10);
14
+ }
15
+ function isDecimalInteger(value) {
16
+ if (value === "0") {
17
+ return true;
18
+ }
19
+ if (value.length === 0 || value[0] === "0") {
20
+ return false;
21
+ }
22
+ for (const char of value) {
23
+ if (char < "0" || char > "9") {
24
+ return false;
25
+ }
26
+ }
27
+ return true;
28
+ }
29
+ function parseOutputPath(value) {
30
+ if (value.length === 0) {
31
+ throw new InvalidArgumentError("output path must not be empty");
32
+ }
33
+ return value;
15
34
  }
16
35
  export async function main(args = process.argv.slice(2), output = defaultOutput) {
17
36
  const program = new Command();
@@ -19,7 +38,7 @@ export async function main(args = process.argv.slice(2), output = defaultOutput)
19
38
  .name("terminal-png")
20
39
  .description("Render a PNG image from ANSI terminal output")
21
40
  .argument("<input>", "Path to the ANSI input file")
22
- .requiredOption("-o, --output <output>", "Path to the output PNG file")
41
+ .requiredOption("-o, --output <output>", "Path to the output PNG file", parseOutputPath)
23
42
  .option("--window", "Include terminal window chrome", true)
24
43
  .option("--no-window", "Exclude terminal window chrome")
25
44
  .option("--padding <n>", "Padding around terminal content", parsePadding)
@@ -32,7 +51,7 @@ export async function main(args = process.argv.slice(2), output = defaultOutput)
32
51
  });
33
52
  });
34
53
  program.configureOutput({
35
- writeErr: value => {
54
+ writeErr: (value) => {
36
55
  output.stderr.write(value);
37
56
  }
38
57
  });
@@ -50,9 +69,7 @@ export async function main(args = process.argv.slice(2), output = defaultOutput)
50
69
  return 1;
51
70
  }
52
71
  }
53
- const entryPoint = process.argv[1]
54
- ? pathToFileURL(path.resolve(process.argv[1])).href
55
- : null;
72
+ const entryPoint = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : null;
56
73
  if (entryPoint === import.meta.url) {
57
74
  const exitCode = await main();
58
75
  process.exitCode = exitCode;
@@ -0,0 +1 @@
1
+ export declare function hasOwnErrorCode(error: unknown, code: string): error is NodeJS.ErrnoException;
@@ -0,0 +1,6 @@
1
+ export function hasOwnErrorCode(error, code) {
2
+ return (typeof error === "object" &&
3
+ error !== null &&
4
+ Object.prototype.hasOwnProperty.call(error, "code") &&
5
+ error.code === code);
6
+ }
package/dist/index.js CHANGED
@@ -1,19 +1,24 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { rename, rm, writeFile } from "node:fs/promises";
3
3
  import { parseAnsi } from "./ansi-parser.js";
4
+ import { hasOwnErrorCode } from "./error-codes.js";
4
5
  import { renderPng } from "./png-renderer.js";
5
6
  import { renderSvg } from "./svg-renderer.js";
6
7
  export async function renderTerminalPng(ansiText, options = {}) {
7
- if (options.padding !== undefined && (!Number.isInteger(options.padding) || options.padding < 0)) {
8
+ if (options.padding !== undefined &&
9
+ (!Number.isInteger(options.padding) || options.padding < 0)) {
8
10
  throw new Error("Padding must be a non-negative integer.");
9
11
  }
12
+ if (options.output !== undefined && options.output.length === 0) {
13
+ throw new Error("Output path must not be empty.");
14
+ }
10
15
  const runs = parseAnsi(ansiText);
11
16
  const svg = renderSvg(runs, {
12
17
  padding: options.padding,
13
18
  window: options.window
14
19
  });
15
20
  const png = renderPng(svg);
16
- if (options.output) {
21
+ if (options.output !== undefined) {
17
22
  const temporaryPath = `${options.output}.${randomUUID()}.tmp`;
18
23
  let temporaryCreated = false;
19
24
  try {
@@ -36,7 +41,7 @@ export async function renderTerminalPng(ansiText, options = {}) {
36
41
  return png;
37
42
  }
38
43
  function isAlreadyExistsError(error) {
39
- return error instanceof Error && "code" in error && error.code === "EEXIST";
44
+ return error instanceof Error && hasOwnErrorCode(error, "EEXIST");
40
45
  }
41
46
  export * from "./ansi-parser.js";
42
47
  export * from "./font.js";
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "terminal-png",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
+ "license": "MIT",
4
5
  "description": "Render PNG images from ANSI terminal output.",
5
6
  "type": "module",
6
7
  "main": "./dist/index.js",
@@ -15,6 +16,7 @@
15
16
  "compare": "tsx scripts/compare.ts"
16
17
  },
17
18
  "files": [
19
+ "LICENSE",
18
20
  "dist",
19
21
  "bin",
20
22
  "assets"
@@ -26,7 +28,7 @@
26
28
  },
27
29
  "dependencies": {
28
30
  "@resvg/resvg-js": "^2.6.2",
29
- "commander": "^14.0.3",
31
+ "commander": "^13.1.0",
30
32
  "jetbrains-mono": "^1.0.6"
31
33
  }
32
34
  }