From bb39591150563bb2781520e39165c470f5f1219f Mon Sep 17 00:00:00 2001 From: TimonPost Date: Tue, 14 Aug 2018 23:02:25 +0200 Subject: [PATCH] Overall commend improvement --- README.md | 4 +-- src/common/crossterm.rs | 54 +++++++++---------------------- src/common/functions.rs | 26 +++++++-------- src/common/screen/screen.rs | 8 ++--- src/lib.rs | 10 +++--- src/modules/cursor/cursor.rs | 45 +------------------------- src/modules/cursor/mod.rs | 2 +- src/modules/input/input.rs | 18 ----------- src/modules/style/color.rs | 14 +------- src/modules/style/mod.rs | 2 +- src/modules/style/styledobject.rs | 2 -- src/modules/terminal/mod.rs | 2 +- src/modules/terminal/terminal.rs | 23 ++----------- src/modules/write/stdout.rs | 2 -- 14 files changed, 46 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index 30bd7ef..e8a9582 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/src/common/crossterm.rs b/src/common/crossterm.rs index 079b457..2c2845d 100644 --- a/src/common/crossterm.rs +++ b/src/common/crossterm.rs @@ -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 @@ -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(); diff --git a/src/common/functions.rs b/src/common/functions.rs index 87349df..3128c9b 100644 --- a/src/common/functions.rs +++ b/src/common/functions.rs @@ -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(winapi_impl: T, unix_impl: T) -> Option { let mut term: Option = 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(); - 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; - if !does_support { - term = Some(winapi_impl); - } + // uncomment this line when you want to use the winapi implementation. + does_support = false; + if !does_support { + term = Some(winapi_impl); } - } + } if does_support { term = Some(unix_impl); diff --git a/src/common/screen/screen.rs b/src/common/screen/screen.rs index fbf5b7b..8f3dc8f 100644 --- a/src/common/screen/screen.rs +++ b/src/common/screen/screen.rs @@ -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(); /// } /// ``` /// diff --git a/src/lib.rs b/src/lib.rs index d8f4f33..477059e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] diff --git a/src/modules/cursor/cursor.rs b/src/modules/cursor/cursor.rs index b043997..9b3a01b 100644 --- a/src/modules/cursor/cursor.rs +++ b/src/modules/cursor/cursor.rs @@ -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, @@ -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); diff --git a/src/modules/cursor/mod.rs b/src/modules/cursor/mod.rs index 8c08aae..b166584 100644 --- a/src/modules/cursor/mod.rs +++ b/src/modules/cursor/mod.rs @@ -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); /// Get the location (x,y) of the current cusror in the context diff --git a/src/modules/input/input.rs b/src/modules/input/input.rs index 653eaca..31dc3b2 100644 --- a/src/modules/input/input.rs +++ b/src/modules/input/input.rs @@ -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 { 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 { 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); diff --git a/src/modules/style/color.rs b/src/modules/style/color.rs index 55bdf1c..972d37e 100644 --- a/src/modules/style/color.rs +++ b/src/modules/style/color.rs @@ -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, @@ -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); diff --git a/src/modules/style/mod.rs b/src/modules/style/mod.rs index f6f63f1..bc5086a 100644 --- a/src/modules/style/mod.rs +++ b/src/modules/style/mod.rs @@ -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); /// Set the background color to the given color. diff --git a/src/modules/style/styledobject.rs b/src/modules/style/styledobject.rs index 8dfc655..aec3d39 100644 --- a/src/modules/style/styledobject.rs +++ b/src/modules/style/styledobject.rs @@ -21,8 +21,6 @@ pub struct StyledObject { impl StyledObject { /// Set the foreground of the styled object to the passed `Color` /// - /// #Example - /// /// ```rust /// use self::crossterm::style::{style,Color}; /// diff --git a/src/modules/terminal/mod.rs b/src/modules/terminal/mod.rs index 0d7a0c5..c53a28e 100644 --- a/src/modules/terminal/mod.rs +++ b/src/modules/terminal/mod.rs @@ -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); /// Get the terminal size (x,y) diff --git a/src/modules/terminal/terminal.rs b/src/modules/terminal/terminal.rs index 91134b8..73154b1 100644 --- a/src/modules/terminal/terminal.rs +++ b/src/modules/terminal/terminal.rs @@ -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); diff --git a/src/modules/write/stdout.rs b/src/modules/write/stdout.rs index 15fdf53..af816fd 100644 --- a/src/modules/write/stdout.rs +++ b/src/modules/write/stdout.rs @@ -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 {