diff --git a/Cargo.lock b/Cargo.lock index a135ac8..529d0bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,7 +179,7 @@ dependencies = [ [[package]] name = "pusher" -version = "0.2.0" +version = "0.3.0" dependencies = [ "clap", "crossterm", diff --git a/Cargo.toml b/Cargo.toml index fa57361..e79bf70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pusher" -version = "0.2.0" +version = "0.3.0" authors = ["km19809 "] edition = "2018" license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index f1a595a..1bdfc12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,10 @@ use crossterm::{ event::{read, Event, KeyCode}, execute, style::Print, - terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen, SetTitle}, + terminal::{ + disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen, + LeaveAlternateScreen, SetTitle, + }, }; use std::error; use std::fs::File; @@ -78,23 +81,26 @@ fn update(s: &mut Stage) -> Result { } /// Render Stage `s` and additional message `msg`. -/// # Panics -/// (TUI) It panics when `crossterm::excute` failed. -fn render(s: &mut Stage, msg: &str) { - let stage_string = format!("{}WASD to move, Q to quit.\n{}", s, msg); +/// # Errors +/// (TUI) It return `Err` when rendering failed. +fn render(s: &mut Stage, msg: &str) -> Result<(), &'static str> { + let stage_string = format!("{}WASD to move, Q to quit.\r\n{}", s, msg); #[cfg(not(feature = "tui"))] { println!("{}", stage_string); + Ok(()) } #[cfg(feature = "tui")] { - execute!( + match execute!( io::stdout(), Clear(ClearType::All), MoveTo(0, 0), Print(stage_string) - ) - .unwrap(); + ) { + Ok(_) => Ok(()), + Err(_) => Err("Failed to render stage."), + } } } @@ -108,7 +114,7 @@ fn render(s: &mut Stage, msg: &str) { /// It panics when `crossterm::excute` failed. pub fn run(args: Arguments) -> Result<(), Box> { //load - let fnf_msg = format!("File {} not found.\n", args.filename); + let fnf_msg = format!("File {} not found.\r\n", args.filename); let mut f = File::open(args.filename).map_err(|err| { if matches!(err.kind(), io::ErrorKind::NotFound) { io::Error::new(io::ErrorKind::NotFound, fnf_msg) @@ -129,20 +135,21 @@ pub fn run(args: Arguments) -> Result<(), Box> { SetTitle("Pusher") ) .map_err(|_err| "Cannot switch screen.")?; + enable_raw_mode().map_err(|_err| "Cannot enable raw mode.")?; } //setup stage let mut s = stage::Stage::new(&contents)?; let mut message = String::new(); //update loop { - render(&mut s, &message); + render(&mut s, &message)?; message.clear(); match update(&mut s) { Ok(true) => (), Ok(false) => break, Err(msg) => { message = if cfg!(feature = "color") { - format!("\x1b[0;31mError: {}\x1b[0m\n", msg) + format!("\x1b[0;31mError: {}\x1b[0m\r\n", msg) } else { format!("Error: {}", msg) }; @@ -150,18 +157,20 @@ pub fn run(args: Arguments) -> Result<(), Box> { }; if s.is_won() { message = if cfg!(feature = "color") { - String::from("\x1b[0;33mYou Won!\x1b[0m\n") + String::from("\x1b[0;33mYou Won!\x1b[0m\r\n") } else { - String::from("You Won!\n") + String::from("You Won!\r\n") }; - render(&mut s, &message); + render(&mut s, &message)?; break; } } #[cfg(feature = "tui")] { - execute!(io::stdout(), Print("\n[Press any key to quit].\n")).unwrap(); - read().unwrap(); //Wait for input. + execute!(io::stdout(), Print("\r\n[Press any key to quit].\r\n")) + .map_err(|_err| "Failed to Print.")?; + read().map_err(|_err| "Cannot read event.")?; //Wait for input. + disable_raw_mode().map_err(|_err| "Cannot disable raw mode.")?; execute!(io::stdout(), LeaveAlternateScreen) .map_err(|_err| "Cannot return to original screen.")?; } diff --git a/src/stage.rs b/src/stage.rs index 894497b..6bdd455 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -321,7 +321,7 @@ impl fmt::Display for Stage { for c in line { write!(f, "{}", c)?; } - write!(f, "\n")?; + write!(f, "\r\n")?; } writeln!( f, @@ -335,7 +335,7 @@ impl fmt::Display for Stage { for c in line { write!(f, "{}", c)?; } - write!(f, "\n")?; + write!(f, "\r\n")?; } writeln!( f,