Skip to content

Commit

Permalink
die method added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Mar 4, 2024
1 parent 0e40aac commit 79f391c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
use std::time::Duration;

use crossterm::{
event::{poll, read, Event::Key, KeyCode, KeyEvent},
terminal::{disable_raw_mode, enable_raw_mode},
};
use std::time::Duration;

fn main() {
let _ = enable_raw_mode();
loop {
let mut c: Option<KeyEvent> = None;

if let Ok(true) = poll(Duration::from_millis(100)) {
if let Ok(Key(key)) = read() {
c = Some(key);
match poll(Duration::from_millis(100)) {
Ok(true) => {
if let Ok(Key(key)) = read() {
c = Some(key);
} else {
die("Read error")
}
}
Ok(false) => (),
Err(_) => die("Poll error"),
}

if let Some(c) = c {
println!("{c:?}\r");
if c.code == KeyCode::Char('q') {
break;
}
} else {
println!("No input\r");
}
}
let _ = disable_raw_mode();
}

fn die<S: Into<String>>(message: S) {
let _ = disable_raw_mode();
eprintln!("{}: {}", message.into(), std::io::Error::last_os_error());
std::process::exit(1);
}

0 comments on commit 79f391c

Please sign in to comment.