Skip to content

Commit

Permalink
Version update: fixed raw mode and render.
Browse files Browse the repository at this point in the history
  • Loading branch information
km19809 committed Mar 5, 2021
1 parent ef05293 commit d181100
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pusher"
version = "0.2.0"
version = "0.3.0"
authors = ["km19809 <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
41 changes: 25 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,23 +81,26 @@ fn update(s: &mut Stage) -> Result<bool, &'static str> {
}

/// 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."),
}
}
}

Expand All @@ -108,7 +114,7 @@ fn render(s: &mut Stage, msg: &str) {
/// It panics when `crossterm::excute` failed.
pub fn run(args: Arguments) -> Result<(), Box<dyn error::Error>> {
//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)
Expand All @@ -129,39 +135,42 @@ pub fn run(args: Arguments) -> Result<(), Box<dyn error::Error>> {
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)
};
}
};
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.")?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit d181100

Please sign in to comment.