Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor #24

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -37,7 +37,6 @@ impl Default for App {
universe: Universe::default(),
i: 0,
poll_t: DEF_DUR,
paused: false,
available_universes: shapes::all(),
}
}
Expand 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
}
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 16 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ use std::{io::Read, str::FromStr};
pub mod app;

fn main() -> Result<(), Box<dyn std::error::Error>> {
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<Vec<Universe>, Box<dyn std::error::Error>> {
let args = std::env::args().skip(1).collect::<Vec<_>>();
if args.contains(&"-h".into()) || args.contains(&"--help".into()) {
println!(
Expand All @@ -13,9 +27,8 @@ USAGE: cgol-tui [<pattern>,...]

where <pattern> 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] == "-" {
Expand All @@ -33,16 +46,5 @@ where <pattern> is either a .cells file, or - for stdin"
.flat_map(|s| Universe::from_str(&s))
.collect::<Vec<_>>();

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())
}
Loading