diff --git a/Cargo.toml b/Cargo.toml index efa0e2d..ce80a51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "crossterm" -version = "0.3.0" +version = "0.4.0" authors = ["T Post "] description = "An crossplatform terminal library for manipulating terminals." repository = "https://github.com/TimonPost/crossterm" documentation = "https://docs.rs/crossterm/" license = "MIT" -keywords = ["console", "color", "cursor", "terminal", "cli"] +keywords = ["console", "color", "cursor", "input", "terminal"] exclude = ["target", "Cargo.lock"] readme = "README.md" diff --git a/examples/examples.rs b/examples/examples.rs index da8bbe9..fa9a323 100644 --- a/examples/examples.rs +++ b/examples/examples.rs @@ -19,7 +19,6 @@ use std::io::Write; use std::{thread,time}; fn main() { - input::keyboard::async_input::read_async_demo(); - terminal::raw_mode::print_wait_screen_on_alternate_window(); + ::crossterm::terminal::terminal(&::crossterm::Screen::default()).terminal_size(); thread::sleep(time::Duration::from_millis(2000)); } diff --git a/examples/program_examples/command_bar.rs b/examples/program_examples/command_bar.rs index 8939d2b..2b33c03 100644 --- a/examples/program_examples/command_bar.rs +++ b/examples/program_examples/command_bar.rs @@ -27,24 +27,24 @@ fn main() { let mut stdin = input.read_async().bytes(); loop - { - let a = stdin.next(); + { + let a = stdin.next(); - match a { - Some(Ok(13)) => - { - input_buf.lock().unwrap().clear(); - } - Some(Ok(val)) => - { - input_buf.lock().unwrap().push(a.unwrap().unwrap() as char); - } - _ => {} - } - - thread::sleep(time::Duration::from_millis(100)); - count += 1; + match a { + Some(Ok(13)) => + { + input_buf.lock().unwrap().clear(); + } + Some(Ok(val)) => + { + input_buf.lock().unwrap().push(a.unwrap().unwrap() as char); + } + _ => {} } + + thread::sleep(time::Duration::from_millis(100)); + count += 1; + } }).join(); diff --git a/src/common/commands/win_commands.rs b/src/common/commands/win_commands.rs index 84de4d3..957496f 100644 --- a/src/common/commands/win_commands.rs +++ b/src/common/commands/win_commands.rs @@ -79,7 +79,8 @@ impl RawModeCommand { pub fn new() -> Self { RawModeCommand { - mask: ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_OUTPUT , + + mask: ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_LINE_INPUT } } } @@ -146,7 +147,6 @@ impl ToAlternateScreenCommand { impl IAlternateScreenCommand for ToAlternateScreenCommand { fn enable(&self, stdout: &mut TerminalOutput) -> Result<()> { - use super::super::super::modules::output::WinApiOutput; let handle = handle::get_output_handle()?; diff --git a/src/common/crossterm.rs b/src/common/crossterm.rs index bcc09f9..f496df9 100644 --- a/src/common/crossterm.rs +++ b/src/common/crossterm.rs @@ -101,10 +101,13 @@ impl<'crossterm> Crossterm { /// This could be used to style an `Displayable` type with colors and attributes. /// /// ```rust - /// use crossterm::Screen ; + /// extern crate crossterm; + /// use crossterm::{Crossterm, Screen}; + /// + /// let crossterm = Crossterm::new(&Screen::default()); /// /// // get an styled object which could be painted to the terminal. - /// let styled_object = style("Some Blue colored text on black background") + /// let styled_object = crossterm.style("Some Blue colored text on black background") /// .with(Color::Blue) /// .on(Color::Black); /// diff --git a/src/common/functions.rs b/src/common/functions.rs index 599e5c1..ad48721 100644 --- a/src/common/functions.rs +++ b/src/common/functions.rs @@ -44,10 +44,10 @@ pub fn get_module(winapi_impl: T, unix_impl: T) -> Option { if !windows_supportable() { // Try to enable ansi on windows if not than use WINAPI. -// does_support = try_enable_ansi_support(); + 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); } diff --git a/src/common/screen/alternate.rs b/src/common/screen/alternate.rs index 463d43b..5f81905 100644 --- a/src/common/screen/alternate.rs +++ b/src/common/screen/alternate.rs @@ -1,6 +1,5 @@ //! This module contains all the logic for switching between alternate screen and main screen. //! -//! //! *Nix style applications often utilize an alternate screen buffer, so that they can modify the entire contents of the buffer, without affecting the application that started them. //! The alternate buffer is exactly the dimensions of the window, without any scrollback region. //! For an example of this behavior, consider when vim is launched from bash. @@ -13,6 +12,7 @@ use std::io; use std::convert::From; /// With this type you will be able to switch to alternate screen and back to main screen. +/// Check also the Screen type for swishing to alternate mode. pub struct AlternateScreen { command: Box, @@ -29,6 +29,8 @@ impl AlternateScreen { /// Switch to alternate screen. This function will return an `AlternateScreen` instance if everything went well this type will give you control over the `AlternateScreen`. /// + /// The bool specifies whether the screen should be in raw mode or not. + /// /// # What is Alternate screen? /// *Nix style applications often utilize an alternate screen buffer, so that they can modify the entire contents of the buffer, without affecting the application that started them. /// The alternate buffer is exactly the dimensions of the window, without any scrollback region. diff --git a/src/common/screen/mod.rs b/src/common/screen/mod.rs index ea00ad6..02cc1cb 100644 --- a/src/common/screen/mod.rs +++ b/src/common/screen/mod.rs @@ -6,6 +6,6 @@ mod screen; use super::{commands, functions, TerminalOutput}; +pub use self::raw::RawScreen; pub use self::alternate::AlternateScreen; pub use self::screen::Screen; -pub use self::raw::RawScreen; diff --git a/src/common/screen/raw.rs b/src/common/screen/raw.rs index 9796001..dbb16bb 100644 --- a/src/common/screen/raw.rs +++ b/src/common/screen/raw.rs @@ -24,7 +24,7 @@ use std::sync::Arc; pub struct RawScreen; impl RawScreen { - /// Put terminal in raw mode. + /// Put terminal in raw mode. How ever using the `Screen` type to enable raw mode is much better. pub fn into_raw_mode() -> io::Result<()> { #[cfg(not(target_os = "windows"))] diff --git a/src/common/screen/screen.rs b/src/common/screen/screen.rs index 74b5e1c..4e82346 100644 --- a/src/common/screen/screen.rs +++ b/src/common/screen/screen.rs @@ -13,8 +13,6 @@ use std::sync::Arc; /// /// Also this screen has an buffer where you can write to. When you want to write the buffer to the screen you could flush the screen. /// -/// #Example -/// /// ```rust /// // create default screen. /// let screen = Screen::default(); @@ -64,7 +62,9 @@ impl Screen return Screen::default(); } - /// Switch to alternate screen. This function will return an `AlternateScreen` instance if everything went well this type will give you control over the `AlternateScreen`. + /// Switch to alternate screen. This function will return an `AlternateScreen` instance. If everything went well this type will give you control over the `AlternateScreen`. + /// + /// The bool 'raw_mode' specifies whether the alternate screen should be raw mode or not. /// /// # What is Alternate screen? /// *Nix style applications often utilize an alternate screen buffer, so that they can modify the entire contents of the buffer, without affecting the application that started them. @@ -92,7 +92,7 @@ impl Screen self.stdout.flush() } - // this will disable the drop which will cause raw modes not to be undone on drop of `Screen`. + /// This will disable the drop which will cause raw modes not to be undone on drop of `Screen`. pub fn disable_drop(&mut self) { self.drop = false; diff --git a/src/kernel/windows_kernel/cursor.rs b/src/kernel/windows_kernel/cursor.rs index 6c130dc..dc205ec 100644 --- a/src/kernel/windows_kernel/cursor.rs +++ b/src/kernel/windows_kernel/cursor.rs @@ -9,7 +9,7 @@ use super::{csbi, handle, kernel, TerminalOutput}; use std::io; use std::sync::Arc; - +use kernel::windows_kernel::kernel::get_largest_console_window_size; /// This stores the cursor pos, at program level. So it can be recalled later. static mut SAVED_CURSOR_POS: (u16, u16) = (0, 0); @@ -75,6 +75,14 @@ pub fn set_console_cursor_position(x: i16, y: i16) { } } +//pub fn set_relative_cursor_pos(x: i16, y: i16) +//{ +// let (cur_x, cur_y) = pos()?; +// let Relative(x, y) = *self; +// let (x, y) = (x + cur_x, y + cur_y); +// platform::set_cursor_pos(x, y)?; +//} + /// change the cursor visibility. pub fn cursor_visibility(visable: bool) -> io::Result<()> { let handle = handle::get_current_handle().unwrap(); diff --git a/src/kernel/windows_kernel/mod.rs b/src/kernel/windows_kernel/mod.rs index d08c874..5546464 100644 --- a/src/kernel/windows_kernel/mod.rs +++ b/src/kernel/windows_kernel/mod.rs @@ -12,7 +12,6 @@ use winapi::um::wincon::{CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT}; use winapi::um::winnt::HANDLE; use TerminalOutput; -use super::super::modules::output::WinApiOutput; use common::traits::Empty; diff --git a/src/kernel/windows_kernel/terminal.rs b/src/kernel/windows_kernel/terminal.rs index 184270e..f885549 100644 --- a/src/kernel/windows_kernel/terminal.rs +++ b/src/kernel/windows_kernel/terminal.rs @@ -7,6 +7,10 @@ use std::sync::Arc; pub fn terminal_size() -> (u16, u16) { let handle = handle::get_output_handle().unwrap(); +// if let Ok(csbi) = csbi::get_csbi_by_handle(&handle) { +// println!("right: {} left: {} bottom: {}, top: {} window top {} windows width {} csbi.dwCursorPosition.X {} csbi.dwCursorPosition.Y {}", csbi.srWindow.Right, csbi.srWindow.Left, csbi.srWindow.Bottom, csbi.srWindow.Top, csbi.dwSize.X,csbi.dwSize.Y, csbi.dwCursorPosition.X, csbi.dwCursorPosition.Y); +// } + if let Ok(csbi) = csbi::get_csbi_by_handle(&handle) { ( (csbi.srWindow.Right - csbi.srWindow.Left) as u16, diff --git a/src/lib.rs b/src/lib.rs index 05e386b..6052ce9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,14 +9,14 @@ mod common; mod kernel; mod modules; -pub use common::screen; +use common::screen; pub use modules::cursor; pub use modules::input; pub use modules::output; pub use modules::style; pub use modules::terminal; -pub use common::screen::Screen; +pub use common::screen::{Screen, AlternateScreen}; pub use common::Crossterm; pub use output::TerminalOutput; pub use self::cursor::*; @@ -24,7 +24,6 @@ pub use self::input::*; pub use self::output::*; pub use self::style::*; -use output::IStdout; use common::functions; #[cfg(unix)] diff --git a/src/modules/input/input.rs b/src/modules/input/input.rs index 2aff617..345bfa4 100644 --- a/src/modules/input/input.rs +++ b/src/modules/input/input.rs @@ -70,6 +70,9 @@ impl<'stdout> TerminalInput<'stdout> { /// Read the input asynchronously from the user. /// + /// This call will not block the current thread. + // Under the hood a thread is fired which will read input on unix systems from TTY and on windows systems with '_getwch' and '_getwche' + /// /// ```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); @@ -97,7 +100,9 @@ impl<'stdout> TerminalInput<'stdout> { self.terminal_input.read_async(&self.stdout) } - /// Read the input asynchronously until a certain character is hit. + /// Read the input asynchronously until a certain character is hit. + /// + /// This is the same as `read_async()` but stops reading when a certain character is hit. /// /// ```rust /// // we need to enable raw mode otherwise the characters will be outputted by default before we are able to read them. diff --git a/src/modules/input/mod.rs b/src/modules/input/mod.rs index 0c64f23..fe9cec4 100644 --- a/src/modules/input/mod.rs +++ b/src/modules/input/mod.rs @@ -46,6 +46,11 @@ pub struct AsyncReader { recv: mpsc::Receiver>, } +impl AsyncReader +{ + +} + impl Read for AsyncReader { /// Read from the byte stream. /// @@ -66,7 +71,6 @@ impl Read for AsyncReader { total += 1; }, _ => return Err(Error::new(ErrorKind::Other, "No characters pressed.")), - } } diff --git a/src/modules/input/windows_input.rs b/src/modules/input/windows_input.rs index 7faa366..36c66fc 100644 --- a/src/modules/input/windows_input.rs +++ b/src/modules/input/windows_input.rs @@ -102,10 +102,6 @@ impl ITerminalInput for WindowsInput { } tx.send(Ok(pressed_char as u8)); - - if pressed_char == 13 { - return; - } } }); diff --git a/src/modules/output/mod.rs b/src/modules/output/mod.rs index 4e01f94..9a8d7b2 100644 --- a/src/modules/output/mod.rs +++ b/src/modules/output/mod.rs @@ -6,9 +6,9 @@ mod ansi_output; #[cfg(target_os = "windows")] mod winapi_output; -pub use self::ansi_output::AnsiOutput; +use self::ansi_output::AnsiOutput; #[cfg(target_os = "windows")] -pub use self::winapi_output::WinApiOutput; +use self::winapi_output::WinApiOutput; pub use self::output::TerminalOutput; @@ -25,7 +25,7 @@ use super::{functions}; /// /// 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 IStdout { +trait IStdout { /// Write an &str to the current stdout and flush the screen. fn write_str(&self, string: &str ) -> io::Result; /// Write [u8] buffer to console. diff --git a/src/modules/style/mod.rs b/src/modules/style/mod.rs index 30b0cef..7c28d37 100644 --- a/src/modules/style/mod.rs +++ b/src/modules/style/mod.rs @@ -44,8 +44,6 @@ trait ITerminalColor { /// This could be used to style an Displayable with colors and attributes. /// -/// #Example -/// /// ```rust /// /// use crossterm::Screen;