Struct Styled

Source
pub struct Styled<'a, T: Display + ?Sized> { /* private fields */ }
Expand description

Styled “string”-like object

Can be used instead of raw ANSI sequences because of cleaner interface.

This type is a wrapper around any object which implements Display trait — usually some kind of strings or other simple ogjects. It wraps one style change at a time, making it possible to chain styles to apply them from the innermost to the outermost.

All the magic happens in Style trait.

use statusline::Style;

let hello = "Hello world!";
let styled = hello.boxed().red().bold().with_reset().to_string();
//                ^^^^^^^^------=======.............
assert_eq!("\x1b[1m\x1b[31m[Hello world!]\x1b[0m", styled);
//          =======--------^            ^.......

This wrapper applies styles only when formatted to string. Formatting results are never saved and every call to <Styled as ToString>::to_string() will format the result one more time, which may lead to different results for special types like the one which tells the exact moment of time at the std::fmt::Display::fmt call.

Trait Implementations§

Source§

impl<T: Display + ?Sized> Display for Styled<'_, T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Styled<'a, T>
where T: ?Sized,

§

impl<'a, T> RefUnwindSafe for Styled<'a, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'a, T> Send for Styled<'a, T>
where T: Sync + ?Sized,

§

impl<'a, T> Sync for Styled<'a, T>
where T: Sync + ?Sized,

§

impl<'a, T> Unpin for Styled<'a, T>
where T: ?Sized,

§

impl<'a, T> UnwindSafe for Styled<'a, T>
where T: RefUnwindSafe + ?Sized,

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

Source§

fn as_title(&self) -> Styled<'_, Self>

Format as a title for terminal Read more
Source§

fn bold(&self) -> Styled<'_, Self>

Prepend bold style. Colors from 16-color palette may shift a bit Read more
Source§

fn italic(&self) -> Styled<'_, Self>

Prepend italic style Read more
Source§

fn low_color(&self, index: usize) -> Styled<'_, Self>

Use colors from 16-color palette, dark version (0 for CSI 31 thru 6 for CSI 37, CSI 30 is black which is useless)
Source§

fn true_color(&self, red: u8, green: u8, blue: u8) -> Styled<'_, Self>

Use true color. Note that some terminals lack true color support and will approximate the result with colors they do support. This may lead to text being completely unreadable. Read more
Source§

fn invisible(&self) -> Styled<'_, Self>

Wrap into “readline invisible” characters, for PS1 output or some other strange things Read more
Source§

fn visible(&self) -> Styled<'_, Self>

Wrap into “readline invisible” but reverse — for making surroundings invisible. Read more
Source§

fn with_reset(&self) -> Styled<'_, Self>

Add “reset colors and boldness” to the end Read more
Source§

fn boxed(&self) -> Styled<'_, Self>

Wrap into square brackets Read more
Source§

fn rounded(&self) -> Styled<'_, Self>

Wrap into round brackets Read more
Source§

fn horizontal_absolute(&self, pos: usize) -> Styled<'_, Self>

Set cursor position, the horizontal part, with absolute value. Coordinates are counted from 1, from line start to line end, which may seem counter-intuitive
Source§

fn prev_line(&self, count: i32) -> Styled<'_, Self>

Move cursor to the beginning of line which is count lines above the current one
Source§

fn save_restore(&self) -> Styled<'_, Self>

Wrap into cursor saver — for example for outputting PS1 above the PS1 “line”
Source§

fn clear_till_end(&self) -> Styled<'_, Self>

Prepends line cleaner
Source§

fn join_lf(&self, s: String) -> Styled<'_, Self>

Join current line with fixed one with newline
Source§

fn red(&self) -> Styled<'_, Self>

Red color from 16-color palette (CSI 31)
Source§

fn green(&self) -> Styled<'_, Self>

Green color from 16-color palette (CSI 32)
Source§

fn yellow(&self) -> Styled<'_, Self>

Yellow color from 16-color palette (CSI 33)
Source§

fn blue(&self) -> Styled<'_, Self>

Blue color from 16-color palette (CSI 34)
Source§

fn purple(&self) -> Styled<'_, Self>

Purple color from 16-color palette (CSI 35)
Source§

fn cyan(&self) -> Styled<'_, Self>

Cyan color from 16-color palette (CSI 36)
Source§

fn light_gray(&self) -> Styled<'_, Self>

Light gray color from 16-color palette (CSI 37)
Source§

fn pink(&self) -> Styled<'_, Self>

Pink color (true)
Source§

fn light_green(&self) -> Styled<'_, Self>

Light green color (true)
Source§

fn light_red(&self) -> Styled<'_, Self>

Light red color (true)
Source§

fn gray(&self) -> Styled<'_, Self>

Gray color (true)
Source§

fn colorize_with(&self, with: &str) -> Styled<'_, Self>

String autocolorizer. 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.