Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pretty 🌸 pretty terminals for Rust and Python

License

NotificationsYou must be signed in to change notification settings

apparebit/prettypretty

Repository files navigation

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

Pretty 🌸 Tty

[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. WriteCommands toOutput. ReadQueryresponses 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.


Pretty 🌸 Pretty:

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

  1. Fluently declare high-resolution styles.
  2. Let prettypretty adjust styles to terminal capabilities and user preferences atprogram startup.
  3. 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.

Python Integration

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.

Scripts Using Prettypretty

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.

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

  • 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:

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

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

    a grid visualizing the 6x6x6 embedded RGB cube

  • prettypretty.viz3dtraces the boundaries of thevisual gamut in 3D and saves thecorresponding point cloud or mesh inPLYformat. The screenshotbelow showsVedo'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 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.

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp