aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: ad66a3e419d8f3695b50aa3ac619278f24f5216f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#![allow(clippy::needless_return)]

mod app;
pub mod error;
pub mod event;
pub mod state;

use std::{
    error::Error,
    fs::read_to_string,
    io::{stdin, IsTerminal, Read},
    path::Path,
};

use app::App;
use clap::Parser;
use rand::{thread_rng, Rng};

#[derive(Parser)]
struct Args {
    quote: String,
}

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)?);
    } else {
        for f in path.read_dir()? {
            if f.is_err() {
                continue;
            }
            let v = f.unwrap().path();
            if v.is_file() {
                ris.push(read_to_string(v)?);
            }
        }
    }
    return Ok(ris);
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let quote = if !stdin().is_terminal() {
        let mut b = Vec::new();
        stdin().read_to_end(&mut b).unwrap();
        String::from_utf8(b).unwrap()
    } else {
        let args = Args::parse();
        let path = Path::new(&args.quote);
        let mut quotes = generate_quotes(path).unwrap();
        let mut rng = thread_rng();
        let chosen = rng.gen_range(0..quotes.len());
        quotes.remove(chosen)
    };

    // TODO Add more options to choose quotes
    let mut app = App::new(&quote);

    app.start().await?;
    return Ok(());
}
XMR address: 854DmXNrxULU3ZFJVs4Wc8PFhbq29RhqHhY8W6cdWrtFN3qmooKyyeYPcDzZTNRxphhJ5UzASQfAdEMwSteVqymk28aLhqj