added paint to crossterm type. And changed some methods names

This commit is contained in:
TimonPost 2018-07-29 23:14:28 +02:00
parent fc61ef778f
commit bf0f330b8f
4 changed files with 41 additions and 48 deletions

View File

@ -9,13 +9,16 @@ use super::super::input;
use super::super::terminal; use super::super::terminal;
use super::super::style; use super::super::style;
use std::fmt::{Display};
use std::io::Write;
use std::io::Result; use std::io::Result;
pub struct Crossterm { pub struct Crossterm {
raw_mode: bool,
alternate_mode: bool,
pub active_screen: manager::ScreenManager, pub active_screen: manager::ScreenManager,
raw_terminal: Option<Box<IRawScreenCommand>>, raw_terminal: Option<Box<IRawScreenCommand>>,
// Would be cool to figure out a way to have multiple screens instead of just only the main and alternate screen.
// For windows this would be easy but for unix I have no idea.
alternate_screen: Option<Box<IAlternateScreenCommand>> alternate_screen: Option<Box<IAlternateScreenCommand>>
} }
@ -25,8 +28,6 @@ impl<'crossterm> Crossterm
{ {
Crossterm Crossterm
{ {
raw_mode: false,
alternate_mode: false,
active_screen: manager::ScreenManager::new(), active_screen: manager::ScreenManager::new(),
raw_terminal: None, raw_terminal: None,
alternate_screen: None, alternate_screen: None,
@ -42,7 +43,7 @@ impl<'crossterm> Crossterm
}, },
Some(ref mut raw_terminal) => { Some(ref mut raw_terminal) => {
raw_terminal.enable()?; raw_terminal.enable()?;
self.raw_mode = true; self.active_screen.set_is_raw_screen(true);
}, },
} }
@ -59,41 +60,41 @@ impl<'crossterm> Crossterm
}, },
Some(ref mut raw_terminal) => { Some(ref mut raw_terminal) => {
raw_terminal.disable()?; raw_terminal.disable()?;
self.raw_mode = false; self.active_screen.set_is_raw_screen(false);
}, },
} }
return Ok(()) return Ok(())
} }
pub fn enable_alternate_screen(&mut self) -> Result<()> pub fn to_alternate_screen(&mut self) -> Result<()>
{ {
match self.alternate_screen match self.alternate_screen
{ {
None => { None => {
self.alternate_screen = Some(AlternateScreen::new()); self.alternate_screen = Some(AlternateScreen::new());
return self.enable_alternate_screen(); return self.to_alternate_screen();
}, },
Some(ref mut alternate_screen) => { Some(ref mut alternate_screen) => {
alternate_screen.enable(&mut self.active_screen)?; alternate_screen.enable(&mut self.active_screen)?;
self.alternate_mode = true; self.active_screen.set_is_alternate_screen(true);
}, },
} }
return Ok(()) return Ok(())
} }
pub fn disable_alternate_screen(&mut self) -> Result<()> pub fn to_main_screen(&mut self) -> Result<()>
{ {
match self.alternate_screen match self.alternate_screen
{ {
None => { None => {
self.alternate_screen = Some(AlternateScreen::new()); self.alternate_screen = Some(AlternateScreen::new());
return self.disable_alternate_screen(); return self.to_main_screen();
}, },
Some(ref mut alternate_screen) => { Some(ref mut alternate_screen) => {
alternate_screen.disable(&mut self.active_screen)?; alternate_screen.disable(&mut self.active_screen)?;
self.alternate_mode = false; self.active_screen.set_is_alternate_screen(false);
}, },
} }
@ -115,6 +116,28 @@ impl<'crossterm> Crossterm
pub fn color(&self) -> style::TerminalColor { pub fn color(&self) -> style::TerminalColor {
return style::TerminalColor::new(&self.active_screen) return style::TerminalColor::new(&self.active_screen)
} }
// Wraps an displayable object so it can be formatted with colors and attributes.
//
// Check `/examples/color` in the libary for more spesific examples.
//
pub fn paint<D>(&self, val: D) -> style::StyledObject<D>
where
D: Display,
{
style::ObjectStyle::new().apply_to(val, &self.active_screen)
}
}
impl Write for Crossterm
{
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.active_screen.write_buf(buf)
}
fn flush(&mut self) -> Result<()> {
self.active_screen.flush()
}
} }
impl Drop for Crossterm impl Drop for Crossterm
@ -133,37 +156,3 @@ impl Drop for Crossterm
// Wraps an displayable object so it can be formatted with colors and attributes.
//
// Check `/examples/color` in the libary for more spesific examples.
//
// #Example
//
// ```rust
// extern crate crossterm;
//
// use self::crossterm::style::{paint,Color};
//
// fn main()
// {
// // Create an styledobject object from the text 'Unstyled font'
// // Currently it has the default foregroundcolor and backgroundcolor.
// println!("{}",paint("Unstyled font"));
//
// // Create an displayable object from the text 'Colored font',
// // Paint this with the `Red` foreground color and `Blue` backgroundcolor.
// // Print the result.
// let styledobject = paint("Colored font").with(Color::Red).on(Color::Blue);
// println!("{}", styledobject);
//
// // Or all in one line
// println!("{}", paint("Colored font").with(Color::Red).on(Color::Blue));
// }
//
// ```
// pub fn paint<D>(&self, val: D) -> style::StyledObject<D>
// where
// D: fmt::Display,
// {
// style::ObjectStyle::new().apply_to(val, self.context.clone())
// }

View File

@ -85,6 +85,10 @@ impl ScreenManager {
self.screen_manager.write_str(string) self.screen_manager.write_str(string)
} }
pub fn write_buf(&self, buf: &[u8]) -> io::Result<usize> {
self.screen_manager.write(buf)
}
/// Can be used to get an reference to an specific implementation used for the current platform. /// Can be used to get an reference to an specific implementation used for the current platform.
pub fn as_any(&self) -> &Any { pub fn as_any(&self) -> &Any {
self.screen_manager.as_any() self.screen_manager.as_any()

View File

@ -30,7 +30,7 @@ impl Default for ObjectStyle {
impl ObjectStyle { impl ObjectStyle {
/// Apply an `StyledObject` to the passed displayable object. /// Apply an `StyledObject` to the passed displayable object.
pub fn apply_to<'style, D: Display>(&self, val: D, screen_manager: &'style mut ScreenManager) -> StyledObject<'style,D> { pub fn apply_to<'style, D: Display>(&self, val: D, screen_manager: &'style ScreenManager) -> StyledObject<'style,D> {
StyledObject { StyledObject {
object_style: self.clone(), object_style: self.clone(),
screen_manager: screen_manager, screen_manager: screen_manager,

View File

@ -15,7 +15,7 @@ use super::super::super::manager::WinApiScreenManager;
pub struct StyledObject<'terminal, D: Display> { pub struct StyledObject<'terminal, D: Display> {
pub object_style: ObjectStyle, pub object_style: ObjectStyle,
pub content: D, pub content: D,
pub screen_manager: &'terminal mut ScreenManager, pub screen_manager: &'terminal ScreenManager,
} }
impl<'terminal,D: Display> StyledObject<'terminal,D> { impl<'terminal,D: Display> StyledObject<'terminal,D> {