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-08-20 07:42:48 +10:00
use super ::{ Color , ObjectStyle } ;
2018-08-12 01:58:15 +10:00
use Screen ;
2018-06-23 05:04:30 +10:00
2018-08-20 07:42:48 +10:00
use std ::fmt ::Display ;
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-01-28 04:26:08 +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-08-14 05:04:07 +10:00
/// let styledobject = style("Some colored text").with(Color::Blue);
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);
///
/// let screen = Screen::default();
2018-01-28 04:26:08 +11:00
///
/// // print the styledobject to see the result
2018-08-14 05:04:07 +10:00
/// styledobject.paint(&screen);
/// styledobject1.paint(&screen);
///
2018-01-28 04:26:08 +11:00
/// // print an styled object directly.
2018-08-14 05:04:07 +10:00
/// style("Some colored text").with(Color::Blue).paint(&screen);
2018-07-02 06:43:43 +10:00
///
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-01-28 04:26:08 +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-08-14 05:04:07 +10:00
/// let styledobject = style("Some colored text").on(Color::Blue);
/// // create an styled object with the foreground color blue.
/// let styledobject1 = style("Some colored text").on(Color::Blue);
///
/// let screen = Screen::default();
///
/// // print the styledobject to see the result
/// styledobject.paint(&screen);
/// styledobject1.paint(&screen);
2018-01-28 04:26:08 +11:00
///
/// // print an styled object directly.
2018-08-14 05:04:07 +10:00
/// style("Some colored text").on(Color::Blue).paint(&screen);
2018-07-02 06:43:43 +10:00
///
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-01-28 04:26:08 +11:00
/// Set the attribute of an styled object to the passed `Attribute`
///
/// #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-08-14 05:04:07 +10:00
/// style("Some colored text").attr(Attribute::Bold).paint(&screen);
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-08-22 02:05:53 +10:00
/// This could be used to paint the styled object on the screen. 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-08-12 01:58:15 +10:00
pub fn paint ( & self , screen : & Screen )
{
2018-08-20 07:42:48 +10:00
let colored_terminal = ::color ( & 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-08-12 01:58:15 +10: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-09-22 05:36:25 +10:00
/// this converts an styled object into an `DisplayableObject` witch implements: `Display` and could be used inside the write function of the standard library's.
///
/// ```
/// let screen = Screen::default();
// let styled_object = style("test").with(Color::Yellow);
// let display_object = styled_object.into_displayable(&screen);
// println!("Colored text: {}. Default color", display_object);
/// ```
pub fn into_displayable ( self , screen : & ' a Screen ) -> DisplayableObject < ' a , D >
{
return DisplayableObject ::new ( screen , self )
}
2018-09-21 06:24:10 +10:00
}
use std ::fmt ::{ Formatter , Error } ;
2018-09-22 05:36:25 +10:00
/// This is a wrapper for a styled object so that the styled object could be printed with the standard write functions in rust.
///
/// ```
/// write! ("some normal text, {} <- some colored text", DisplayableObject::new(&screen, styled_object));
/// println! ("some normal text, {} <- some colored text", DisplayableObject::new(&screen, styled_object))
/// ```
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 >
{
fn fmt ( & self , f : & mut Formatter ) -> Result < ( ) , Error > {
self . styled_object . paint ( & self . screen ) ;
return Ok ( ( ) )
}
2018-07-04 06:48:04 +10:00
}