From 72af5df56fd08709feea4832d8edc4a21d4776b7 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 1 Oct 2022 20:49:05 +0200 Subject: [PATCH] Reorganize main function --- rust/cli_player/src/constants.rs | 3 +++ rust/cli_player/src/lib.rs | 1 + rust/cli_player/src/main.rs | 20 ++++++++++--------- .../src/ui_screens/select_play_mode.rs | 6 +++--- 4 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 rust/cli_player/src/constants.rs diff --git a/rust/cli_player/src/constants.rs b/rust/cli_player/src/constants.rs new file mode 100644 index 0000000..09cb3b6 --- /dev/null +++ b/rust/cli_player/src/constants.rs @@ -0,0 +1,3 @@ +use std::time::Duration; + +pub const TICK_RATE: Duration = Duration::from_millis(250); diff --git a/rust/cli_player/src/lib.rs b/rust/cli_player/src/lib.rs index 0a9c1b1..c53eb9f 100644 --- a/rust/cli_player/src/lib.rs +++ b/rust/cli_player/src/lib.rs @@ -1,3 +1,4 @@ pub mod cli_args; +pub mod constants; pub mod server; pub mod ui_screens; diff --git a/rust/cli_player/src/main.rs b/rust/cli_player/src/main.rs index 8931974..95be3b2 100644 --- a/rust/cli_player/src/main.rs +++ b/rust/cli_player/src/main.rs @@ -1,8 +1,6 @@ use std::error::Error; use std::io; -use std::time::Duration; -use cli_player::ui_screens::select_play_mode; use crossterm::event::DisableMouseCapture; use crossterm::event::EnableMouseCapture; use crossterm::execute; @@ -10,9 +8,17 @@ use crossterm::terminal::EnterAlternateScreen; use crossterm::terminal::LeaveAlternateScreen; use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use env_logger::Env; -use tui::backend::CrosstermBackend; +use tui::backend::{Backend, CrosstermBackend}; use tui::Terminal; + use cli_player::server::start_server_if_missing; +use cli_player::ui_screens::select_play_mode; + +async fn run_app(terminal: &mut Terminal) -> Result<(), Box> { + let res = select_play_mode::select_play_mode(terminal)?; + println!("selected play mode: {:?}", res); + Ok(()) +} #[tokio::main] pub async fn main() -> Result<(), Box> { @@ -28,9 +34,7 @@ pub async fn main() -> Result<(), Box> { let mut terminal = Terminal::new(backend)?; // create app and run it - let tick_rate = Duration::from_millis(250); - - let res = select_play_mode::select_play_mode(&mut terminal, tick_rate); + let res = run_app(&mut terminal).await; // restore terminal disable_raw_mode()?; @@ -41,11 +45,9 @@ pub async fn main() -> Result<(), Box> { )?; terminal.show_cursor()?; - if let Err(err) = &res { + if let Err(err) = res { println!("{:?}", err) } - println!("selected play mode: {:?}", res.unwrap()); - Ok(()) } diff --git a/rust/cli_player/src/ui_screens/select_play_mode.rs b/rust/cli_player/src/ui_screens/select_play_mode.rs index adda484..78a159b 100644 --- a/rust/cli_player/src/ui_screens/select_play_mode.rs +++ b/rust/cli_player/src/ui_screens/select_play_mode.rs @@ -1,6 +1,7 @@ use std::io; use std::time::{Duration, Instant}; +use crate::constants::TICK_RATE; use crate::ui_screens::utils::centered_rect_size; use crossterm::event; use crossterm::event::{Event, KeyCode}; @@ -47,7 +48,6 @@ struct SelectPlayModeScreen { pub fn select_play_mode( terminal: &mut Terminal, - tick_rate: Duration, ) -> io::Result { let mut model = SelectPlayModeScreen::default(); @@ -56,7 +56,7 @@ pub fn select_play_mode( model.state.select(Some(model.curr_selection)); terminal.draw(|f| ui(f, &mut model))?; - let timeout = tick_rate + let timeout = TICK_RATE .checked_sub(last_tick.elapsed()) .unwrap_or_else(|| Duration::from_secs(0)); @@ -73,7 +73,7 @@ pub fn select_play_mode( model.curr_selection %= AVAILABLE_PLAY_MODES.len(); } } - if last_tick.elapsed() >= tick_rate { + if last_tick.elapsed() >= TICK_RATE { last_tick = Instant::now(); } }