Module tutil::crayon
[−]
[src]
A module for handling terminal output styling.
It would be fair to say that this module is a rip off of is heavily
influenced by ogham's ansi_term crate and
peter-murach's Pastel library. However if there is
something original of mine in here it would be true-colour support at the
very least.
OS Support
- Linux
- OS X
- FreeBSD
Most other POSIX/*nix systems will probably work as well.
Windows support is planned.
Basic Usage
The two main data structures in this module are Style
, which holds
stylistic information such as the foreground colour, the background colour
and other properties such as if the text should be bold, blinking, etc. and
ANSIString
, which is string paired with a Style
.
In order to format a string, call the paint()
method on a Style
or
Color
. For example, here is how you get red text and blue text:
use tutil::crayon::Color::{Red, Blue}; println!("{}", Red.paint("Hello world in red!")); println!("{}", Blue.paint("Hello world in blue!"));
It is worth noting that paint()
does not actually return a string with the
escape codes surrounding it, but instead returns a StyledString
that has
an implementation of Display
that will return the escape codes as well as
the string when formatted.
In the case that you do want the escape codes, you can convert the
StyledString
to a string:
use std::string::ToString; use tutil::crayon::Color::Blue; let string = Blue.paint("Hello!").to_string(); // => "\x1b[34mTEST\x1b[0m"
Advanced Usage
For complex styling you can construct a Style
object rather than operating
directly on a Color
:
use tutil::crayon::Style; use tutil::crayon::Color::{Red, Blue}; // Red blinking text on a black background: println!("This will be {} and this will be {}.", Style::new().foreground(Red).bold().paint("red and bold"), Style::new().foreground(Blue).italic().paint("blue and italic"));
The same styling methods that you can use on a Style
have been implemented
on the Color
struct, allowing you to skip creating an empty Style
with
Style::new()
:
use tutil::crayon::Color::{Red, Blue}; // Red blinking text on a black background: println!("This will be {} and this will be {}.", Red.bold().paint("red and bold"), Blue.italic().paint("blue and italic"));
Structs
Style |
A collection of properties that are used to format a string. |
StyledString |
A string coupled with a |
Enums
Color |
A |