A library for conveniently creating styled ANSI text.
Features
no_stdsupport- No allocations
- No dependencies
- Hyperlink support
- [
styled] and [hyperlink] macros for convenient styling of formatted text const[Style] constructors allows defining styles as constants- Correctly supports nested styled text (currently requires
stdfeature) - Enable or disable styling globally using the [
set_style_mode] function
Basic styling
Use the [styled] macro to create styled text. This supports format arguments and returns an instance of [Styled], which has methods for styling the text.
use ;
println!;
Attributes that can be set using [Styled]'s methods are:
Colors can be from the 8-color ANSI palette, 256-color ANSI palette or RGB colors.
use ;
// 8-color ANSI palette
println!;
// 256-color ANSI palette
for i in 0..255
// RGB colors
println!;
Hyperlinks
Use the [hyperlink] macro to create hyperlinks. Similar to [styled], this supports format arguments and returns an instance of [Hyperlink], which has the same methods for styling the text.
Styles
Styles can also be created on their own, allowing them to be reused:
use ;
const MY_STYLE: Style = new.bold.italic;
println!;
They have the same set of methods available as [Styled].
They can also be applied to hyperlinks:
use ;
const MY_STYLE: Style = new.bold.italic;
println!;
Enabling or disabling styling globally
You can use the [set_style_mode] function to enable or disable styling globally. By default, it is auto-detected from the environment (in a non-std environment, it is enabled by default).
use ;
set_style_mode;
// Styling will not be applied
println!;
Nested styled text
Consider the following case, which prints some text in cyan, with the word "brown" in brown:
use ;
println!;
By default, this results in all styling being reset after the word "brown". However, by enabling the nested feature (which currently also enables the std feature), this is handled correctly, and the remaining text is printed cyan.