Skip to content

Commit

Permalink
fix(code): faster, slower fn
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeSchmied committed Feb 25, 2024
1 parent bb882ae commit e243468
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 45 deletions.
47 changes: 42 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/// Default poll duration
pub const DEF_DUR: Duration = Duration::from_millis(400);
/// Default Width and Height
pub const DEF_WH: u32 = 32;
/// Help message
pub const HELP: &str = r#"Blocking poll() & non-blocking read()
- Prints current state of Conway's Game of Life if there's no event
- Use Esc or `q` to quit
- `j`, `k`: decreasing, increasing speed
- press Space to pause, play
- hit `n` to switch to next shape
- and now, press Enter to continue
"#;

/// information about one `Cell`: either `Dead` or `Alive`
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cell {
Expand Down Expand Up @@ -152,7 +166,7 @@ impl Universe {
}
}

use std::fmt;
use std::{fmt, time::Duration};

impl fmt::Display for Universe {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down Expand Up @@ -386,19 +400,23 @@ pub mod kmaps {
]
}

pub fn restart() -> Vec<Event> {
vec![ch_to_event('r')]
}

pub fn reset() -> Vec<Event> {
vec![Event::Key(KeyCode::Char('R').into())]
vec![ch_to_event('R')]
}

pub fn next() -> Vec<Event> {
vec![Event::Key(KeyCode::Char('n').into())]
vec![ch_to_event('n')]
}

pub fn bigger() -> Vec<Event> {
vec![Event::Key(KeyCode::Char('+').into())]
vec![ch_to_event('+')]
}
pub fn smaller() -> Vec<Event> {
vec![Event::Key(KeyCode::Char('-').into())]
vec![ch_to_event('-')]
}

// mouse-bullshit, no-need
Expand All @@ -422,3 +440,22 @@ pub mod kmaps {
// - execute!(io::stdout(), (Enable/Disable)MouseCapture)
// - Cursor::position()
}

pub fn faster(poll_t: &mut Duration, _big: bool) {
// if _big {
// *poll_t -= *poll_t * 10;
// *poll_t = poll_t.checked_sub(*poll_t * 2).unwrap_or(*poll_t);
// }
*poll_t = poll_t
.checked_sub(poll_t.checked_div(10).unwrap_or(DEF_DUR))
.unwrap_or(DEF_DUR);
}

pub fn slower(poll_t: &mut Duration, _big: bool) {
// if _big {
// *poll_t = poll_t.checked_add(*poll_t * 2).unwrap_or(*poll_t);
// }
*poll_t = poll_t
.checked_add(poll_t.checked_div(10).unwrap_or(DEF_DUR))
.unwrap_or(DEF_DUR);
}
87 changes: 47 additions & 40 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use conways_game_of_life_cli_rs::*;

const DEFAULT_DUR: Duration = Duration::from_millis(400);
const HELP: &str = r#"Blocking poll() & non-blocking read()
- Prints current state of Conway's Game of Life if there's no event
- Use Esc or `q` to quit
- `j`, `k`: decreasing, increasing speed
- press Space to pause, play
- hit `n` to switch to next shape
- and now, press Enter to continue
"#;

fn main() -> io::Result<()> {
println!("{}", HELP);
std::io::stdin().read_line(&mut String::new()).unwrap();
Expand Down Expand Up @@ -40,20 +30,17 @@ use std::{
};

fn print_events() -> io::Result<()> {
execute!(
io::stdout(),
EnterAlternateScreen /*, EnableMouseCapture*/
)?;
execute!(io::stdout(), EnterAlternateScreen)?;

// widht and height, as they're the same
let mut wh = 38;
let mut wh = DEF_WH;

let mut i: usize = 0;
let mut universe = get_shape(wh, i).unwrap();

let mut poll_t = DEFAULT_DUR;
let mut poll_t = DEF_DUR;
let mut paused = false;
let mut prev_poll_t = DEFAULT_DUR;
let mut prev_poll_t = poll_t;

loop {
// Wait up to `poll_t` for another event
Expand All @@ -65,37 +52,50 @@ fn print_events() -> io::Result<()> {
println!("Quitting...\r");
break;
} else if kmaps::slower().contains(&event) {
if poll_t < Duration::from_millis(40) {
poll_t = poll_t
.checked_add(Duration::from_millis(1))
.unwrap_or(DEFAULT_DUR);
} else {
poll_t = poll_t
.checked_add(Duration::from_millis(10))
.unwrap_or(DEFAULT_DUR);
// if poll_t < Duration::from_millis(40) {
// poll_t = poll_t
// .checked_add(Duration::from_millis(1))
// .unwrap_or(DEFAULT_DUR);
// } else {
// poll_t = poll_t
// .checked_add(Duration::from_millis(10))
// .unwrap_or(DEFAULT_DUR);
// }
if !paused {
slower(&mut poll_t, false);
}
// queue!(io::stdout(), Print("Poll time is now"))?;
println!("poll time is now: {:?}\r", poll_t);
} else if kmaps::faster().contains(&event) {
if poll_t < Duration::from_millis(40) {
poll_t = poll_t
.checked_sub(Duration::from_millis(1))
.unwrap_or(DEFAULT_DUR);
} else {
poll_t = poll_t
.checked_sub(Duration::from_millis(10))
.unwrap_or(DEFAULT_DUR);
// if poll_t < Duration::from_millis(40) {
// poll_t = poll_t
// .checked_sub(Duration::from_millis(1))
// .unwrap_or(DEFAULT_DUR);
// } else {
// poll_t = poll_t
// .checked_sub(Duration::from_millis(10))
// .unwrap_or(DEFAULT_DUR);
// }
if !paused {
faster(&mut poll_t, false);
}

println!("poll time is now: {:?}\r", poll_t);
} else if kmaps::slower_big().contains(&event) {
poll_t = poll_t
.checked_add(Duration::from_millis(100))
.unwrap_or(Duration::from_millis(400));
// poll_t = poll_t
// .checked_add(Duration::from_millis(100))
// .unwrap_or(Duration::from_millis(400));
if !paused {
slower(&mut poll_t, true);
}
println!("poll time is now: {:?}\r", poll_t);
} else if kmaps::faster_big().contains(&event) {
poll_t = poll_t
.checked_sub(Duration::from_millis(100))
.unwrap_or(Duration::from_millis(400));
// poll_t = poll_t
// .checked_sub(Duration::from_millis(100))
// .unwrap_or(Duration::from_millis(400));
if !paused {
faster(&mut poll_t, true);
}
println!("poll time is now: {:?}\r", poll_t);
} else if kmaps::play_pause().contains(&event) {
if paused {
Expand All @@ -107,7 +107,7 @@ fn print_events() -> io::Result<()> {
poll_t = Duration::MAX;
}
paused = !paused;
} else if kmaps::reset().contains(&event) {
} else if kmaps::restart().contains(&event) {
universe = get_shape(wh, i).unwrap();
} else if kmaps::next().contains(&event) {
if i + 1 != SHAPES_N as usize {
Expand All @@ -134,6 +134,13 @@ fn print_events() -> io::Result<()> {
} else {
eprintln!("Couldn't make larger");
}
} else if kmaps::reset().contains(&event) {
i = 0;
paused = false;
wh = DEF_WH;
poll_t = DEF_DUR;
prev_poll_t = poll_t;
universe = get_shape(wh, i).unwrap();
} else {
eprintln!("Unknown event: {:?}", event);
}
Expand Down

0 comments on commit e243468

Please sign in to comment.