summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: f66e192c6c76c979da72b2a82e0dd3c7904500c8 (plain)
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
#![allow(dead_code, unused_imports, unused_mut, unused_variables)]
mod cli;
mod config;
mod error;
mod stackexchange;
mod term;
mod tui;
mod utils;

use crossterm::style::Color;
use error::Error;
use lazy_static::lazy_static;
use minimad::mad_inline;
use stackexchange::{LocalStorage, StackExchange};
use term::mk_print_error;
use termimad::{CompoundStyle, MadSkin};

fn main() {
    let mut skin = MadSkin::default();
    // TODO style configuration
    skin.inline_code = CompoundStyle::with_fg(Color::Cyan);
    skin.code_block.set_fgbg(Color::Cyan, termimad::gray(20));
    let mut print_error = mk_print_error(&skin);
    (|| {
        let opts = cli::get_opts()?;
        let config = opts.config;
        let site = &config.site;
        let mut ls = LocalStorage::new()?;

        if let Some(key) = opts.set_api_key {
            config::set_api_key(key)?;
        }

        if opts.update_sites {
            ls.update_sites()?;
        }

        if opts.list_sites {
            let sites = ls.sites()?;
            let mut md = String::new();
            md.push_str("|:-:|:-:|\n");
            md.push_str("|Site Code|Site URL|\n");
            md.push_str("|-:|:-|\n");
            for s in sites.iter() {
                md.push_str(&format!("|{}|{}\n", s.api_site_parameter, s.site_url));
            }
            md.push_str("|-\n");
            termimad::print_text(&md);
            return Ok(());
        }

        if !ls.validate_site(site)? {
            print_error!(skin, "$0 is not a valid StackExchange site.\n\n", site)?;
            // TODO should only use inline for single lines; use termimad::text stuff
            print_notice!(
                skin,
                "If you think this is incorrect, try running\n\
                    ```\n\
                    so --update-sites\n\
                    ```\n\
                    to update the cached site listing. You can also run `so --list-sites` \
                    to list all available sites.",
            )?;
            return Ok(());
        }

        if let Some(q) = opts.query {
            //let se = StackExchange::new(config);
            // TODO async
            //let qs = se.search(&q)?;
            //let ans = que.first().ok_or(Error::NoResults)?.answers.first().expect(
            //"StackExchange returned a question with no answers; \
            //this shouldn't be possible!",
            //);
            // TODO eventually do this in the right place, e.g. abstract out md parser, write benches, & do within threads
            // TODO do the below for --lucky with option to continue
            //let md = ans.body.replace("<kbd>", "**[").replace("</kbd>", "]**");
            //skin.print_text(&md);
            use crate::stackexchange::{Answer, Question};
            tui::run(vec![Question {
                id: 42,
                score: 323,
                title: "How do I exit Vim?".to_string(),
                body: "yo this be my problem dawg but don't say **do** `this`".to_string(),
                answers: vec![
                    Answer {
                        id: 422,
                        score: -4,
                        body: "# bad\nthis is my bad answer".to_string(),
                        is_accepted: false,
                    },
                    Answer {
                        id: 423,
                        score: 23,
                        body: "this is a *good* answer tho".to_string(),
                        is_accepted: true,
                    },
                ],
            }])?;
        }

        Ok(())
    })()
    .or_else(|e: Error| {
        print_error(&e.to_string())?;
        match e {
            Error::EmptySites => {
                print_notice!(skin, "This can likely be fixed by `so --update-sites`.")
            }
            _ => Ok(()),
        }
    })
    .unwrap();
}