Rust crate to generate formatted ANSI 256 (8-bit) and truecolor (24-bit) color output to stdout. On macOS, the default Terminal.app does not support truecolor, so ANSI 256 colors are used instead.
This crate performs its own detection of terminal color capability heuristically. And does not use other crates to perform this function.
Here's a screenshot of running the main
example on various operating systems:
![]() |
---|
Running on Linux Tilix |
![]() |
---|
Running on Windows Terminal |
![]() |
---|
Running on macOS terminal app (note ANSI 256 runtime detection) |
![]() |
---|
Running on macOS terminal app (note Truecolor runtime detection) |
The main struct that we have to consider is AnsiStyledText
. It has two fields:
text
- the text to print.style
- a list of styles to apply to the text.
Here's an example.
AnsiStyledText {
text: "Print a formatted (bold, italic, underline) string w/ ANSI color codes.",
style: &[
Style::Bold,
Style::Italic,
Style::Underline,
Style::Foreground(Color::Rgb(50, 50, 50)),
Style::Background(Color::Rgb(100, 200, 1)),
],
}
.println();
Please a look at the
main
example to get a
better idea of how to use this crate.
- Build:
cargo build
- Run examples:
cargo run --example main
- Run tests:
cargo test
Fish scripts are provided to facilitate the above tasks. Here is a list of them.
- Build
build.fish
- build the crate.
- Run
run.fish
- run the examples.run-watch.fish
- watch the files in this folder and re-run the examples when they change.run-release.fish
- run the examples in release mode.run-with-flamegraph-profiling.fish
- run the examples with flamegraph profiling.
- Test
cargo-watch-all-tests.fish
- watch the files in this folder and re-run the tests whentest.fish
- run the tests.cargo-watch-one-test.fish
- same as above, but just watch a single test passed in as an argument to this script.cargo-watch-macro-expand-one-test.fish
- same as above, but also expand all the macros that are used in the test.
- Clippy
clippy.fish
- run clippy and re-run it when the files in this folder change.
- Docs
doc.fish
- generate the Rust docs.serve-doc.fish
- serve the Rust docs onhttp://localhost:3000
. This is useful when using SSH to connect to a remote machine to actually do development work on and you want to view the docs on your local machine. Make sure you havenode
andnpm
andserve
installed. If not you can get it withbrew
.
- https://notes.burke.libbey.me/ansi-escape-codes/
- https://www.asciitable.com/
- https://commons.wikimedia.org/wiki/File:Xterm_256color_chart.svg
- https://www.ditig.com/256-colors-cheat-sheet
- https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
- https://www.compuphase.com/cmetric.htm
- There are a few crates on crates.io that do similar things but they don't amenable licenses.
- Other crates simply ignore ANSI 256 colors and only support truecolor, even when they claim that they support it.
- And there are other crates which don't correctly report that macOS Terminal.app does not support truecolor and only supports ANSI 256 color.
Here are some links:
- rust-cli/concolor#47
- https://docs.rs/anstream/latest/anstream/
- https://docs.rs/colorchoice/latest/colorchoice/
- https://docs.rs/colorchoice-clap/latest/colorchoice_clap/
- https://docs.rs/anstyle-query/latest/anstyle_query/fn.term_supports_ansi_color.html
- https://crates.io/crates/anstyle-query
- https://docs.rs/supports-color/2.0.0/supports_color/
- https://crates.io/crates/ansi_colours
- https://crates.io/crates/colored