aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: cce7334a850a9eaefa50d4894f7b96502e6092d7 (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
110
111
112
113
114
115
#![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::Parser;
use rand::{Rng, thread_rng};

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

#[derive(Parser)]
struct Args {
    /// Turns all text into lowercase (NOOB mode)
    #[arg(short, long)]
    lower: bool,
    #[arg(short, long)]
    short: bool,
    #[arg(short, long)]
    medium: bool,
    #[arg(short, long)]
    long: bool,
    #[arg(short, long)]
    huge: bool,
    quote: Option<String>,
}

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 = Args::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 = thread_rng();
        let chosen = rng.gen_range(0..quotes.len());
        Quote {
            text: quotes.remove(chosen),
            source: None,
        }
    } else {
        let mut specifier = 0;
        if args.short {
            specifier += 1;
        }
        if args.medium {
            specifier += 1;
        }
        if args.long {
            specifier += 1;
        }
        if args.huge {
            specifier += 1;
        }
        if specifier > 1 {
            panic!("You can't use more than one quote length specifier");
        }
        let mut quoter = get_quoter()?;
        if args.short {
            quoter.get_short()?
        } else if args.medium {
            quoter.get_medium()?
        } else if args.long {
            quoter.get_long()?
        } else if args.huge {
            quoter.get_huge()?
        } else {
            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