- Notifications
You must be signed in to change notification settings - Fork0
Pretty 🌸 pretty terminals for Rust and Python
License
apparebit/prettypretty
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
[Docs.rs|GitHub Pages|Rust Crate|Repository]
Prettytty is alightweight and flexible terminal library for Rust that hasonly one low-level dependency, i.e.,libc
onUnix andwindows-sys
on Windows. ItsAPI is clean and simple: Open aConnection
to the terminal and share itacross threads as needed. WriteCommand
s toOutput
. ReadQuery
responses fromInput
.Scan::read_token
takes care of low-level UTF-8 andANSI escape sequence decoding andQuery::parse
turns token payloads intoobjects. Acmd
library with 70+ built-in commands covers basic needs andthen some.
[Docs.rs|GitHub Pages|Rust Crate|Python Package|Repository]
🎖️ As featured onReal Python #211
🎖️ InspirediTerm2's color preferences
Prettypretty is a Rust library with optional Python bindings that applies2020s color science to 1970s terminals to facilitate scalable userinterfaces. However, instead of progressive enhancement, it primarily relies ongraceful degradation from high-resolution colors down to more limited terminalcolors.
Thethree steps for better terminal styles are:
- Fluently declare high-resolution styles.
- Let prettypretty adjust styles to terminal capabilities and user preferences atprogram startup.
- Use adjusted styles at will.
Prettypretty seamlessly integrates withprettytty forquerying the terminal forits current color theme. It then uses said color theme to produce moreaccurate results when converting high resultion colors down to 256 or 16terminal colors. The integration also is entirely optional, controlled by thetty
feature, and fairly small, requiring about 80 lines of code forTheme::query
.Hence integration with another terminal library should be easy enough.
As far as colors are concerned, prettypretty comes with all the expressivity andconvenience ofhigh-resolution, floating point colors andcolorspaces, including the perceptually uniformOklab, whether in Cartesian or polarform, with original orrevisedlightness.It further implements state-of-the-art algorithms forgamut-mapping,colorinterpolation,perceptualcontrast, as well as its own hue- andlightness-based downsampling for optimal selection of ANSI colors.
The optional Python integration is enabled with thepyffi
feature flag andrelies onPyO3 andMaturinfor building an extension module with the same functionality. Only where theRust library uses trait implementations, the Python moduleuses dedicatedmethods.While prettytty takes care of terminal access for Rust, the Python version ofprettypretty has its own terminal abstraction, with its own Pythonic interface.
Besides thedocumentation, a goodstarting point for familiarizing yourself with prettypretty are the scripts:
prettypretty.progressillustrates the library's use on the example of a progress bar in less than100 lines of Python. The finished progress bar is shown below for both lightand dark themes.
prettypretty.plotcharts colors on the chroma/hue plane of Oklab, if you don't feed it colorsdefaulting to your terminal's current color scheme. Here's the one for thebasic theme in Apple's Terminal.app:
prettypretty.gridvisualizes perceptual contrast and color downsampling strategies,exhaustively for the 6x6x6 RGB cube embedded in 8-bit color and selectivelyfor 32x32 slices through the much bigger 24-bit RGB cube.
prettypretty.viz3dtraces the boundaries of thevisual gamut in 3D and saves thecorresponding point cloud or mesh inPLYformat. The screenshotbelow showsVedo's rendering.
I wrote much of prettypretty over a two-month period in 2024. Twice. I firstimplemented 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-1runtime target for prettypretty. Two things really helped with getting thisproject started. First, I had been toying with different approaches to terminalstyles for a while and knew what I was looking for. Second, I benefittedtremendously fromLea Verou's andChrisLilley's work on theColor.js libraryandCSS Color 4 specification.Prettypretty directly reuses Color.js' formulae for conversion between colorspaces and implements several CSS Color 4 algorithms.
Copyright 2024-2025 Robert Grimm. The code in this repository has been releasedas open source under theApache2.0 license.
About
Pretty 🌸 pretty terminals for Rust and Python
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.