Trait Style

Source
pub trait Style: Display {
Show 27 methods // Provided methods fn as_title(&self) -> Styled<'_, Self> { ... } fn bold(&self) -> Styled<'_, Self> { ... } fn italic(&self) -> Styled<'_, Self> { ... } fn low_color(&self, index: usize) -> Styled<'_, Self> { ... } fn true_color(&self, red: u8, green: u8, blue: u8) -> Styled<'_, Self> { ... } fn invisible(&self) -> Styled<'_, Self> { ... } fn visible(&self) -> Styled<'_, Self> { ... } fn with_reset(&self) -> Styled<'_, Self> { ... } fn boxed(&self) -> Styled<'_, Self> { ... } fn rounded(&self) -> Styled<'_, Self> { ... } fn horizontal_absolute(&self, pos: usize) -> Styled<'_, Self> { ... } fn prev_line(&self, count: i32) -> Styled<'_, Self> { ... } fn save_restore(&self) -> Styled<'_, Self> { ... } fn clear_till_end(&self) -> Styled<'_, Self> { ... } fn join_lf(&self, s: String) -> Styled<'_, Self> { ... } fn red(&self) -> Styled<'_, Self> { ... } fn green(&self) -> Styled<'_, Self> { ... } fn yellow(&self) -> Styled<'_, Self> { ... } fn blue(&self) -> Styled<'_, Self> { ... } fn purple(&self) -> Styled<'_, Self> { ... } fn cyan(&self) -> Styled<'_, Self> { ... } fn light_gray(&self) -> Styled<'_, Self> { ... } fn pink(&self) -> Styled<'_, Self> { ... } fn light_green(&self) -> Styled<'_, Self> { ... } fn light_red(&self) -> Styled<'_, Self> { ... } fn gray(&self) -> Styled<'_, Self> { ... } fn colorize_with(&self, with: &str) -> Styled<'_, Self> { ... }
}
Expand description

Styling functions for function chaining

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);
//          =======--------^            ^.......

Every chained function call a new Styled object wraps the previous result like a cabbage.

Provided Methods§

Source

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

Format as a title for terminal

use statusline::Style;
assert_eq!("\x1b]0;yuki@reimu: /home/yuki\x07", "yuki@reimu: /home/yuki".as_title().to_string());
Source

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

Prepend bold style. Colors from 16-color palette may shift a bit

use statusline::Style;
assert_eq!("\x1b[1mBOLD text", "BOLD text".bold().to_string());
Source

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

Prepend italic style

use statusline::Style;
assert_eq!("\x1b[3mItalic text", "Italic text".italic().to_string());
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.

However, since most GUI terminal emulators in linux do support true color display no worry is usually needed. Just use it as-is

Source

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

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

use statusline::Style;
assert_eq!("\x01invis\x02", "invis".invisible().to_string());
Source

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

Wrap into “readline invisible” but reverse — for making surroundings invisible.

use statusline::Style;
assert_eq!("\x01\x1b[31m\x02Visible\x01\x1b[0m\x02",
    "Visible".visible().red().with_reset().invisible().to_string());
Source

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

Add “reset colors and boldness” to the end

use statusline::Style;
assert_eq!("\x1b[31mRED\x1b[0mnormal", "RED".red().with_reset().to_string() + "normal");
Source

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

Wrap into square brackets

use statusline::Style;
assert_eq!("[nya]", "nya".boxed().to_string());
Source

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

Wrap into round brackets

use statusline::Style;
assert_eq!("(nyaah~)", "nyaah~".rounded().to_string());
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.

Colors self with a “random” color associated with given string with.

with valueResulting color
="root"Red
otherSome non-red color

There are 24 different colors

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Display + ?Sized> Style for T

All types which can be displayed can be styled too