Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Refactor unix (a)sync readers #9

Merged
merged 18 commits into from
Oct 18, 2019
Merged
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
- Remove all references to the crossterm book
- Mouse coordinates synchronized with the cursor (breaking)
- Upper/left reported as `(0, 0)`
- Fixed bug that read sync didnt block
- Fixed bug that read sync didn't block (Windows)
- AsyncReader produces mouse events (#271)
- One reading thread per application, not per AsyncReader
- Fixed cursor position getting consumed by async reader
- Implemented sync reader for read_char (requires raw mode)
- Added mio for reading from FD and more efficient polling (UNIX only)

## Windows only

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ crossterm_winapi = { git = "https://github.com/crossterm-rs/crossterm-winapi.git

[target.'cfg(unix)'.dependencies]
libc = "0.2.51"
mio = "0.6.19"

[dependencies]
crossterm_utils = { version = "0.3.1" }
Expand Down
29 changes: 29 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! A module that contains all the actions related to reading input from the terminal.
//! Like reading a line, reading a character and reading asynchronously.

use std::io;

use crossterm_utils::Result;

// TODO Create a new common AsyncReader structure (like TerminalCursor, TerminalInput, ...).
Expand All @@ -24,6 +26,33 @@ pub(crate) mod windows;
/// This trait is implemented for Windows and UNIX systems.
/// Unix is using the 'TTY' and windows is using 'libc' C functions to read the input.
pub(crate) trait Input {
/// Reads one line from the user input and strips the new line character(s).
///
/// This function **does not work** when the raw mode is enabled (see the
/// [`crossterm_screen`](https://docs.rs/crossterm_screen/) crate documentation
/// to learn more). You should use the
/// [`read_async`](struct.TerminalInput.html#method.read_async),
/// [`read_until_async`](struct.TerminalInput.html#method.read_until_async)
/// or [`read_sync`](struct.TerminalInput.html#method.read_sync) method if the
/// raw mode is enabled.
///
/// # Examples
///
/// ```no_run
/// let input = crossterm_input::input();
/// match input.read_line() {
/// Ok(s) => println!("string typed: {}", s),
/// Err(e) => println!("error: {}", e),
/// }
/// ```
fn read_line(&self) -> Result<String> {
let mut rv = String::new();
io::stdin().read_line(&mut rv)?;
let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
rv.truncate(len);
Ok(rv)
}

/// Read one character from the user input
fn read_char(&self) -> Result<char>;
/// Read the input asynchronously from the user.
Expand Down
Loading