aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 7be57c6a8f9d47d2f181bca9f7251da7e0a207dd (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#![allow(clippy::needless_return)]

mod app;
pub mod config;
pub mod error;
pub mod input;
pub mod state;

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

use anyhow::Result;
use app::App;
use clap::{Args, Parser};
use rand::Rng;

use crate::config::{Quote, get_quoter};

#[derive(Parser)]
struct Cli {
    /// Turns all text into lowercase (NOOB mode)
    #[arg(short, long)]
    lower: bool,
    #[command(flatten)]
    quote_size: QuoteSize,
    quote: Option<String>,
}

#[derive(Args)]
#[group(required = false, multiple = false)]
struct QuoteSize {
    #[arg(short, long)]
    short: bool,
    #[arg(short, long)]
    medium: bool,
    #[arg(short, long)]
    long: bool,
    #[arg(short, long)]
    huge: bool,
}

fn generate_quotes(path: &Path) -> Result<Vec<String>> {
    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<()> {
    let args = Cli::parse();
    let mut quote = if !stdin().is_terminal() {
        let mut b = Vec::new();
        stdin().read_to_end(&mut b).unwrap();
        Quote {
            text: String::from_utf8(b)?,
            source: None,
        }
    } else if let Some(q) = &args.quote {
        let path = Path::new(q);
        let mut quotes = generate_quotes(path).unwrap();
        let mut rng = rand::rand_core::UnwrapErr(rand::rngs::SysRng);
        let chosen = rng.next_u64() as usize;
        Quote {
            text: quotes.remove(chosen),
            source: None,
        }
    } else {
        println!("Loading available quotes...");
        let mut quoter = get_quoter()?;
        if args.quote_size.short {
            quoter.get_short()?;
        }
        if args.quote_size.medium {
            quoter.get_medium()?;
        }
        if args.quote_size.long {
            quoter.get_long()?;
        }
        if args.quote_size.huge {
            quoter.get_huge()?;
        }
        quoter.get_random()?
    };

    if args.lower {
        quote.text = quote.text.to_lowercase();
    }

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

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