Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io::Result cleanup #232

Merged
merged 8 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don’t drop fixes
Signed-off-by: Robert Vojta <rvojta@me.com>
  • Loading branch information
zrzka committed Sep 18, 2019
commit 24aaaba3ba0ee669fbdb7ac516dde9cf154cdb56
5 changes: 3 additions & 2 deletions examples/alternate_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ fn print_wait_screen() -> Result<()> {
}

/// print wait screen on alternate screen, then switch back.
pub fn print_wait_screen_on_alternate_window() -> Result<()> {
AlternateScreen::to_alternate(false).and_then(|_| print_wait_screen())
fn print_wait_screen_on_alternate_window() -> Result<()> {
let _alt = AlternateScreen::to_alternate(false)?;
print_wait_screen()
}

// cargo run --example alternate_screen
Expand Down
68 changes: 33 additions & 35 deletions examples/key_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,59 +74,57 @@ fn process_input_event(key_event: InputEvent) -> bool {
false
}

pub fn read_asynchronously() -> Result<()> {
fn read_asynchronously() -> Result<()> {
// make sure to enable raw mode, this will make sure key events won't be handled by the terminal it's self and allows crossterm to read the input and pass it back to you.
if let Ok(_) = RawScreen::into_raw_mode() {
let input = input();
let _raw = RawScreen::into_raw_mode()?;

// enable mouse events to be captured.
input.enable_mouse_mode()?;
let input = input();

let mut stdin = input.read_async();
// enable mouse events to be captured.
input.enable_mouse_mode()?;

loop {
if let Some(key_event) = stdin.next() {
if process_input_event(key_event) {
break;
}
let mut stdin = input.read_async();

loop {
if let Some(key_event) = stdin.next() {
if process_input_event(key_event) {
break;
}
thread::sleep(Duration::from_millis(50));
}
thread::sleep(Duration::from_millis(50));
}

// disable mouse events to be captured.
input.disable_mouse_mode()?;
} // <=== raw modes will be disabled here
Ok(())
} // <=== background reader will be disposed when dropped.
// disable mouse events to be captured.
input.disable_mouse_mode()
} // <=== raw modes will be disabled here

pub fn read_synchronously() -> Result<()> {
fn read_synchronously() -> Result<()> {
// make sure to enable raw mode, this will make sure key events won't be handled by the terminal it's self and allows crossterm to read the input and pass it back to you.
if let Ok(_) = RawScreen::into_raw_mode() {
let input = input();
let _raw = RawScreen::into_raw_mode()?;

// enable mouse events to be captured.
input.enable_mouse_mode()?;
let input = input();

let mut sync_stdin = input.read_sync();
// enable mouse events to be captured.
input.enable_mouse_mode()?;

loop {
let event = sync_stdin.next();
let mut sync_stdin = input.read_sync();

if let Some(key_event) = event {
if process_input_event(key_event) {
break;
}
loop {
let event = sync_stdin.next();

if let Some(key_event) = event {
if process_input_event(key_event) {
break;
}
}
}

// disable mouse events to be captured.
input.disable_mouse_mode()?;
} // <=== raw modes will be disabled here
Ok(())
}
// disable mouse events to be captured.
input.disable_mouse_mode()
} // <=== raw modes will be disabled here

// cargo run --example key_events
fn main() -> Result<()> {
read_synchronously()
// read_asynchronously();
// read_asynchronously()
}
5 changes: 3 additions & 2 deletions examples/raw_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ fn print_wait_screen() -> Result<()> {
Ok(())
}

pub fn print_wait_screen_on_alternate_window() -> Result<()> {
fn print_wait_screen_on_alternate_window() -> Result<()> {
// by passing in 'true' the alternate screen will be in raw modes.
AlternateScreen::to_alternate(true).and_then(|_| print_wait_screen())
let _alt = AlternateScreen::to_alternate(true)?;
print_wait_screen()
}

// cargo run --example raw_mode
Expand Down