summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 0f1e81b131a29d0b5ab4ec52f5353e48f7eb47b6 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// modules
pub mod config;
pub mod hn_client;
pub mod keybindings;
pub mod prelude;
pub mod utils;
pub mod view;

use clap::*;
use prelude::*;

fn set_up_global_callbacks(s: &mut Cursive, client: &hn_client::HNClient) {
    s.clear_global_callbacks(Event::CtrlChar('c'));

    let global_keymap = get_global_keymap().clone();

    // .............................................................
    // global shortcuts for switching between different Story Views
    // .............................................................

    s.set_on_post_event(global_keymap.goto_front_page_view, {
        let client = client.clone();
        move |s| {
            story_view::add_story_view_layer(
                s,
                &client,
                "front_page",
                false,
                0,
                hn_client::StoryNumericFilters::default(),
                false,
            );
        }
    });

    s.set_on_post_event(global_keymap.goto_all_stories_view, {
        let client = client.clone();
        move |s| {
            story_view::add_story_view_layer(
                s,
                &client,
                "story",
                true,
                0,
                hn_client::StoryNumericFilters::default(),
                false,
            );
        }
    });

    s.set_on_post_event(global_keymap.goto_ask_hn_view, {
        let client = client.clone();
        move |s| {
            story_view::add_story_view_layer(
                s,
                &client,
                "ask_hn",
                true,
                0,
                hn_client::StoryNumericFilters::default(),
                false,
            );
        }
    });

    s.set_on_post_event(global_keymap.goto_show_hn_view, {
        let client = client.clone();
        move |s| {
            story_view::add_story_view_layer(
                s,
                &client,
                "show_hn",
                true,
                0,
                hn_client::StoryNumericFilters::default(),
                false,
            );
        }
    });

    s.set_on_post_event(global_keymap.goto_jobs_view, {
        let client = client.clone();
        move |s| {
            story_view::add_story_view_layer(
                s,
                &client,
                "job",
                true,
                0,
                hn_client::StoryNumericFilters::default(),
                false,
            );
        }
    });

    // custom navigation shortcuts
    let custom_keymap = get_custom_keymap();
    custom_keymap
        .custom_view_navigation
        .iter()
        .for_each(|data| {
            let client = client.clone();
            s.set_on_post_event(data.key.clone(), move |s| {
                story_view::add_story_view_layer(
                    s,
                    &client,
                    &data.tag,
                    data.by_date,
                    0,
                    data.numeric_filters,
                    true,
                )
            });
        });

    // .........................................
    // end of navigation shortcuts for StoryView
    // .........................................

    s.set_on_post_event(global_keymap.goto_previous_view, |s| {
        if s.screen_mut().len() > 1 {
            s.pop_layer();
        }
    });

    s.set_on_post_event(global_keymap.goto_search_view, {
        let client = client.clone();
        move |s| {
            search_view::add_search_view_layer(s, &client);
        }
    });

    s.set_on_post_event(global_keymap.open_help_dialog, |s| {
        s.add_layer(DefaultHelpView::construct_help_view())
    });

    s.set_on_post_event(global_keymap.quit, |s| s.quit());
}

fn load_config(config_file_path: Option<&str>) {
    // if no config file is specified, use the default value
    // at $HOME/.config/hn-tui.toml
    let config_file_path = match config_file_path {
        None => match dirs_next::home_dir() {
            None => None,
            Some(path) => Some(format!("{}/.config/hn-tui.toml", path.to_str().unwrap())),
        },
        Some(path) => Some(path.to_string()),
    };

    let config = match config_file_path {
        None => config::Config::default(),
        Some(config_file_path) => match config::Config::from_config_file(&config_file_path) {
            Err(err) => {
                error!(
                    "failed to load the application config from the file {}: {:#?} \
                     \n...Use the default configurations instead",
                    config_file_path, err
                );
                config::Config::default()
            }
            Ok(config) => config,
        },
    };

    init_config(config);
}

fn run() {
    let mut s = cursive::default();

    // update cursive's default theme
    let config_theme = get_config().theme.clone();
    s.update_theme(|theme| {
        config_theme.update_theme(theme);
    });

    let client = hn_client::HNClient::new().unwrap();
    set_up_global_callbacks(&mut s, &client);

    story_view::add_story_view_layer(
        &mut s,
        &client,
        "front_page",
        false,
        0,
        hn_client::StoryNumericFilters::default(),
        false,
    );

    // use buffered_backend to fix the flickering issue
    // when using cursive with crossterm_backend
    // (https://github.com/gyscos/Cursive/issues/142)
    let crossterm_backend = backends::crossterm::Backend::init().unwrap();
    let buffered_backend = Box::new(cursive_buffered_backend::BufferedBackend::new(
        crossterm_backend,
    ));
    let mut app = CursiveRunner::new(s, buffered_backend);

    app.run();
}

fn main() {
    env_logger::init();

    // parse command line arguments
    let matches = App::new("hackernews-tui")
        .version("0.6.1")
        .author("Thang Pham <phamducthang1234@gmail>")
        .arg(
            Arg::with_name("config")
                .short("c")
                .long("config")
                .value_name("FILE")
                .help("Path to the application's config file (default: ~/.config/hn-tui.toml)")
                .next_line_help(true),
        )
        .arg(
            Arg::with_name("example-config")
                .long("example-config")
                .help("Prints the example configurations"),
        )
        .get_matches();

    if matches.is_present("example-config") {
        println!("{}", include_str!("hn-tui-default.toml"));
    } else {
        load_config(matches.value_of("config"));
        run();
    }
}