From 79f391cc3a87e8a5145093e9e2b2ba11017e712e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Macio=C5=82ek?= Date: Mon, 4 Mar 2024 12:32:58 +0100 Subject: [PATCH] die method added --- src/main.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index b02e9be..4eaf3a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,24 @@ -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 = 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 { @@ -21,9 +26,13 @@ fn main() { if c.code == KeyCode::Char('q') { break; } - } else { - println!("No input\r"); } } let _ = disable_raw_mode(); } + +fn die>(message: S) { + let _ = disable_raw_mode(); + eprintln!("{}: {}", message.into(), std::io::Error::last_os_error()); + std::process::exit(1); +}