2018-06-17 04:10:51 +10:00
//! This module contains the logic to style an object that contains some state witch can be styled.
2018-07-30 05:30:09 +10:00
2018-11-15 02:53:27 +11:00
use super ::{ color , from_screen , Color , ObjectStyle } ;
2018-08-12 01:58:15 +10:00
use Screen ;
2018-06-23 05:04:30 +10:00
2018-11-15 02:53:27 +11:00
use std ::fmt ::{ self , Display , Formatter } ;
use std ::io ::Write ;
2018-01-28 04:48:49 +11:00
2018-03-04 01:40:51 +11:00
#[ cfg(unix) ]
2018-07-29 03:46:05 +10:00
use super ::Attribute ;
2018-01-07 07:31:14 +11:00
2018-03-11 03:33:06 +11:00
/// Struct that contains both the style and the content wits can be styled.
2018-08-12 01:58:15 +10:00
pub struct StyledObject < D : Display > {
2018-01-04 00:43:54 +11:00
pub object_style : ObjectStyle ,
2018-01-07 07:31:14 +11:00
pub content : D ,
2018-01-04 00:43:54 +11:00
}
2018-09-22 05:36:25 +10:00
impl < ' a , D : Display + ' a > StyledObject < D > {
2018-11-15 02:53:27 +11:00
/// Set the foreground of the styled object to the passed `Color`.
2018-01-07 07:31:14 +11:00
///
2018-01-04 00:43:54 +11:00
/// ```rust
2018-08-14 05:04:07 +10:00
/// use self::crossterm::style::{style,Color};
2018-01-07 07:31:14 +11:00
///
2018-01-28 04:26:08 +11:00
/// // create an styled object with the foreground color red.
2018-11-15 02:53:27 +11:00
/// let styledobject = style("Some colored text").with(Color::Red);
2018-01-28 04:26:08 +11:00
/// // create an styled object with the foreground color blue.
2018-08-14 05:04:07 +10:00
/// let styledobject1 = style("Some colored text").with(Color::Blue);
///
2018-01-28 04:26:08 +11:00
/// // print the styledobject to see the result
2018-11-15 02:53:27 +11:00
/// println!("{}", styledobject);
/// println!("{}", styledobject1);
2018-08-14 05:04:07 +10:00
///
2018-01-28 04:26:08 +11:00
/// // print an styled object directly.
2018-11-15 02:53:27 +11:00
/// println!("{}", style("Some colored text").on(Color::Blue));
2018-01-04 00:43:54 +11:00
/// ```
2018-08-12 01:58:15 +10:00
pub fn with ( mut self , foreground_color : Color ) -> StyledObject < D > {
2018-01-04 00:43:54 +11:00
self . object_style = self . object_style . fg ( foreground_color ) ;
self
}
2018-11-15 02:53:27 +11:00
/// Set the background of the styled object to the passed `Color`.
2018-01-07 07:31:14 +11:00
///
/// #Example
///
2018-07-02 06:43:43 +10:00
/// ```rust
2018-09-22 05:36:25 +10:00
/// use self::crossterm::style::{style,Color};
2018-01-07 07:31:14 +11:00
///
2018-01-28 04:26:08 +11:00
/// // create an styled object with the background color red.
2018-11-15 02:53:27 +11:00
/// let styledobject = style("Some colored text").on(Color::Red);
2018-08-14 05:04:07 +10:00
/// // 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
2018-11-15 02:53:27 +11:00
/// println!("{}", styledobject);
/// println!("{}", styledobject1);
2018-01-28 04:26:08 +11:00
///
/// // print an styled object directly.
2018-11-15 02:53:27 +11:00
/// println!("{}", style("Some colored text").on(Color::Blue));
2018-01-04 00:43:54 +11:00
/// ```
2018-08-12 01:58:15 +10:00
pub fn on ( mut self , background_color : Color ) -> StyledObject < D > {
2018-01-04 00:43:54 +11:00
self . object_style = self . object_style . bg ( background_color ) ;
self
}
2018-01-26 04:26:08 +11:00
2018-11-15 02:53:27 +11:00
/// Set the attribute of an styled object to the passed `Attribute`.
2018-01-28 04:26:08 +11:00
///
/// #Example
///
/// ```rust
/// extern crate crossterm;
2018-08-14 05:04:07 +10:00
/// use self::crossterm::style::{style,Attribute};
2018-01-28 04:26:08 +11:00
///
2018-11-15 02:53:27 +11:00
/// println!("{}", style("Some bold text").attr(Attribute::Bold);
2018-01-28 04:26:08 +11:00
/// ```
#[ cfg(unix) ]
2018-08-12 01:58:15 +10:00
pub fn attr ( mut self , attr : Attribute ) -> StyledObject < D > {
2018-01-28 04:26:08 +11:00
& self . object_style . add_attr ( attr ) ;
2018-01-26 04:26:08 +11:00
self
}
2018-01-28 04:26:08 +11:00
/// Increase the font intensity.
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn bold ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::Bold )
}
2018-01-28 04:26:08 +11:00
/// Faint (decreased intensity) (Not widely supported).
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn dim ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::Dim )
}
2018-01-28 04:26:08 +11:00
/// Make the font italic (Not widely supported; Sometimes treated as inverse).
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn italic ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::Italic )
}
2018-01-28 04:26:08 +11:00
/// Underline font.
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn underlined ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::Underlined )
}
2018-01-28 04:26:08 +11:00
/// Slow Blink (less than 150 per minute; not widely supported).
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn slow_blink ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::SlowBlink )
}
2018-01-28 04:26:08 +11:00
/// Rapid Blink (MS-DOS ANSI.SYS; 150+ per minute; not widely supported).
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn rapid_blink ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::RapidBlink )
}
/// Swap foreground and background colors.
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn reverse ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::Reverse )
}
2018-01-28 04:26:08 +11:00
/// Hide text (Not widely supported).
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn hidden ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::Hidden )
}
2018-01-28 04:26:08 +11:00
/// Characters legible, but marked for deletion. Not widely supported.
2018-07-02 06:43:43 +10:00
#[ cfg(unix) ]
#[ inline(always) ]
2018-08-12 01:58:15 +10:00
pub fn crossed_out ( self ) -> StyledObject < D > {
2018-07-02 06:43:43 +10:00
self . attr ( Attribute ::CrossedOut )
}
2018-01-04 00:43:54 +11:00
2018-11-15 02:53:27 +11:00
/// 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.
2018-08-14 05:04:07 +10:00
///
/// ``` rust
/// style("Some colored text")
/// .with(Color::Blue)
/// .on(Color::Black)
/// .paint(&screen);
/// ```
2018-11-15 02:53:27 +11:00
///
/// 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.
2018-08-12 01:58:15 +10:00
pub fn paint ( & self , screen : & Screen )
{
2018-11-15 02:53:27 +11:00
let colored_terminal = from_screen ( & screen ) ;
2018-07-04 06:48:04 +10:00
let mut reset = true ;
if let Some ( bg ) = self . object_style . bg_color {
colored_terminal . set_bg ( bg ) ;
reset = true ;
}
if let Some ( fg ) = self . object_style . fg_color {
colored_terminal . set_fg ( fg ) ;
reset = true ;
}
#[ cfg(unix) ]
2018-11-15 02:53:27 +11:00
for attr in self . object_style . attrs . iter ( ) {
2018-08-13 06:41:08 +10:00
screen . stdout . write_string ( format! ( csi! ( " {}m " ) , * attr as i16 ) ) ;
2018-07-04 06:48:04 +10:00
reset = true ;
2018-01-07 07:31:14 +11:00
}
2018-01-04 00:43:54 +11:00
2018-07-29 03:26:35 +10:00
use std ::fmt ::Write ;
2018-08-12 01:58:15 +10:00
let mut content = String ::new ( ) ;
write! ( content , " {} " , self . content ) . unwrap ( ) ;
screen . stdout . write_string ( content ) ;
screen . stdout . flush ( ) ;
2018-07-04 06:48:04 +10:00
if reset {
colored_terminal . reset ( ) ;
}
}
2018-09-21 06:24:10 +10:00
2018-11-15 02:53:27 +11:00
/// 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.
2018-09-22 05:36:25 +10:00
///
/// ```
2018-11-15 02:53:27 +11:00
/// 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);
2018-09-22 05:36:25 +10:00
/// ```
pub fn into_displayable ( self , screen : & ' a Screen ) -> DisplayableObject < ' a , D >
{
return DisplayableObject ::new ( screen , self )
}
2018-09-21 06:24:10 +10:00
}
2018-11-15 02:53:27 +11:00
impl < D : Display > Display for StyledObject < D > {
fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , fmt ::Error > {
let mut colored_terminal = color ( ) ;
let mut reset = true ;
if let Some ( bg ) = self . object_style . bg_color {
colored_terminal . set_bg ( bg ) ;
reset = true ;
}
if let Some ( fg ) = self . object_style . fg_color {
colored_terminal . set_fg ( fg ) ;
reset = true ;
}
fmt ::Display ::fmt ( & self . content , f ) ? ;
std ::io ::stdout ( ) . flush ( ) . expect ( " Flush stdout failed " ) ;
if reset {
colored_terminal . reset ( ) ;
}
Ok ( ( ) )
}
}
2018-09-21 06:24:10 +10:00
2018-11-15 02:53:27 +11:00
/// This is a wrapper for a styled object on alternate screen so that the styled object could be printed on the alternate screen with the standard write functions in rust.
2018-09-22 05:36:25 +10:00
///
/// ```
/// write! ("some normal text, {} <- some colored text", DisplayableObject::new(&screen, styled_object));
2018-11-15 02:53:27 +11:00
/// println! ("some normal text, {} <- some colored text", DisplayableObject::new(&screen, styled_object));
2018-09-22 05:36:25 +10:00
/// ```
2018-09-21 06:24:10 +10:00
pub struct DisplayableObject < ' a , D :Display + ' a >
{
2018-09-22 05:36:25 +10:00
styled_object : StyledObject < D > ,
2018-09-21 06:24:10 +10:00
screen : & ' a Screen ,
}
impl < ' a , D : Display + ' a > DisplayableObject < ' a , D >
{
2018-09-22 05:36:25 +10:00
pub fn new ( screen : & ' a Screen , styled_object : StyledObject < D > ) -> DisplayableObject < ' a , D >
2018-09-21 06:24:10 +10:00
{
DisplayableObject { screen , styled_object }
}
}
impl < ' a , D : Display + ' a > Display for DisplayableObject < ' a , D >
{
2018-11-15 02:53:27 +11:00
fn fmt ( & self , _f : & mut Formatter ) -> Result < ( ) , fmt ::Error > {
2018-09-21 06:24:10 +10:00
self . styled_object . paint ( & self . screen ) ;
return Ok ( ( ) )
}
2018-07-04 06:48:04 +10:00
}