7 releases
Uses new Rust 2024
| 0.3.1 | Mar 24, 2025 |
|---|---|
| 0.3.0 | Mar 24, 2025 |
| 0.2.3 | Sep 6, 2024 |
| 0.1.0 | Sep 1, 2024 |
#277 in Command-line interface
293 downloads per month
41KB
877 lines
A simple, fast library for styling text with ANSI escape codes.
Features
- Simple, intuitive API
- Fast, no allocations
- No dependencies
#![no_std]out of the box- Hyperlink support
Basic Usage
Import 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));
Styling
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:
bolddimitalicunderlinedblinkinvertedhiddenstrikethrough
Available colors include basic ANSI colors, the extended 256-color palette, and RGB colors.
The ANSI colors are:
blackredgreenyellowbluemagentacyanwhite
Plus 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));
Hyperlinks
You can add a hyperlink to a styled value using the Styled::link method:
use stylic::Styleable;
println!("{}", "Example!".styled().link("https://example.com"));