Reorganize main function
This commit is contained in:
parent
7a06feb7cf
commit
72af5df56f
3
rust/cli_player/src/constants.rs
Normal file
3
rust/cli_player/src/constants.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub const TICK_RATE: Duration = Duration::from_millis(250);
|
@ -1,3 +1,4 @@
|
|||||||
pub mod cli_args;
|
pub mod cli_args;
|
||||||
|
pub mod constants;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod ui_screens;
|
pub mod ui_screens;
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use cli_player::ui_screens::select_play_mode;
|
|
||||||
use crossterm::event::DisableMouseCapture;
|
use crossterm::event::DisableMouseCapture;
|
||||||
use crossterm::event::EnableMouseCapture;
|
use crossterm::event::EnableMouseCapture;
|
||||||
use crossterm::execute;
|
use crossterm::execute;
|
||||||
@ -10,9 +8,17 @@ use crossterm::terminal::EnterAlternateScreen;
|
|||||||
use crossterm::terminal::LeaveAlternateScreen;
|
use crossterm::terminal::LeaveAlternateScreen;
|
||||||
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
|
||||||
use env_logger::Env;
|
use env_logger::Env;
|
||||||
use tui::backend::CrosstermBackend;
|
use tui::backend::{Backend, CrosstermBackend};
|
||||||
use tui::Terminal;
|
use tui::Terminal;
|
||||||
|
|
||||||
use cli_player::server::start_server_if_missing;
|
use cli_player::server::start_server_if_missing;
|
||||||
|
use cli_player::ui_screens::select_play_mode;
|
||||||
|
|
||||||
|
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> {
|
||||||
|
let res = select_play_mode::select_play_mode(terminal)?;
|
||||||
|
println!("selected play mode: {:?}", res);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
pub async fn main() -> Result<(), Box<dyn Error>> {
|
pub async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
@ -28,9 +34,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let mut terminal = Terminal::new(backend)?;
|
let mut terminal = Terminal::new(backend)?;
|
||||||
|
|
||||||
// create app and run it
|
// create app and run it
|
||||||
let tick_rate = Duration::from_millis(250);
|
let res = run_app(&mut terminal).await;
|
||||||
|
|
||||||
let res = select_play_mode::select_play_mode(&mut terminal, tick_rate);
|
|
||||||
|
|
||||||
// restore terminal
|
// restore terminal
|
||||||
disable_raw_mode()?;
|
disable_raw_mode()?;
|
||||||
@ -41,11 +45,9 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
)?;
|
)?;
|
||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = &res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err)
|
println!("{:?}", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("selected play mode: {:?}", res.unwrap());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use crate::constants::TICK_RATE;
|
||||||
use crate::ui_screens::utils::centered_rect_size;
|
use crate::ui_screens::utils::centered_rect_size;
|
||||||
use crossterm::event;
|
use crossterm::event;
|
||||||
use crossterm::event::{Event, KeyCode};
|
use crossterm::event::{Event, KeyCode};
|
||||||
@ -47,7 +48,6 @@ struct SelectPlayModeScreen {
|
|||||||
|
|
||||||
pub fn select_play_mode<B: Backend>(
|
pub fn select_play_mode<B: Backend>(
|
||||||
terminal: &mut Terminal<B>,
|
terminal: &mut Terminal<B>,
|
||||||
tick_rate: Duration,
|
|
||||||
) -> io::Result<SelectPlayModeResult> {
|
) -> io::Result<SelectPlayModeResult> {
|
||||||
let mut model = SelectPlayModeScreen::default();
|
let mut model = SelectPlayModeScreen::default();
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ pub fn select_play_mode<B: Backend>(
|
|||||||
model.state.select(Some(model.curr_selection));
|
model.state.select(Some(model.curr_selection));
|
||||||
terminal.draw(|f| ui(f, &mut model))?;
|
terminal.draw(|f| ui(f, &mut model))?;
|
||||||
|
|
||||||
let timeout = tick_rate
|
let timeout = TICK_RATE
|
||||||
.checked_sub(last_tick.elapsed())
|
.checked_sub(last_tick.elapsed())
|
||||||
.unwrap_or_else(|| Duration::from_secs(0));
|
.unwrap_or_else(|| Duration::from_secs(0));
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ pub fn select_play_mode<B: Backend>(
|
|||||||
model.curr_selection %= AVAILABLE_PLAY_MODES.len();
|
model.curr_selection %= AVAILABLE_PLAY_MODES.len();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if last_tick.elapsed() >= tick_rate {
|
if last_tick.elapsed() >= TICK_RATE {
|
||||||
last_tick = Instant::now();
|
last_tick = Instant::now();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user