Overall commend improvement

This commit is contained in:
TimonPost 2018-08-14 23:02:25 +02:00
parent 4005634086
commit bb39591150
14 changed files with 46 additions and 166 deletions

View File

@ -54,8 +54,6 @@ use self::crossterm::cursor::*;
use self::crossterm::terminal::*;
// this mudule is used for input related actions
use self::crossterm::terminal::*;
// this type could be used to access the above modules.
use self::crossterm::Crossterm;
```
@ -131,7 +129,7 @@ let style2 = style("Some Red font on Yellow background").with(Color::Red).on(Col
let screen = Screen::default();
/// ! The following code only works for unix based systems.
/// !! The following code only works for unix based systems !!
// some attributes
let normal = style("Normal text");
let bold = style("Bold text").bold();

View File

@ -23,36 +23,31 @@ use common::commands::win_commands;
use write::Stdout;
/// This type could be used to access the `cursor, terminal, color, input, styling` module more easily.
/// You need to pass a reference to the screen where on you want to perform the actions.
/// You need to pass a reference to the screen where on you want to perform the actions to the `Crossterm` type.
///
///
/// #Example
/// If you want to use the default screen you could do it like this:
///
/// ```rust
///
/// extern crate crossterm;
/// use crossterm::{Crossterm, Screen};
///
/// let crossterm = Crossterm::new();
/// let cursor = crossterm.cursor(&Screen::default());
///
/// let crossterm = Crossterm::new(&Screen::default());
/// let cursor = crossterm.cursor();
/// ```
///
/// If you want to perform actions on the `AlternateScreen` make sure to pass a refrence to the screen of the `AlternateScreen`.
/// If you want to perform actions on the `AlternateScreen` make sure to pass a reference to the screen of the `AlternateScreen`.
///
/// ```
/// /// extern crate crossterm;
/// extern crate crossterm;
/// use crossterm::{Crossterm, Screen};
///
/// let main_screen = Screen::default();
///
/// if let Ok(alternate_srceen) = main_screen.enable_alternate_modes(false)
/// {
/// let crossterm = Crossterm::new();
/// let cursor = crossterm.cursor(&alternate_screen.screen);
/// let crossterm = Crossterm::new(&alternate_screen.screen);
/// let cursor = crossterm.cursor();
/// }
///
/// ```
pub struct Crossterm {
stdout: Arc<Stdout>
@ -64,85 +59,68 @@ impl<'crossterm> Crossterm {
Crossterm { stdout: screen.stdout.clone() }
}
/// Get an TerminalCursor implementation whereon cursor related actions can be performed.
///
/// #Example
/// Get an `TerminalCursor` implementation whereon cursor related actions can be performed.
///
/// ```rust
///
/// extern crate crossterm;
/// use crossterm::{Crossterm, Screen};
///
/// let crossterm = Crossterm::new(&Screen::default());
/// let cursor = crossterm.cursor();
///
/// ```
pub fn cursor(&self) -> cursor::TerminalCursor {
cursor::TerminalCursor::new(&self.stdout)
}
/// Get an TerminalInput implementation whereon terminal related actions can be performed.
///
/// #Example
/// Get an `TerminalInput` implementation whereon terminal related actions can be performed.
///
/// ```rust
///
/// extern crate crossterm;
/// use crossterm::{Crossterm, Screen};
/// use crossterm::terminal;
///
/// let crossterm = Crossterm::new(&Screen::default());
/// let input = crossterm.input();
///
/// ```
pub fn input(&self) -> input::TerminalInput {
return input::TerminalInput::new(&self.stdout);
}
/// Get an Terminal implementation whereon terminal related actions can be performed.
///
/// #Example
/// Get an `Terminal` implementation whereon terminal related actions can be performed.
///
/// ```rust
///
/// extern crate crossterm;
/// use crossterm::{Crossterm, Screen};
///
/// let crossterm = Crossterm::new(&Screen::default());
/// let mut terminal = crossterm.terminal();
///
/// ```
pub fn terminal(&self) -> terminal::Terminal {
return terminal::Terminal::new(&self.stdout);
}
/// Get an TerminalColor implementation whereon color related actions can be performed.
///
/// #Example
/// Get an `TerminalColor` implementation whereon color related actions can be performed.
///
/// ```rust
///
/// extern crate crossterm;
/// use crossterm::{Crossterm, Screen};
///
/// let crossterm = Crossterm::new(&Screen::default());
/// let mut terminal = crossterm.terminal();
///
/// ```
pub fn color(&self) -> style::TerminalColor {
return style::TerminalColor::new(&self.stdout);
}
/// This could be used to style an Displayable with colors and attributes.
///
/// #Example
/// This could be used to style an `Displayable` type with colors and attributes.
///
/// ```rust
///
/// use crossterm::{ Screen };
/// use crossterm::Screen ;
///
/// // get an styled object which could be painted to the terminal.
/// let styled_object = style("Some Blue colored text on black background").with(Color::Blue).on(Color::Black);
/// let styled_object = style("Some Blue colored text on black background")
/// .with(Color::Blue)
/// .on(Color::Black);
///
/// // create an default screen.
/// let screen = Screen::default();

