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 iTerm 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.
rust
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.
🌠In order for these to work you have to install the Rust toolchain and just
and
cargo-watch
:
rustup
by following the instructions
here.cargo-watch
using cargo install cargo-watch
.flamegraph
using cargo install flamegraph
.just
just
on your system using
cargo install just
. It is available for Linux, macOS, and Windows.
just
you can follow these
instructions.just
using cargo install just
or brew install just
you will
not get shell completions without doing one extra configuration step. So on Linux
it is best to use sudo apt install -y just
if you want them.Note to run a just command named
all
on Windows, you have to use the following:just --shell powershell.exe --shell-arg -c all
just build
just clean
just run
just run-release
just run-flamegraph
just test
just clippy
just docs
just serve-docs
. This is only useful if you SSH into a remote machine via
VSCode (where you build and serve the docs) and want to view the docs in a browser on
your local machine.just upgrade-deps
just rustfmt
The following commands will watch for changes in the source folder and re-run:
just watch-run
just watch-all-tests
just watch-one-test <test_name>
just watch-clippy
just watch-macro-expansion-one-test <test_name>
Here are some links: