Struct crossterm::StyledObject

source ·
pub struct StyledObject<D: Display> {
    pub object_style: ObjectStyle,
    pub content: D,
}
Expand description

Struct that contains both the style and the content wits can be styled.

Fields§

§object_style: ObjectStyle§content: D

Implementations§

Set the foreground of the styled object to the passed Color.

use self::crossterm::style::{style,Color};

// create an styled object with the foreground color red.
let styledobject =  style("Some colored text").with(Color::Red);
// create an styled object with the foreground color blue.
let styledobject1 = style("Some colored text").with(Color::Blue);

// print the styledobject to see the result
println!("{}", styledobject);
println!("{}", styledobject1);

// print an styled object directly.
println!("{}", style("Some colored text").on(Color::Blue));

Set the background of the styled object to the passed Color.

#Example

use self::crossterm::style::{style,Color};

// create an styled object with the background color red.
let styledobject =  style("Some colored text").on(Color::Red);
// create an styled object with the foreground color blue.
let styledobject1 = style("Some colored text").on(Color::Blue);

// print the styledobject to see the result
println!("{}", styledobject);
println!("{}", styledobject1);

// print an styled object directly.
println!("{}", style("Some colored text").on(Color::Blue));

Set the attribute of an styled object to the passed Attribute.

#Example

extern crate crossterm;
use self::crossterm::style::{style,Attribute};

println!("{}", style("Some bold text").attr(Attribute::Bold);

Increase the font intensity.

Faint (decreased intensity) (Not widely supported).

Make the font italic (Not widely supported; Sometimes treated as inverse).

Underline font.

Slow Blink (less than 150 per minute; not widely supported).

Rapid Blink (MS-DOS ANSI.SYS; 150+ per minute; not widely supported).

Swap foreground and background colors.

Hide text (Not widely supported).

Characters legible, but marked for deletion. Not widely supported.

This could be used to paint the styled object onto the given screen. You have to pass a reference to the screen whereon you want to perform the painting.

style("Some colored text")
    .with(Color::Blue)
    .on(Color::Black)
    .paint(&screen);

You should take not that StyledObject implements Display. You don’t need to call paint unless you are on alternate screen. Checkout into_displayable() for more information about this.

This converts an styled object into an DisplayableObject witch implements: Display and could be used inside the write function of the standard library.

StyledObject already implements Display right?

This is true, however there are some complex issues why this won’t work on alternate screen. That is the reason why this functions exists. You could just pass in the ‘screen’ from your alternate screen to this method and your StyledObject will be printed to the alternate screen just fine.

   let screen = Screen::default(); /* represents the alternate screen */
   let styled_object = style("test").with(Color::Yellow);
   let display_object = styled_object.into_displayable(&screen);
   println!("Colored text: {}. Default color", display_object);

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.