Represents a set of styling options.
See the crate level documentation for usage information.
The Style
structure exposes many methods for convenience. The majority of
these methods are shared with Paint
.
Return a new Style
structure with no styling applied.
Return a new Style
structure with a foreground color applied.
Return information about a Style
structure.
Set a style property on a given Style
structure.
These methods can be chained:
use yansi::Style;
Style::red().underline().invert().italic().dimmed().bold();
Convert a Style
into another structure.
Write the raw ANSI codes for a given Style
to a fmt::Formatter
.
Default, unstylized Style
. This is identical to Style::default()
.
use yansi::Style;
let plain = Style::new();
assert_eq!(plain, Style::default());
Default, unstylized but masked Style
. Aside from masking, this is
identical to Style::default()
.
An item with masked styling is not written out when painting is
disabled during Display
or Debug
invocations. When painting is
enabled, masking has no effect.
use yansi::Style;
let plain = Style::masked();
assert_eq!(plain, Style::default());
Constructs a new Style
structure with the foreground color set to black.
use yansi::Style;
let black = Style::black();
println!("This is going to be black: {}", black.paint("yay!"));
Constructs a new Style
structure with the foreground color set to red.
use yansi::Style;
let red = Style::red();
println!("This is going to be red: {}", red.paint("yay!"));
Constructs a new Style
structure with the foreground color set to green.
use yansi::Style;
let green = Style::green();
println!("This is going to be green: {}", green.paint("yay!"));
Constructs a new Style
structure with the foreground color set to yellow.
use yansi::Style;
let yellow = Style::yellow();
println!("This is going to be yellow: {}", yellow.paint("yay!"));
Constructs a new Style
structure with the foreground color set to blue.
use yansi::Style;
let blue = Style::blue();
println!("This is going to be blue: {}", blue.paint("yay!"));
Constructs a new Style
structure with the foreground color set to purple.
use yansi::Style;
let purple = Style::purple();
println!("This is going to be purple: {}", purple.paint("yay!"));
Constructs a new Style
structure with the foreground color set to cyan.
use yansi::Style;
let cyan = Style::cyan();
println!("This is going to be cyan: {}", cyan.paint("yay!"));
Constructs a new Style
structure with the foreground color set to white.
use yansi::Style;
let white = Style::white();
println!("This is going to be white: {}", white.paint("yay!"));
Returns the foreground color of self
.
use yansi::{Style, Color};
let plain = Style::new();
assert_eq!(plain.fg_color(), Color::Unset);
let red = plain.fg(Color::Red);
assert_eq!(red.fg_color(), Color::Red);
Returns the foreground color of self
.
use yansi::{Style, Color};
let plain = Style::new();
assert_eq!(plain.bg_color(), Color::Unset);
let white = plain.bg(Color::White);
assert_eq!(white.bg_color(), Color::White);
Returns true
if self
is masked.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_masked());
let masked = plain.mask();
assert!(masked.is_masked());
Returns true
if the bold property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_bold());
let styled = plain.bold();
assert!(styled.is_bold());
Returns true
if the dimmed property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_dimmed());
let styled = plain.dimmed();
assert!(styled.is_dimmed());
Returns true
if the italic property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_italic());
let styled = plain.italic();
assert!(styled.is_italic());
Returns true
if the underline property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_underline());
let styled = plain.underline();
assert!(styled.is_underline());
Returns true
if the blink property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_blink());
let styled = plain.blink();
assert!(styled.is_blink());
Returns true
if the invert property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_invert());
let styled = plain.invert();
assert!(styled.is_invert());
Returns true
if the hidden property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_hidden());
let styled = plain.hidden();
assert!(styled.is_hidden());
Returns true
if the strikethrough property is set on self
.
use yansi::Style;
let plain = Style::new();
assert!(!plain.is_strikethrough());
let styled = plain.strikethrough();
assert!(styled.is_strikethrough());
Sets the foreground to color
.
use yansi::{Color, Style};
let red_fg = Style::new().fg(Color::Red);
Sets the background to color
.
use yansi::{Color, Style};
let red_bg = Style::new().bg(Color::Red);
Sets self
to be masked.
An item with masked styling is not written out when painting is
disabled during Display
or Debug
invocations. When painting is
enabled, masking has no effect.
use yansi::Style;
let masked = Style::new().mask();
println!("{}Something happened.", masked.paint("Whoops! "));
Enables the bold style on self
.
use yansi::Paint;
println!("Using bold: {}", Paint::new("hi").bold());
Enables the dimmed style on self
.
use yansi::Paint;
println!("Using dimmed: {}", Paint::new("hi").dimmed());
Enables the italic style on self
.
use yansi::Paint;
println!("Using italic: {}", Paint::new("hi").italic());
Enables the underline style on self
.
use yansi::Paint;
println!("Using underline: {}", Paint::new("hi").underline());
Enables the blink style on self
.
use yansi::Paint;
println!("Using blink: {}", Paint::new("hi").blink());
Enables the invert style on self
.
use yansi::Paint;
println!("Using invert: {}", Paint::new("hi").invert());
Enables the hidden style on self
.
use yansi::Paint;
println!("Using hidden: {}", Paint::new("hi").hidden());
Enables the strikethrough style on self
.
use yansi::Paint;
println!("Using strikethrough: {}", Paint::new("hi").strikethrough());
Constructs a new Paint
structure that encapsulates item
with the
style set to self
.
use yansi::{Style, Color};
let alert = Style::red().bold().underline();
println!("Alert: {}", alert.paint("This thing happened!"));
Writes the ANSI code prefix for the currently set styles.
This method is intended to be used inside of fmt::Display
and
fmt::Debug
implementations for custom or specialized use-cases. Most
users should use Paint
for all painting needs.
use std::fmt;
use yansi::Style;
struct CustomItem {
item: u32,
style: Style
}
impl fmt::Display for CustomItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.style.fmt_prefix(f)?;
write!(f, "number: {}", self.item)?;
self.style.fmt_suffix(f)
}
}
Writes the ANSI code suffix for the currently set styles.
This method is intended to be used inside of fmt::Display
and
fmt::Debug
implementations for custom or specialized use-cases. Most
users should use Paint
for all painting needs.
use std::fmt;
use yansi::Style;
struct CustomItem {
item: u32,
style: Style
}
impl fmt::Display for CustomItem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.style.fmt_prefix(f)?;
write!(f, "number: {}", self.item)?;
self.style.fmt_suffix(f)
}
}