aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMroik <mroik@delayed.space>2025-01-27 17:56:06 +0100
committerMroik <mroik@delayed.space>2025-02-01 19:33:03 +0100
commit695024ae427eb5014457d42d832ba5e6ff233c64 (patch)
tree7c629c58621e482617754b0b25334c6d568bb0d2
parent5bd36b139d8ba07bce7edb8828beec3f114b3271 (diff)
Move out of App the quote selection
-rw-r--r--src/app.rs19
-rw-r--r--src/main.rs31
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<String>,
pub event_tx: Sender<Event>,
event_rx: Receiver<Event>,
running: bool,
+ quote: Vec<String>,
+ current: (usize, usize),
}
impl App {
- pub fn new(quotes: &[Vec<String>]) -> 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<Event>, Receiver<Event>) = 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<Event>, 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<Vec<Vec<String>>, Box<dyn Error>> {
+fn generate_quotes(path: &Path) -> Result<Vec<String>, Box<dyn Error>> {
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<Vec<Vec<String>>, Box<dyn Error>> {
}
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<Vec<Vec<String>>, Box<dyn Error>> {
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let path = Path::new(&args.quote_folder);
- let quotes = generate_quotes(&path).unwrap();
- let mut app = App::new(&quotes);
+ 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(());
XMR address: 854DmXNrxULU3ZFJVs4Wc8PFhbq29RhqHhY8W6cdWrtFN3qmooKyyeYPcDzZTNRxphhJ5UzASQfAdEMwSteVqymk28aLhqj