Struct nu_ansi_term::Style

source ·
pub struct Style {
    pub foreground: Option<Color>,
    pub background: Option<Color>,
    pub is_bold: bool,
    pub is_dimmed: bool,
    pub is_italic: bool,
    pub is_underline: bool,
    pub is_blink: bool,
    pub is_reverse: bool,
    pub is_hidden: bool,
    pub is_strikethrough: bool,
    pub prefix_with_reset: bool,
}
Expand description

A style is a collection of properties that can format a string using ANSI escape codes.

§Examples

use nu_ansi_term::{Style, Color};

let style = Style::new().bold().on(Color::Black);
println!("{}", style.paint("Bold on black"));

Fields§

§foreground: Option<Color>

The style’s foreground color, if it has one.

§background: Option<Color>

The style’s background color, if it has one.

§is_bold: bool

Whether this style is bold.

§is_dimmed: bool

Whether this style is dimmed.

§is_italic: bool

Whether this style is italic.

§is_underline: bool

Whether this style is underlined.

§is_blink: bool

Whether this style is blinking.

§is_reverse: bool

Whether this style has reverse colors.

§is_hidden: bool

Whether this style is hidden.

§is_strikethrough: bool

Whether this style is struckthrough.

§prefix_with_reset: bool

Wether this style is always displayed starting with a reset code to clear any remaining style artifacts

Implementations§

source§

impl Style

source

pub const fn prefix(self) -> Prefix

The prefix bytes for this style. These are the bytes that tell the terminal to use a different color or font style.

§Examples
use nu_ansi_term::{Style, Color::Blue};

let style = Style::default().bold();
assert_eq!("\x1b[1m",
           style.prefix().to_string());

let style = Blue.bold();
assert_eq!("\x1b[1;34m",
           style.prefix().to_string());

let style = Style::default();
assert_eq!("",
           style.prefix().to_string());
§Examples with gnu_legacy feature enabled

Styles like bold, underlined, etc. are two-digit now

use nu_ansi_term::{Style, Color::Blue};

let style = Style::default().bold();
assert_eq!("\x1b[01m",
           style.prefix().to_string());

let style = Blue.bold();
assert_eq!("\x1b[01;34m",
           style.prefix().to_string());
source

pub const fn infix(self, next: Style) -> Infix

The infix bytes between this style and next style. These are the bytes that tell the terminal to change the style to next. These may include a reset followed by the next color and style, depending on the two styles.

§Examples
use nu_ansi_term::{Style, Color::Green};

let style = Style::default().bold();
assert_eq!("\x1b[32m",
           style.infix(Green.bold()).to_string());

let style = Green.normal();
assert_eq!("\x1b[1m",
           style.infix(Green.bold()).to_string());

let style = Style::default();
assert_eq!("",
           style.infix(style).to_string());
§Examples with gnu_legacy feature enabled

Styles like bold, underlined, etc. are two-digit now

use nu_ansi_term::Color::Green;

let style = Green.normal();
assert_eq!("\x1b[01m",
           style.infix(Green.bold()).to_string());
source

pub const fn suffix(self) -> Suffix

The suffix for this style. These are the bytes that tell the terminal to reset back to its normal color and font style.

§Examples
use nu_ansi_term::{Style, Color::Green};

let style = Style::default().bold();
assert_eq!("\x1b[0m",
           style.suffix().to_string());

let style = Green.normal().bold();
assert_eq!("\x1b[0m",
           style.suffix().to_string());

let style = Style::default();
assert_eq!("",
           style.suffix().to_string());
source§

impl Style

source

pub fn new() -> Style

Creates a new Style with no properties set.

§Examples
use nu_ansi_term::Style;

let style = Style::new();
println!("{}", style.paint("hi"));
source

pub const fn reset_before_style(&self) -> Style

Returns a Style with the Style.prefix_with_reset property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().reset_before_style();
println!("{}", style.paint("hey"));
source

pub const fn bold(&self) -> Style

Returns a Style with the bold property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().bold();
println!("{}", style.paint("hey"));
source

pub const fn dimmed(&self) -> Style

Returns a Style with the dimmed property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().dimmed();
println!("{}", style.paint("sup"));
source

pub const fn italic(&self) -> Style

Returns a Style with the italic property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().italic();
println!("{}", style.paint("greetings"));
source

pub const fn underline(&self) -> Style

Returns a Style with the underline property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().underline();
println!("{}", style.paint("salutations"));

Returns a Style with the blink property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().blink();
println!("{}", style.paint("wazzup"));
source

pub const fn reverse(&self) -> Style

Returns a Style with the reverse property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().reverse();
println!("{}", style.paint("aloha"));
source

pub const fn hidden(&self) -> Style

Returns a Style with the hidden property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().hidden();
println!("{}", style.paint("ahoy"));
source

pub const fn strikethrough(&self) -> Style

Returns a Style with the strikethrough property set.

§Examples
use nu_ansi_term::Style;

let style = Style::new().strikethrough();
println!("{}", style.paint("yo"));
source

pub const fn fg(&self, foreground: Color) -> Style

Returns a Style with the foreground color property set.

§Examples
use nu_ansi_term::{Style, Color};

let style = Style::new().fg(Color::Yellow);
println!("{}", style.paint("hi"));
source

pub const fn on(&self, background: Color) -> Style

Returns a Style with the background color property set.

§Examples
use nu_ansi_term::{Style, Color};

let style = Style::new().on(Color::Blue);
println!("{}", style.paint("eyyyy"));
source

pub fn is_plain(self) -> bool

Return true if this Style has no actual styles, and can be written without any control characters.

§Examples
use nu_ansi_term::Style;

assert_eq!(true,  Style::default().is_plain());
assert_eq!(false, Style::default().bold().is_plain());
source§

impl Style

source

pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>( self, input: I, ) -> AnsiGenericString<'a, S>
where I: Into<Cow<'a, S>>, <S as ToOwned>::Owned: Debug,

Paints the given text with this color, returning an ANSI string.

Trait Implementations§

source§

impl Clone for Style

source§

fn clone(&self) -> Style

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Style

Styles have a special Debug implementation that only shows the fields that are set. Fields that haven’t been touched aren’t included in the output.

This behaviour gets bypassed when using the alternate formatting mode format!("{:#?}").

use nu_ansi_term::Color::{Red, Blue};
assert_eq!("Style { fg(Red), on(Blue), bold, italic }",
           format!("{:?}", Red.on(Blue).bold().italic()));
source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Style

source§

fn default() -> Style

Returns a style with no properties set. Formatting text using this style returns the exact same text.

use nu_ansi_term::Style;
assert_eq!(None,  Style::default().foreground);
assert_eq!(None,  Style::default().background);
assert_eq!(false, Style::default().is_bold);
assert_eq!("txt", Style::default().paint("txt").to_string());
source§

impl From<Color> for Style

source§

fn from(color: Color) -> Style

You can turn a Color into a Style with the foreground color set with the From trait.

use nu_ansi_term::{Style, Color};
let green_foreground = Style::default().fg(Color::Green);
assert_eq!(green_foreground, Color::Green.normal());
assert_eq!(green_foreground, Color::Green.into());
assert_eq!(green_foreground, Style::from(Color::Green));
source§

impl PartialEq for Style

source§

fn eq(&self, other: &Style) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Style

source§

impl Eq for Style

source§

impl StructuralPartialEq for Style

Auto Trait Implementations§

§

impl Freeze for Style

§

impl RefUnwindSafe for Style

§

impl Send for Style

§

impl Sync for Style

§

impl Unpin for Style

§

impl UnwindSafe for Style

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.