added tests and method to convert styledobject into displayableobject
This commit is contained in:
parent
3d7748ba3d
commit
a9f47ecebc
@ -2,6 +2,7 @@
|
|||||||
//! Like moving the cursor position;saving and resetting the cursor position; hiding showing and control the blinking of the cursor.
|
//! Like moving the cursor position;saving and resetting the cursor position; hiding showing and control the blinking of the cursor.
|
||||||
|
|
||||||
mod cursor;
|
mod cursor;
|
||||||
|
mod test;
|
||||||
|
|
||||||
mod ansi_cursor;
|
mod ansi_cursor;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
|
@ -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;
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
//! This module provides a way to work with an handle to an screen on different platforms.
|
//! This module provides a way to work with an handle to an screen on different platforms.
|
||||||
|
|
||||||
mod output;
|
mod output;
|
||||||
|
mod test;
|
||||||
|
|
||||||
mod ansi_output;
|
mod ansi_output;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
|
@ -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<usize>, 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;
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
//! Module that contains all the actions related to the styling of the terminal. like coloring adding attributes etc.
|
//! 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 color;
|
||||||
pub mod objectstyle;
|
pub mod objectstyle;
|
||||||
pub mod styledobject;
|
pub mod styledobject;
|
||||||
@ -22,6 +23,7 @@ pub use self::objectstyle::ObjectStyle;
|
|||||||
pub use self::styledobject::StyledObject;
|
pub use self::styledobject::StyledObject;
|
||||||
pub use self::styledobject::DisplayableObject;
|
pub use self::styledobject::DisplayableObject;
|
||||||
use super::functions;
|
use super::functions;
|
||||||
|
|
||||||
use TerminalOutput;
|
use TerminalOutput;
|
||||||
|
|
||||||
/// This trait defines the actions that can be preformed with the terminal color.
|
/// This trait defines the actions that can be preformed with the terminal color.
|
||||||
@ -61,7 +63,7 @@ trait ITerminalColor {
|
|||||||
/// styled_object.paint(&screen);
|
/// styled_object.paint(&screen);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn style<D>(val: D) -> StyledObject<D>
|
pub fn style<'a,D: 'a>(val: D) -> StyledObject<D>
|
||||||
where
|
where
|
||||||
D: Display, {
|
D: Display, {
|
||||||
ObjectStyle::new().apply_to(val)
|
ObjectStyle::new().apply_to(val)
|
||||||
|
@ -14,7 +14,7 @@ pub struct StyledObject<D: Display> {
|
|||||||
pub content: D,
|
pub content: D,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, D: Display> StyledObject<D> {
|
impl<'a, D: Display + 'a> StyledObject<D> {
|
||||||
/// Set the foreground of the styled object to the passed `Color`
|
/// Set the foreground of the styled object to the passed `Color`
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
@ -72,12 +72,10 @@ impl<'a, D: Display> StyledObject<D> {
|
|||||||
/// #Example
|
/// #Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
///
|
|
||||||
/// extern crate crossterm;
|
/// extern crate crossterm;
|
||||||
/// use self::crossterm::style::{style,Attribute};
|
/// use self::crossterm::style::{style,Attribute};
|
||||||
///
|
///
|
||||||
/// style("Some colored text").attr(Attribute::Bold).paint(&screen);
|
/// style("Some colored text").attr(Attribute::Bold).paint(&screen);
|
||||||
///
|
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub fn attr(mut self, attr: Attribute) -> StyledObject<D> {
|
pub fn attr(mut self, attr: Attribute) -> StyledObject<D> {
|
||||||
@ -180,23 +178,37 @@ impl<'a, D: Display> StyledObject<D> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn get_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's.
|
||||||
// {
|
///
|
||||||
// return DisplayableObject::new(screen, &self)
|
/// ```
|
||||||
// }
|
/// 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};
|
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>
|
pub struct DisplayableObject<'a, D:Display + 'a>
|
||||||
{
|
{
|
||||||
styled_object: &'a StyledObject<D>,
|
styled_object: StyledObject<D>,
|
||||||
screen: &'a Screen,
|
screen: &'a Screen,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a, D: Display + 'a> DisplayableObject<'a, D>
|
impl <'a, D: Display + 'a> DisplayableObject<'a, D>
|
||||||
{
|
{
|
||||||
pub fn new(screen: &'a Screen, styled_object: &'a StyledObject<D>) -> DisplayableObject<'a, D>
|
pub fn new(screen: &'a Screen, styled_object: StyledObject<D>) -> DisplayableObject<'a, D>
|
||||||
{
|
{
|
||||||
DisplayableObject { screen, styled_object }
|
DisplayableObject { screen, styled_object }
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
//}
|
Loading…
Reference in New Issue
Block a user