View File

@ -3,6 +3,9 @@
use super::Stdout;
use std::sync::Arc;
#[cfg(windows)]
use kernel::windows_kernel::ansi_support::{try_enable_ansi_support, windows_supportable};
#[cfg(windows)]
use kernel::windows_kernel::terminal::{buffer_size, exit, terminal_size};
@ -47,22 +50,17 @@ pub fn get_module<T>(winapi_impl: T, unix_impl: T) -> Option<T> {
let mut term: Option<T> = None;
let mut does_support = true;
if cfg!(target_os = "windows") {
#[cfg(windows)]
use kernel::windows_kernel::ansi_support::{try_enable_ansi_support, windows_supportable};
if !windows_supportable()
{
// Try to enable ansi on windows if not than use WINAPI.
does_support = try_enable_ansi_support();
// uncomment this line when you want to use the winapi implementation.
// does_support = false;
does_support = false;
if !does_support {
term = Some(winapi_impl);
}
}
}
if does_support {
term = Some(unix_impl);

View File

@ -37,15 +37,15 @@ use std::sync::Arc;
/// // write the above text by flushing the internal buffer of this type.
/// screen.flush();
///
/// // create raw alternate screen from normal screen.
/// let screen = Screen::new();
/// let screen = Screen::new(false);
///
/// // create raw alternate screen from normal screen.
/// if let Ok(alternate_screen) = screen.enable_alternate_modes(true)
/// {
/// let crossterm = Crossterm::new();
/// let crossterm = Crossterm::new(&alternate_screen.screen);
///
/// // make sure to pass in the screen of the AlternateScreen.
/// crossterm.cursor(&alternate_screen.screen);
/// crossterm.cursor();
/// }
/// ```
///

View File

@ -4,7 +4,7 @@
//! You can just call the action you want to perform and under water it will check what to do based on the current platform.
#[macro_use]
pub mod common;
mod common;
mod kernel;
mod modules;
@ -14,11 +14,13 @@ pub use modules::cursor;
pub use modules::input;
pub use modules::write;
pub use modules::style;
pub use modules::terminal;
pub use modules::terminal;
pub use common::Crossterm;
pub use write::{IStdout, Stdout};
pub use common::screen::Screen;
pub use common::Crossterm;
pub use write::{Stdout};
use write::IStdout;
#[cfg(unix)]
extern crate libc;
#[cfg(unix)]

View File

@ -12,12 +12,9 @@ use std::sync::Arc;
/// Struct that stores an specific platform implementation for cursor related actions.
///
/// Check `/examples/version/cursor` in the library for more specific examples.
///
/// #Example
/// Check `/examples/cursor` in the library for more specific examples.
///
/// ```rust
///
/// extern crate crossterm;
/// use self::crossterm::cursor::cursor;
/// use self::crossterm::Screen;
@ -32,7 +29,6 @@ use std::sync::Arc;
/// cursor.hide();
/// cursor.blink(true);
/// cursor.move_left(2);
///
/// ```
pub struct TerminalCursor<'stdout> {
screen: &'stdout Arc<Stdout>,
@ -58,8 +54,6 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Goto some position (x,y) in the terminal.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
@ -74,32 +68,25 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Get current cursor position (x,y) in the terminal.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// // get the current cursor pos
/// let (x,y) = cursor.pos();
///
/// ```
pub fn pos(&self) -> (u16, u16) {
self.terminal_cursor.pos(&self.screen)
}
/// Move the current cursor position `n` times up.
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// // Move the cursor to position 3 times to the up in the terminal
/// cursor.move_up(3);
///
/// ```
pub fn move_up(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_up(count, &self.screen);
@ -108,16 +95,12 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Move the current cursor position `n` times right.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// // Move the cursor to position 3 times to the right in the terminal
/// cursor.move_right(3);
///
/// ```
pub fn move_right(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_right(count, &self.screen);
@ -126,16 +109,12 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Move the current cursor position `n` times down.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// // Move the cursor to position 3 times to the down in the terminal
/// cursor.move_down(3);
///
/// ```
pub fn move_down(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_down(count, &self.screen);
@ -144,16 +123,12 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Move the current cursor position `n` times left.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// // Move the cursor to position 3 times to the left in the terminal
/// cursor.move_left(3);
///
/// ```
pub fn move_left(&mut self, count: u16) -> &mut TerminalCursor<'stdout> {
self.terminal_cursor.move_left(count, &self.screen);
@ -164,15 +139,11 @@ impl<'stdout> TerminalCursor<'stdout> {
///
/// Note that this position is stored program based not per instance of the `Cursor` struct.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// cursor.safe_position();
///
/// ```
pub fn save_position(&self) {
self.terminal_cursor.save_position(&self.screen);
@ -182,15 +153,11 @@ impl<'stdout> TerminalCursor<'stdout> {
///
/// Note that this method reset to the position set by `save_position()` and that this position is stored program based not per instance of the `Cursor` struct.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
///
/// cursor.reset_position();
///
/// ```
pub fn reset_position(&self) {
self.terminal_cursor.reset_position(&self.screen);
@ -198,13 +165,9 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Hide de cursor in the console.
///
/// #Example
///
/// ```rust
///
/// let cursor = cursor(&Screen::default());
/// cursor.hide();
///
/// ```
pub fn hide(&self) {
self.terminal_cursor.hide(&self.screen);
@ -212,8 +175,6 @@ impl<'stdout> TerminalCursor<'stdout> {
/// Show the cursor in the console.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
@ -229,15 +190,11 @@ impl<'stdout> TerminalCursor<'stdout> {
///
/// Not all terminals are supporting this functionality. Windows versions lower than windows 10 also are not supporting this version.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let cursor = cursor(&screen);
/// cursor.blink(true);
/// cursor.blink(false);
///
/// ```
pub fn blink(&self, blink: bool) {
self.terminal_cursor.blink(blink, &self.screen);

View File

@ -24,7 +24,7 @@ use std::sync::Arc;
///!
///! This trait is implemented for `WINAPI` (Windows specific) and `ANSI` (Unix specific),
///! so that cursor related actions can be preformed on both unix and windows systems.
pub trait ITerminalCursor {
trait ITerminalCursor {
/// Goto some location (x,y) in the context.
fn goto(&self, x: u16, y: u16, screen_manager: &Arc<Stdout>);
/// Get the location (x,y) of the current cusror in the context

View File

@ -9,10 +9,7 @@ use super::*;
///
/// Check `/examples/input` the examples folder on github for more info.
///
/// #Example
///
/// ```rust
///
/// extern crate crossterm;
/// use self::crossterm::Screen;
/// use self::crossterm::input::input;
@ -45,17 +42,13 @@ impl<'stdout> TerminalInput<'stdout> {
/// Read one line from the user input.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let input = input(&screen);
/// match input.read_line() {
/// Ok(s) => println!("string typed: {}", s),
/// Err(e) => println!("error: {}", e),
/// }
///
/// ```
pub fn read_line(&self) -> io::Result<String> {
self.terminal_input.read_line(&self.stdout)
@ -63,10 +56,7 @@ impl<'stdout> TerminalInput<'stdout> {
/// Read one character from the user input
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let input = input(&screen);
///
@ -74,7 +64,6 @@ impl<'stdout> TerminalInput<'stdout> {
/// Ok(c) => println!("character pressed: {}", c),
/// Err(e) => println!("error: {}", e),
/// }
///
/// ```
pub fn read_char(&self) -> io::Result<char> {
return self.terminal_input.read_char(&self.stdout);
@ -82,12 +71,7 @@ impl<'stdout> TerminalInput<'stdout> {
/// Read the input asynchronously from the user.
///
/// #Example
///
/// ```rust
///
/// use crossterm::{Crossterm, Screen}
///
/// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them.
/// let screen = Screen::new(true);
/// let input = input(&screen);
@ -116,8 +100,6 @@ impl<'stdout> TerminalInput<'stdout> {
/// Read the input asynchronously until a certain character is hit.
///
/// #Example
///
/// ```rust
/// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them.
/// let screen = Screen::new(true);

View File

@ -7,12 +7,9 @@ use Screen;
/// Struct that stores an specific platform implementation for color related actions.
///
/// Check `/examples/version/color` in the library for more specific examples.
///
/// #Example
/// Check `/examples/color` in the library for more specific examples.
///
/// ```rust
///
/// use crossterm::{Screen}
/// use crossterm::color::color;
///
@ -25,7 +22,6 @@ use Screen;
/// colored_terminal.set_bg(Color::Red);
/// // reset color to default
/// colored_terminal.reset();
///
/// ```
pub struct TerminalColor<'stdout> {
color: Box<ITerminalColor>,
@ -52,8 +48,6 @@ impl<'stdout> TerminalColor<'stdout> {
/// Set the foreground color to the given color.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let colored_terminal = color(&screen);
@ -70,10 +64,7 @@ impl<'stdout> TerminalColor<'stdout> {
/// Set the background color to the given color.
///
/// #Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let colored_terminal = color(&screen);
///
@ -88,14 +79,11 @@ impl<'stdout> TerminalColor<'stdout> {
}
/// Reset the terminal colors and attributes to default.
/// # Example
///
/// ```rust
///
/// let screen = Screen::default();
/// let colored_terminal = color(&screen);
/// colored_terminal.reset();
///
/// ```
pub fn reset(&self) {
self.color.reset(&self.stdout);

View File

@ -30,7 +30,7 @@ use super::{functions, Stdout};
///
/// This trait is implemented for `WINAPI` (Windows specific) and `ANSI` (Unix specific),
/// so that color related actions can be preformed on both unix and windows systems.
pub trait ITerminalColor {
trait ITerminalColor {
/// Set the foreground color to the given color.
fn set_fg(&self, fg_color: Color, stdout: &Arc<Stdout>);
/// Set the background color to the given color.

View File

@ -21,8 +21,6 @@ pub struct StyledObject<D: Display> {
impl<D: Display> StyledObject<D> {
/// Set the foreground of the styled object to the passed `Color`
///
/// #Example
///
/// ```rust
/// use self::crossterm::style::{style,Color};
///

View File

@ -32,7 +32,7 @@ pub enum ClearType {
///
/// This trait is implemented for `WINAPI` (Windows specific) and `ANSI` (Unix specific),
/// so that color related actions can be preformed on both unix and windows systems.
pub trait ITerminal {
trait ITerminal {
/// Clear the current cursor by specifying the clear type
fn clear(&self, clear_type: ClearType, screen_manager: &Arc<Stdout>);
/// Get the terminal size (x,y)

View File

@ -8,12 +8,9 @@ use std::io::Write;
/// Struct that stores an specific platform implementation for terminal related actions.
///
/// Check `/examples/version/terminal` in the library for more specific examples.
///
/// #Example
/// Check `/examples/terminal` in the library for more specific examples.
///
/// ```rust
///
/// use crossterm::terminal::terminal;
///
/// let screen = Screen::default();
@ -47,12 +44,9 @@ impl<'stdout> Terminal<'stdout> {
}
}
/// Clear the current cursor by specifying the clear type
///
/// #Example
/// Clear the current cursor by specifying the clear type.
///
/// ```rust
///
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
///
@ -66,7 +60,6 @@ impl<'stdout> Terminal<'stdout> {
/// term.clear(terminal::ClearType::CurrentLine);
/// // clear all cells from cursor position until new line in terminal.
/// term.clear(terminal::ClearType::UntilNewLine);
///
/// ```
pub fn clear(&self, clear_type: ClearType) {
self.terminal.clear(clear_type, &self.screen);
@ -74,8 +67,6 @@ impl<'stdout> Terminal<'stdout> {
/// Get the terminal size (x,y).
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
@ -90,8 +81,6 @@ impl<'stdout> Terminal<'stdout> {
/// Scroll `n` lines up in the current terminal.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
@ -106,8 +95,6 @@ impl<'stdout> Terminal<'stdout> {
/// Scroll `n` lines up in the current terminal.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
@ -122,8 +109,6 @@ impl<'stdout> Terminal<'stdout> {
/// Set the terminal size. Note that not all terminals can be set to a very small scale.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
@ -138,8 +123,6 @@ impl<'stdout> Terminal<'stdout> {
/// Exit the current process.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);
@ -153,8 +136,6 @@ impl<'stdout> Terminal<'stdout> {
/// Write any displayable content to the current terminal screen.
///
/// #Example
///
/// ```rust
/// let screen = Screen::default();
/// let mut term = terminal(&screen);

View File

@ -39,8 +39,6 @@ pub struct Stdout {
pub is_in_raw_mode:bool,
}
// todo: impl Default for stdout
impl Stdout {
/// Create new screen write instance whereon screen related actions can be performed.
pub fn new(is_in_raw_mode: bool) -> Self {