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.
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.
cargo build
cargo run --example main
cargo test
Fish scripts are provided to facilitate the above tasks. Here is a list of them.
build.fish
- build the crate.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.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.fish
- run clippy and re-run it when the files in this folder change.doc.fish
- generate the Rust docs.serve-doc.fish
- serve the Rust docs on http://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 have node
and npm
and serve
installed. If not
you can get it with brew
.Here are some links: