| Crates.io | stylic |
| lib.rs | stylic |
| version | 0.3.1 |
| created_at | 2024-09-01 13:21:50.910771+00 |
| updated_at | 2025-03-24 20:49:15.74768+00 |
| description | A simple, fast library for styling text with ANSI escape codes |
| homepage | |
| repository | https://github.com/SamuelMcGowan/stylic |
| max_upload_size | |
| id | 1359623 |
| size | 58,436 |
A simple, fast library for styling text with ANSI escape codes.
#![no_std] out of the boxImport the Styleable trait for an easy way to create
styled values. Then use the methods from Styled to style the value.
use stylic::Styleable;
println!("{}", "Hello, World!".styled().green().italic());
You can also create a style and apply it later. Style has the same
styling options as Styled. Styling methods (on both types) are const,
so you can create constant styles.
use stylic::{Style, Styleable};
const EMPHASIS: Style = Style::new().bold().italic();
println!("To be or not to be, {} is the question.", "that".styled_with(EMPHASIS));
Both Style and Styled have methods for setting
the foreground color, background color, underline color, and attributes of the text
(such as bold, italic, etc).
Available attributes are:
bolddimitalicunderlinedblinkinvertedhiddenstrikethroughAvailable colors include basic ANSI colors, the extended 256-color palette, and RGB colors.
The ANSI colors are:
blackredgreenyellowbluemagentacyanwhitePlus bright variants.
There are methods for setting the foreground color, background color and underline color to basic ANSI colors:
use stylic::Styleable;
println!("{}", "Hello, World!".styled().black().on_blue());
There are also fg, bg
and underline_colored methods that take a Color,
allowing the use of colors from the extended 256-color palette and RGB colors:
use stylic::{Styleable, BasicColor, Color};
// Setting the foreground color to red.
println!("{}", "Hello, World!".styled().fg(Color::Basic(BasicColor::Red)));
println!("{}", "Hello, World!".styled().fg(BasicColor::Red.into()));
// Setting the background color to a color from the 256-color palette.
println!("{}", "Hello, World!".styled().bg(Color::Extended(58)));
println!("{}", "Hello, World!".styled().bg(58.into()));
// Setting the underline color to a RGB color.
println!("{}", "Hello, World!".styled().underline_colored(Color::Rgb(255, 0, 255)));
println!("{}", "Hello, World!".styled().underline_colored((255, 0, 255).into()));
You can also create attributes separately and apply them later:
use stylic::{Styleable, Attributes};
let my_attrs = Attributes::ITALIC | Attributes::STRIKETHROUGH;
println!("My homework was {}", "redacted".styled().attributes(my_attrs));
Attributes have methods for performing bitwise operations in a const environment:
use stylic::{Styleable, Attributes};
const MY_ATTRS: Attributes = Attributes::ITALIC.or(Attributes::BLINK);
println!("Did you hear about the {}?", "thing".styled().attributes(MY_ATTRS));
You can add a hyperlink to a styled value using the Styled::link method:
use stylic::Styleable;
println!("{}", "Example!".styled().link("https://example.com"));