diff --git a/src/app.rs b/src/app.rs index e37a056..32ed261 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,12 +3,13 @@ pub use cell::Cell; use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind}; use ratatui::{backend::Backend, Terminal}; pub use shapes::HandleError; -pub use std::str::FromStr; -use std::{io, time::Duration}; +use std::{io, str::FromStr, time::Duration}; pub use universe::Universe; /// Default poll duration const DEF_DUR: Duration = Duration::from_millis(400); +/// Pause duration: a day +const PAUSE: Duration = Duration::from_secs(60 * 60 * 24); mod area; mod cell; @@ -27,7 +28,6 @@ pub struct App { universe: Universe, i: usize, pub poll_t: Duration, - pub paused: bool, pub area: Area, } impl Default for App { @@ -37,7 +37,6 @@ impl Default for App { universe: Universe::default(), i: 0, poll_t: DEF_DUR, - paused: false, available_universes: shapes::all(), } } @@ -55,10 +54,12 @@ impl App { universe: available_universes[0].clone(), i: 0, poll_t, - paused: false, available_universes, } } + pub fn paused(&self) -> bool { + self.poll_t == PAUSE + } pub fn len(&self) -> usize { self.available_universes.len() + shapes::N } @@ -83,13 +84,12 @@ impl App { // } pub fn play_pause(&mut self, prev_poll_t: &mut Duration) { - if self.paused { + if self.paused() { self.poll_t = *prev_poll_t; } else { *prev_poll_t = self.poll_t; - self.poll_t = Duration::MAX; + self.poll_t = PAUSE; } - self.paused = !self.paused; } pub fn restart(&mut self) { let univ = self.get(); @@ -101,7 +101,7 @@ impl App { } pub fn faster(&mut self, big: bool) { - if !self.paused { + if !self.paused() { let div = if big { 2 } else { 5 }; self.poll_t = self .poll_t @@ -110,7 +110,7 @@ impl App { } } pub fn slower(&mut self, big: bool) { - if !self.paused { + if !self.paused() { let div = if big { 2 } else { 5 }; self.poll_t = self .poll_t diff --git a/src/app/ui.rs b/src/app/ui.rs index fae5423..41dddb9 100644 --- a/src/app/ui.rs +++ b/src/app/ui.rs @@ -55,7 +55,7 @@ pub fn ui(f: &mut Frame, app: &mut App) { let current_keys_hint = "[q]uit, [r]estart, pause: [ ], nav: vim/arrows".yellow(); let poll_t = { - if let std::time::Duration::MAX = app.poll_t { + if let super::PAUSE = app.poll_t { "paused".into() } else { format!("Poll time: {:.0?}", app.poll_t) diff --git a/src/main.rs b/src/main.rs index c846562..2abd685 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,20 @@ use std::{io::Read, str::FromStr}; pub mod app; fn main() -> Result<(), Box> { + let arg_universes = parse_args()?; + + let mut app = App::default().with_universes(arg_universes); + + let mut terminal = ratatui::try_init()?; + + let res = app.run(&mut terminal); + + ratatui::try_restore()?; + + Ok(res?) +} + +fn parse_args() -> Result, Box> { let args = std::env::args().skip(1).collect::>(); if args.contains(&"-h".into()) || args.contains(&"--help".into()) { println!( @@ -13,9 +27,8 @@ USAGE: cgol-tui [,...] where is either a .cells file, or - for stdin" ); - std::process::exit(1); + std::process::exit(0); } - let piped_universe = { let mut univ = String::new(); if args.len() == 1 && args[0] == "-" { @@ -33,16 +46,5 @@ where is either a .cells file, or - for stdin" .flat_map(|s| Universe::from_str(&s)) .collect::>(); - let mut app = App::default().with_universes([universes, piped_universe].concat()); - - let mut terminal = ratatui::try_init()?; - - let res = app.run(&mut terminal); - - ratatui::try_restore()?; - - // if any error has occured while executing, print it in cooked mode - res.inspect_err(|e| println!("error: {e:?}"))?; - - Ok(()) + Ok([universes, piped_universe].concat()) }