From a9f47ecebce7475aa42b3a1857a666032a2e0d52 Mon Sep 17 00:00:00 2001 From: TimonPost Date: Fri, 21 Sep 2018 21:36:25 +0200 Subject: [PATCH] added tests and method to convert styledobject into displayableobject --- src/modules/cursor/mod.rs | 1 + src/modules/cursor/test.rs | 88 +++++++++++++++++++++++++++++++ src/modules/output/mod.rs | 1 + src/modules/output/test.rs | 64 ++++++++++++++++++++++ src/modules/style/mod.rs | 4 +- src/modules/style/styledobject.rs | 32 +++++++---- src/modules/style/test.rs | 46 ++++++++++++++++ 7 files changed, 225 insertions(+), 11 deletions(-) diff --git a/src/modules/cursor/mod.rs b/src/modules/cursor/mod.rs index 99f0ca7..0296a49 100644 --- a/src/modules/cursor/mod.rs +++ b/src/modules/cursor/mod.rs @@ -2,6 +2,7 @@ //! Like moving the cursor position;saving and resetting the cursor position; hiding showing and control the blinking of the cursor. mod cursor; +mod test; mod ansi_cursor; #[cfg(target_os = "windows")] diff --git a/src/modules/cursor/test.rs b/src/modules/cursor/test.rs index e69de29..d829912 100644 --- a/src/modules/cursor/test.rs +++ b/src/modules/cursor/test.rs @@ -0,0 +1,88 @@ +use modules::cursor::winapi_cursor::WinApiCursor; +use modules::cursor::ansi_cursor::AnsiCursor; + +use modules::cursor::ITerminalCursor; + +use Screen; + +/* ======================== WinApi =========================== */ +#[test] +fn goto_winapi() +{ + let screen = Screen::default(); + let cursor = WinApiCursor::new(); + + cursor.goto(5,5,&screen.stdout); + let (x,y) = cursor.pos(&screen.stdout); + + assert_eq!(x, 5); + assert_eq!(y, 5); +} + +#[test] +fn reset_safe_winapi() +{ + let screen = Screen::default(); + let cursor = WinApiCursor::new(); + let (x,y) = cursor.pos(&screen.stdout); + + cursor.save_position(&screen.stdout); + cursor.goto(5,5,&screen.stdout); + cursor.reset_position(&screen.stdout); + + let (x_saved,y_saved) = cursor.pos(&screen.stdout); + + assert_eq!(x, x_saved); + assert_eq!(y, y_saved); +} + + + +/* ======================== ANSI =========================== */ +#[test] +fn reset_safe_ansi() +{ + if try_enable_ansi() { + let screen = Screen::default(); + let cursor = AnsiCursor::new(); + let (x, y) = cursor.pos(&screen.stdout); + + cursor.save_position(&screen.stdout); + cursor.goto(5, 5,&screen.stdout); + cursor.reset_position(&screen.stdout); + + let (x_saved, y_saved) = cursor.pos(&screen.stdout); + + assert_eq!(x, x_saved); + assert_eq!(y, y_saved); + } +} + +#[test] +fn goto_ansi() +{ + if try_enable_ansi() { + let screen = Screen::default(); + let cursor = AnsiCursor::new(); + + cursor.goto(5, 5, &screen.stdout); + let (x, y) = cursor.pos(&screen.stdout); + + assert_eq!(x, 5); + assert_eq!(y, 5); + } +} + + +fn try_enable_ansi() -> bool +{ + if cfg!(target_os = "windows") { + #[cfg(windows)] + use kernel::windows_kernel::ansi_support::try_enable_ansi_support; + + if !try_enable_ansi_support() + { return false; } + } + + return true; +} diff --git a/src/modules/output/mod.rs b/src/modules/output/mod.rs index 9a8d7b2..6f9d096 100644 --- a/src/modules/output/mod.rs +++ b/src/modules/output/mod.rs @@ -1,6 +1,7 @@ //! This module provides a way to work with an handle to an screen on different platforms. mod output; +mod test; mod ansi_output; #[cfg(target_os = "windows")] diff --git a/src/modules/output/test.rs b/src/modules/output/test.rs index e69de29..40b0740 100644 --- a/src/modules/output/test.rs +++ b/src/modules/output/test.rs @@ -0,0 +1,64 @@ +use modules::output::winapi_output::WinApiOutput; +use modules::output::ansi_output::AnsiOutput; + +use modules::output::IStdout; + +use Screen; + +/* ======================== WinApi =========================== */ +#[test] +fn write_winapi() +{ + let screen = Screen::default(); + let output = WinApiOutput::new(); + + let bytes = "test".as_bytes(); + let result = output.write(bytes); + is_valid_write(result, bytes.len()); +} + + +/* ======================== ANSI =========================== */ +#[test] +fn write_ansi() +{ + let screen = Screen::default(); + let output = AnsiOutput::new(); + + let bytes = "test".as_bytes(); + let result = output.write(bytes); + println!("bytes: {} written: {}", bytes.len(), result.unwrap()); + is_valid_write(result, bytes.len()); +} + +fn write_str_ansi() +{ + let screen = Screen::default(); + let output = AnsiOutput::new(); + + let bytes = "test".as_bytes(); + let result = output.write_str("test"); + is_valid_write(result, bytes.len()); +} + + +fn is_valid_write(result: ::std::io::Result, str_length: usize) +{ + match result { + Err(_) => assert!(false), + Ok(length) => if str_length == length { assert!(true) } else { assert!(false) } + }; +} + +fn try_enable_ansi() -> bool +{ + if cfg!(target_os = "windows") { + #[cfg(windows)] + use kernel::windows_kernel::ansi_support::try_enable_ansi_support; + + if !try_enable_ansi_support() + { return false; } + } + + return true; +} diff --git a/src/modules/style/mod.rs b/src/modules/style/mod.rs index 626be9a..82d1a53 100644 --- a/src/modules/style/mod.rs +++ b/src/modules/style/mod.rs @@ -1,5 +1,6 @@ //! Module that contains all the actions related to the styling of the terminal. like coloring adding attributes etc. +mod test; pub mod color; pub mod objectstyle; pub mod styledobject; @@ -22,6 +23,7 @@ pub use self::objectstyle::ObjectStyle; pub use self::styledobject::StyledObject; pub use self::styledobject::DisplayableObject; use super::functions; + use TerminalOutput; /// This trait defines the actions that can be preformed with the terminal color. @@ -61,7 +63,7 @@ trait ITerminalColor { /// styled_object.paint(&screen); /// } /// ``` -pub fn style(val: D) -> StyledObject +pub fn style<'a,D: 'a>(val: D) -> StyledObject where D: Display, { ObjectStyle::new().apply_to(val) diff --git a/src/modules/style/styledobject.rs b/src/modules/style/styledobject.rs index 1921690..23396da 100644 --- a/src/modules/style/styledobject.rs +++ b/src/modules/style/styledobject.rs @@ -14,7 +14,7 @@ pub struct StyledObject { pub content: D, } -impl<'a, D: Display> StyledObject { +impl<'a, D: Display + 'a> StyledObject { /// Set the foreground of the styled object to the passed `Color` /// /// ```rust @@ -45,7 +45,7 @@ impl<'a, D: Display> StyledObject { /// #Example /// /// ```rust - /// use self::crossterm::style::{style,Color}; + /// use self::crossterm::style::{style,Color}; /// /// // create an styled object with the background color red. /// let styledobject = style("Some colored text").on(Color::Blue); @@ -72,12 +72,10 @@ impl<'a, D: Display> StyledObject { /// #Example /// /// ```rust - /// /// extern crate crossterm; /// use self::crossterm::style::{style,Attribute}; /// /// style("Some colored text").attr(Attribute::Bold).paint(&screen); - /// /// ``` #[cfg(unix)] pub fn attr(mut self, attr: Attribute) -> StyledObject { @@ -180,23 +178,37 @@ impl<'a, D: Display> StyledObject { } } -// pub fn get_displayable(&self, screen: &'a Screen) -> DisplayableObject<'a, D> -// { -// return DisplayableObject::new(screen, &self) -// } + /// 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) + } } use std::fmt::{Formatter, Error}; +/// 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)) +/// ``` pub struct DisplayableObject<'a, D:Display + 'a> { - styled_object: &'a StyledObject, + styled_object: StyledObject, screen: &'a Screen, } impl <'a, D: Display + 'a> DisplayableObject<'a, D> { - pub fn new(screen: &'a Screen, styled_object: &'a StyledObject) -> DisplayableObject<'a, D> + pub fn new(screen: &'a Screen, styled_object: StyledObject) -> DisplayableObject<'a, D> { DisplayableObject { screen, styled_object } } diff --git a/src/modules/style/test.rs b/src/modules/style/test.rs index e69de29..1d256d7 100644 --- a/src/modules/style/test.rs +++ b/src/modules/style/test.rs @@ -0,0 +1,46 @@ +//use modules::style::winapi_color::WinApiColor; +//use modules::style::ansi_color::AnsiColor; +// +//use modules::style::ITerminalColor; +// +//use Screen; +// +//* ======================== WinApi =========================== */ +//#[test] +//fn goto_winapi() +//{ +// let screen = Screen::default(); +// let color = WinApiColor::new(); +// +// assert_eq!(x, 5); +// assert_eq!(y, 5); +//} +// +// +// +//* ======================== ANSI =========================== */ +//#[test] +//fn reset_safe_ansi() +//{ +// if try_enable_ansi() { +// let screen = Screen::default(); +// let cursor = AnsiColor::new(); +// +// assert_eq!(x, x_saved); +// assert_eq!(y, y_saved); +// } +//} +// +// +//fn try_enable_ansi() -> bool +//{ +// if cfg!(target_os = "windows") { +// #[cfg(windows)] +// use kernel::windows_kernel::ansi_support::try_enable_ansi_support; +// +// if !try_enable_ansi_support() +// { return false; } +// } +// +// return true; +//}