Crate stylic

Source
Expand description

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:

  • bold
  • dim
  • italic
  • underlined
  • blink
  • inverted
  • hidden
  • strikethrough

Available colors include basic ANSI colors, the extended 256-color palette, and RGB colors.

The ANSI colors are:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

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));

You can add a hyperlink to a styled value using the Styled::link method:

use stylic::Styleable;

println!("{}", "Example!".styled().link("https://example.com"));

Macros§

lazy_format
Format a string lazily.

Structs§

Attributes
Attributes such as bold, italic, etc.
Style
A style that can be applied to a value.
Styled
A styled value.
WithLink
Type parameter for Styled value with an associated link.
WithoutLink
Type parameter for Styled value with no associated link.

Enums§

BasicColor
A color from the basic 16-color palette.
Color
A color.

Traits§

Styleable
A trait for making types styleable with ANSI colors and attributes.