Struct crossterm::style::styledobject::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§
source§impl<'a, D: Display + 'a> StyledObject<D>
impl<'a, D: Display + 'a> StyledObject<D>
sourcepub fn with(self, foreground_color: Color) -> StyledObject<D>
pub fn with(self, foreground_color: Color) -> StyledObject<D>
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));
sourcepub fn on(self, background_color: Color) -> StyledObject<D>
pub fn on(self, background_color: Color) -> StyledObject<D>
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));
sourcepub fn attr(self, attr: Attribute) -> StyledObject<D>
pub fn attr(self, attr: Attribute) -> StyledObject<D>
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);
sourcepub fn bold(self) -> StyledObject<D>
pub fn bold(self) -> StyledObject<D>
Increase the font intensity.
sourcepub fn dim(self) -> StyledObject<D>
pub fn dim(self) -> StyledObject<D>
Faint (decreased intensity) (Not widely supported).
sourcepub fn italic(self) -> StyledObject<D>
pub fn italic(self) -> StyledObject<D>
Make the font italic (Not widely supported; Sometimes treated as inverse).
sourcepub fn underlined(self) -> StyledObject<D>
pub fn underlined(self) -> StyledObject<D>
Underline font.
sourcepub fn slow_blink(self) -> StyledObject<D>
pub fn slow_blink(self) -> StyledObject<D>
Slow Blink (less than 150 per minute; not widely supported).
sourcepub fn rapid_blink(self) -> StyledObject<D>
pub fn rapid_blink(self) -> StyledObject<D>
Rapid Blink (MS-DOS ANSI.SYS; 150+ per minute; not widely supported).
sourcepub fn reverse(self) -> StyledObject<D>
pub fn reverse(self) -> StyledObject<D>
Swap foreground and background colors.
Hide text (Not widely supported).
sourcepub fn crossed_out(self) -> StyledObject<D>
pub fn crossed_out(self) -> StyledObject<D>
Characters legible, but marked for deletion. Not widely supported.
sourcepub fn paint(&self, screen: &Screen)
pub fn paint(&self, screen: &Screen)
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.
sourcepub fn into_displayable(self, screen: &'a Screen) -> DisplayableObject<'a, D>
pub fn into_displayable(self, screen: &'a Screen) -> DisplayableObject<'a, D>
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);