From 695024ae427eb5014457d42d832ba5e6ff233c64 Mon Sep 17 00:00:00 2001 From: Mroik Date: Mon, 27 Jan 2025 17:56:06 +0100 Subject: Move out of App the quote selection --- src/app.rs | 19 +++++++++++-------- src/main.rs | 31 ++++++++++++------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/src/app.rs b/src/app.rs index 7ba3da8..e5d0f25 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,7 +7,6 @@ use crossterm::{ terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand, }; -use rand::Rng; use tokio::{ spawn, sync::mpsc::{channel, Receiver, Sender}, @@ -19,23 +18,27 @@ pub const TICK_RATE: u64 = 1000 / 20; pub struct App { stdout: Stdout, - quote: Vec, pub event_tx: Sender, event_rx: Receiver, running: bool, + quote: Vec, + current: (usize, usize), } impl App { - pub fn new(quotes: &[Vec]) -> App { - let mut rng = rand::thread_rng(); - let chosen = rng.gen_range(0..quotes.len()); + pub fn new(quote: String) -> App { let (event_tx, event_rx): (Sender, Receiver) = channel(10); App { stdout: stdout(), - quote: quotes[chosen].clone(), + quote: quote + .split_whitespace() + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()) + .collect(), event_rx, event_tx, running: false, + current: (0, 0), } } @@ -75,8 +78,8 @@ impl App { async fn start_input_handler(ev: Sender, mut kill_switch: Receiver<()>) { loop { tokio::select! { - _ = handle_input(&ev) => (), - _ = kill_switch.recv() => return, + _ = handle_input(&ev) => (), + _ = kill_switch.recv() => return, } } } diff --git a/src/main.rs b/src/main.rs index 0365d5b..d10b40c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use std::{error::Error, fs::read_to_string, path::Path}; use app::App; use clap::Parser; +use rand::{thread_rng, Rng}; #[derive(Parser)] struct Args { @@ -12,17 +13,10 @@ struct Args { quote_folder: String, } -fn generate_quotes(path: &Path) -> Result>, Box> { +fn generate_quotes(path: &Path) -> Result, Box> { let mut ris = Vec::new(); if path.is_file() { - ris.push( - read_to_string(path)? - .trim() - .split_whitespace() - .filter(|s| !s.is_empty()) - .map(|s| s.to_string()) - .collect(), - ); + ris.push(read_to_string(path)?); } else { for f in path.read_dir()? { if f.is_err() { @@ -30,14 +24,7 @@ fn generate_quotes(path: &Path) -> Result>, Box> { } let v = f.unwrap().path(); if v.is_file() { - ris.push( - read_to_string(v)? - .trim() - .split_whitespace() - .filter(|s| !s.is_empty()) - .map(|s| s.to_string()) - .collect(), - ); + ris.push(read_to_string(v)?); } } } @@ -48,8 +35,14 @@ fn generate_quotes(path: &Path) -> Result>, Box> { async fn main() -> Result<(), Box> { let args = Args::parse(); let path = Path::new(&args.quote_folder); - let quotes = generate_quotes(&path).unwrap(); - let mut app = App::new("es); + let mut quotes = generate_quotes(&path).unwrap(); + let mut rng = thread_rng(); + let chosen = rng.gen_range(0..quotes.len()); + let quote = quotes.remove(chosen); + drop(quotes); + + // TODO Add more options to choose quotes + let mut app = App::new(quote); app.run().await?; return Ok(()); -- cgit v1.3