Skip to main content

ANSI terminal emulation and conversion to ANSI, HTML, and plain text

Project description

ANSI terminal emulation and conversion to ANSI, HTML, and plain text

erbsland-ansi-convert is a Python 3.10+ library that simulates an ANSI terminal and converts the resulting terminal history into:

  • Plain text
  • Compact ANSI text
  • Compact HTML with CSS classes

It is designed to process terminal output from tools that emit ANSI escape sequences and render a static representation close to what users see in a real terminal.

Installation

pip install erbsland-ansi-convert

Quick Start

from pathlib import Path
from erbsland.ansi_convert import Terminal

terminal = Terminal(width=120, height=40, back_buffer_height=2000)
terminal.write(Path("path/to/output.log").read_text())

Path("out.txt").write_text(terminal.to_text())
Path("out.ansi.txt").write_text(terminal.to_ansi())
Path("out.visible-esc.txt").write_text(terminal.to_ansi(esc_char="␛"))
Path("out.html").write_text(terminal.to_html(class_prefix="my-ansi"))

For terminal capture files, prefer writeFile() to preserve carriage returns:

terminal = Terminal()
terminal.writeFile("capture-via-script-cmd.txt")

If your input text already normalized carriage returns to newlines, you can enable the progress-line collapse heuristic on write():

terminal.write(text, collapse_capture_updates=True)

Command Line

Installing the package also installs the erbsland-ansi-convert command:

erbsland-ansi-convert [-f/--format (text|ansi|html)] [-c|--collapse] [-C|--no-collapse] [-o/--output <file>] [<input file>|-]
  • -f, --format: output format (text, ansi, html), default ansi
  • -o, --output: write output to file, default stdout
  • -c, --collapse: enable collapse heuristic
  • -C, --no-collapse: disable collapse heuristic
  • <input file>: input capture file, or - for stdin

Supported Terminal Features

  • Control characters: BEL, BS, HT, LF, VT, FF, CR, DEL
  • Cursor controls (CSI A/B/C/D/E/F/G/H/f, ESC M, save/restore variants)
  • Erase functions (CSI J, CSI K including mode 3J for saved lines)
  • SGR text styles (bold, dim, italic, underline, blink, reverse, hidden, strike)
  • Basic + bright 8 ANSI colors for foreground/background
  • Common private modes: cursor visibility (?25) and alternate buffer (?47, ?1049)

Unknown sequences can optionally be reported as warnings.

HTML Output Model

to_html() produces:

<div class="{prefix}-block"><pre>...</pre></div>

Text styles are represented with flat span classes such as:

  • {prefix}-bold
  • {prefix}-red
  • {prefix}-background-blue

Spans are reopened only when style actually changes to keep output compact and easy to style.

Development

Install development dependencies:

pip install -r requirements-dev.txt

Run tests:

pytest

License

Copyright (c) 2026 Tobias Erbsland / Erbsland DEV (https://erbsland.dev)

Licensed under the Apache License, Version 2.0. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

erbsland_ansi_convert-0.9.1.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

erbsland_ansi_convert-0.9.1-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file erbsland_ansi_convert-0.9.1.tar.gz.

File metadata

  • Download URL: erbsland_ansi_convert-0.9.1.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for erbsland_ansi_convert-0.9.1.tar.gz
Algorithm Hash digest
SHA256 7d7e5431c735ccdee3f55937773e3833eb7e20775629b31924b1c06c0d1bdf21
MD5 36b24dc541747312d46c9bfa6b61aa58
BLAKE2b-256 68cd1ff8edcc68ba7652499ef358367ce55b893343fbaada8c3d1cac9e439bd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for erbsland_ansi_convert-0.9.1.tar.gz:

Publisher: publish.yml on erbsland-dev/erbsland-py-ansi-convert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file erbsland_ansi_convert-0.9.1-py3-none-any.whl.

File metadata

File hashes

Hashes for erbsland_ansi_convert-0.9.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f691fde8463992bc38165543c8e6974afb2878657acd9a8be677b7286ed238ad
MD5 2f5e5d59a9483f575ef489c8854c36b2
BLAKE2b-256 fa04f763ba1f28d32943330feb4f621ff3ada9a5f424fb536cafba1558e88221

See more details on using hashes here.

Provenance

The following attestation bundles were made for erbsland_ansi_convert-0.9.1-py3-none-any.whl:

Publisher: publish.yml on erbsland-dev/erbsland-py-ansi-convert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page