MCPcopy Index your code
hub / github.com/apparebit/prettypretty

github.com/apparebit/prettypretty @v0.11.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.11.1 ↗ · + Follow
888 symbols 2,223 edges 67 files 348 documented · 39%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Pretty 🌸 Terminals

Run Tests, Build Wheels, & Publish to PyPI Publish to GitHub Pages

Pretty 🌸 Tty

[ Docs.rs | GitHub Pages | Rust Crate | Repository ]

Prettytty is a lightweight and flexible terminal library for Rust that has only one low-level dependency, i.e., libc on Unix and windows-sys on Windows. Its API is clean and simple: Open a [Connection] to the terminal and share it across threads as needed. Write [Command]s to [Output]. Read [Query] responses from [Input]. [Scan::read_token] takes care of low-level UTF-8 and ANSI escape sequence decoding and [Query::parse] turns token payloads into objects. A [cmd] library with 70+ built-in commands covers basic needs and then some.


Pretty 🌸 Pretty:

[ Docs.rs | GitHub Pages | Rust Crate | Python Package | Repository ]

🎖️ As featured on Real Python #211

🎖️ Inspired iTerm2's color preferences

Prettypretty is a Rust library with optional Python bindings that applies 2020s color science to 1970s terminals to facilitate scalable user interfaces. However, instead of progressive enhancement, it primarily relies on graceful degradation from high-resolution colors down to more limited terminal colors.

The three steps for better terminal styles are:

  1. Fluently declare high-resolution styles.
  2. Let prettypretty adjust styles to terminal capabilities and user preferences at program startup.
  3. Use adjusted styles at will.

Prettypretty seamlessly integrates with prettytty for querying the terminal for its current color theme. It then uses said color theme to produce more accurate results when converting high resultion colors down to 256 or 16 terminal colors. The integration also is entirely optional, controlled by the tty feature, and fairly small, requiring about 80 lines of code for Theme::query. Hence integration with another terminal library should be easy enough.

As far as colors are concerned, prettypretty comes with all the expressivity and convenience of high-resolution, floating point colors and color spaces, including the perceptually uniform Oklab, whether in Cartesian or polar form, with original or revised lightness. It further implements state-of-the-art algorithms for gamut-mapping, color interpolation, perceptual contrast, as well as its own hue- and lightness-based downsampling for optimal selection of ANSI colors.

Python Integration

The optional Python integration is enabled with the pyffi feature flag and relies on PyO3 and Maturin for building an extension module with the same functionality. Only where the Rust library uses trait implementations, the Python module uses dedicated methods. While prettytty takes care of terminal access for Rust, the Python version of prettypretty has its own terminal abstraction, with its own Pythonic interface.

Scripts Using Prettypretty

Besides the documentation, a good starting point for familiarizing yourself with prettypretty are the scripts:

  • prettypretty.progress illustrates the library's use on the example of a progress bar in less than 100 lines of Python. The finished progress bar is shown below for both light and dark themes.

    a complete, green progress bar under light mode a complete, green progress bar under dark mode

  • prettypretty.plot charts colors on the chroma/hue plane of Oklab, if you don't feed it colors defaulting to your terminal's current color scheme. Here's the one for the basic theme in Apple's Terminal.app:

    colors from the basic theme for Apple's Terminal.app in Oklch

  • prettypretty.grid visualizes perceptual contrast and color downsampling strategies, exhaustively for the 6x6x6 RGB cube embedded in 8-bit color and selectively for 32x32 slices through the much bigger 24-bit RGB cube.

    a grid visualizing the 6x6x6 embedded RGB cube

  • prettypretty.viz3d traces the boundaries of the visual gamut in 3D and saves the corresponding point cloud or mesh in PLY format. The screenshot below shows Vedo's rendering.

    a 3D visualization of the gamut for visible light,
          somewhat shaped like a fat, squat hot pocket

Acknowledgements

I wrote much of prettypretty over a two-month period in 2024. Twice. I first implemented the core color routines in Python and then I did so again in Rust. At this point, only the Rust version survives. But Python remains a tier-1 runtime target for prettypretty. Two things really helped with getting this project started. First, I had been toying with different approaches to terminal styles for a while and knew what I was looking for. Second, I benefitted tremendously from Lea Verou's and Chris Lilley's work on the Color.js library and CSS Color 4 specification. Prettypretty directly reuses Color.js' formulae for conversion between color spaces and implements several CSS Color 4 algorithms.


Copyright 2024-2025 Robert Grimm. The code in this repository has been released as open source under the Apache 2.0 license.

Extension points exported contracts — how you extend this code

Command (Interface)
A command for the terminal. Commands provide instructions to the terminal and are communicated in-band by writing ANSI [7 …
crates/prettytty/src/api.rs
SpectralDistribution (Interface)
A spectral distribution at nanometer resolution. A concrete implementation of this trait must provide methods that retu [5 …
crates/prettypretty/src/spectrum.rs
Query (Interface)
A command that receives a response. Queries are request/response interactions with the terminal. For purposes of this t [9 …
crates/prettytty/src/api.rs
Environment (Interface)
A trait to abstract over environment variable access. The standard library is a bit spartan when it comes to environmen [2 …
crates/prettypretty/src/util.rs
Sgr (Interface)
A command using select-graphic-rendition ANSI escape sequences. To facilitate composition, SGR commands implement [`Sgr [3 …
crates/prettytty/src/api.rs
FloatExt (Interface)
An extension trait for floating point numbers. For now, this trait exists solely to pre-compute the rounding factor for [2 …
crates/prettypretty/src/core/math.rs
Scan (Interface)
A scanner for UTF-8 characters and control sequences. An implementation of this trait provides the state machine necess [3 …
crates/prettytty/src/api.rs
WriteNicely (Interface)
Write bytes nicely. Conveniently, this trait's two methods have default implementations, and the trait has a default im [1 …
crates/prettytty/src/util.rs

Core symbols most depended-on inside this repo

to
called by 53
crates/prettypretty/src/object.rs
write_str
called by 47
crates/prettytty/src/util.rs
convert
called by 29
crates/prettypretty/src/core/conversion.rs
write_control
called by 27
prettypretty/terminal.py
write
called by 27
crates/prettytty/src/sys/unix.rs
write
called by 24
prettypretty/terminal.py
as_ref
called by 23
crates/prettypretty/src/theme.rs
log
called by 19
prettypretty/viz3d.py

Shape

Method 579
Function 193
Class 78
Enum 28
Interface 10

Languages

Rust74%
Python26%

Modules by API surface

prettypretty/terminal.py83 symbols
crates/prettypretty/src/termco.rs55 symbols
crates/prettypretty/src/object.rs51 symbols
crates/prettypretty/src/spectrum.rs45 symbols
test/runtime.py41 symbols
crates/prettypretty/src/core/conversion.rs41 symbols
crates/prettypretty/src/theme.rs39 symbols
crates/prettypretty/src/style/format.rs35 symbols
crates/prettypretty/src/style/styling.rs28 symbols
prettypretty/viz3d.py24 symbols
crates/prettytty/src/conn.rs23 symbols
crates/prettypretty/src/trans/translator.rs23 symbols

For agents

$ claude mcp add prettypretty \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact