6 releases (3 breaking)

0.11.1 Jan 31, 2025
0.11.0 Jan 31, 2025
0.10.0 Jul 12, 2024
0.9.0 Jul 10, 2024
0.1.1 Jun 13, 2024

#398 in GUI

Download history 1/week @ 2024-12-05 2/week @ 2024-12-12 260/week @ 2025-01-30 12/week @ 2025-02-06

272 downloads per month

Apache-2.0

490KB
8K SLoC

Pretty 🌸 Pretty

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

[ 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 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.

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.

Dependencies

~0–7MB
~44K SLoC