TerminalStyle

Struct TerminalStyle 

Source
pub struct TerminalStyle { /* private fields */ }
Expand description

Stores a custom style for terminal text. This creates the escape command needed to change terminal text color. ANSI Escape Codes

§Examples

use terminal_text_styler::{TerminalStyle, SGREffect, ANSIForegroundColor};

// Manual with codes
let yellow_manual = TerminalStyle::from(vec![0, 93]);

// Using enums
let yellow = TerminalStyle::new(vec![SGREffect::Normal], Some(ANSIForegroundColor::BrightYellow), None);
let no_color = TerminalStyle::new_empty();
assert_eq!(yellow.command(), "\u{001B}[0;93m");
assert_eq!(no_color.command(), "\u{001B}[0m");
assert_eq!(yellow.wrap("Hello, World!"), "\u{001B}[0;93mHello, World!\u{001B}[0m");

Implementations§

Source§

impl TerminalStyle

Source

pub fn codes(&self) -> &Vec<u8>

List of raw ANSI SGR escape codes used to build the style

Source

pub fn command(&self) -> &str

The built command string with escape code

Source

pub fn styles(&self) -> Vec<SGREffect>

Gets a list of all applied SGR effect styles

Source

pub fn foreground(&self) -> Option<ANSIForegroundColor>

Looks up the foreground color (Note: This is not supported for ANSI 256-colors and will return None)

Source

pub fn background(&self) -> Option<ANSIBackgroundColor>

Looks up the background color (Note: This is not supported for ANSI 256-colors and will return None)

Source

pub fn from(codes: Vec<u8>) -> Self

Creates a new terminal color with given escape codes.

Source

pub fn new_empty() -> Self

Creates a new empty terminal color (aka no color).

Source

pub fn new( effects: Vec<SGREffect>, foreground: Option<ANSIForegroundColor>, background: Option<ANSIBackgroundColor>, ) -> Self

Creates a new terminal color with given options.

Source

pub fn wrap(&self, text: &str) -> String

Wraps given text with a command to start the custom color at the beginning and ends the string with a no-color command.

§Examples
use terminal_text_styler::{TerminalStyle, SGREffect, ANSIForegroundColor};

let style = TerminalStyle::new(vec![SGREffect::Bold], Some(ANSIForegroundColor::BrightYellow), None);
let output = style.wrap("This will be bold and highlighted in bright yellow.");
println!("{}", output);
Source§

impl TerminalStyle

Source

pub fn no_color() -> Self

Source

pub fn reset() -> Self

Source

pub fn black() -> Self

Source

pub fn red() -> Self

Source

pub fn green() -> Self

Source

pub fn yellow() -> Self

Source

pub fn blue() -> Self

Source

pub fn magenta() -> Self

Source

pub fn cyan() -> Self

Source

pub fn white() -> Self

Source

pub fn bright_black() -> Self

Source

pub fn bright_red() -> Self

Source

pub fn bright_green() -> Self

Source

pub fn bright_yellow() -> Self

Source

pub fn bright_blue() -> Self

Source

pub fn bright_magenta() -> Self

Source

pub fn bright_cyan() -> Self

Source

pub fn bright_white() -> Self

Source

pub fn italic_black() -> Self

Source

pub fn italic_red() -> Self

Source

pub fn italic_green() -> Self

Source

pub fn italic_yellow() -> Self

Source

pub fn italic_blue() -> Self

Source

pub fn italic_magenta() -> Self

Source

pub fn italic_cyan() -> Self

Source

pub fn italic_white() -> Self

Source

pub fn red_background() -> Self

Source

pub fn blue_background() -> Self

Source

pub fn green_background() -> Self

Source

pub fn yellow_background() -> Self

Source

pub fn normal_bright_yellow() -> Self

Trait Implementations§

Source§

impl Clone for TerminalStyle

Source§

fn clone(&self) -> Self

Returns a duplicate 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 TerminalStyle

Source§

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

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

impl Display for TerminalStyle

Source§

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

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

impl PartialEq for TerminalStyle

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for TerminalStyle

Auto Trait Implementations§

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.