git_hist/app/
mod.rs

1use anyhow::Result;
2use std::panic;
3
4mod commit;
5mod controller;
6mod dashboard;
7mod diff;
8mod git;
9mod history;
10mod state;
11mod terminal;
12
13use crate::args::Args;
14use dashboard::Dashboard;
15use state::State;
16use terminal::Terminal;
17
18pub fn run(args: Args) -> Result<()> {
19    let repo = git::get_repository()?;
20    let history = git::get_history(&args.file_path, &repo, &args)?;
21
22    terminal::initialize()?;
23
24    let default_hook = panic::take_hook();
25    panic::set_hook(Box::new(move |panic_info| {
26        let _ = exit();
27        default_hook(panic_info);
28    }));
29
30    (|| -> Result<()> {
31        let mut terminal = Terminal::new()?;
32        let mut current_state = State::first(&history, &terminal, &args);
33        let dashboard = Dashboard::new(&current_state);
34        dashboard.draw(&mut terminal)?;
35
36        while let Some(next_state) = controller::poll_next_event(current_state, &history)? {
37            current_state = next_state;
38            let dashboard = Dashboard::new(&current_state);
39            dashboard.draw(&mut terminal)?;
40        }
41
42        Ok(())
43    })()
44    .map_err(|e| {
45        let _ = exit();
46        e
47    })?;
48
49    exit()
50}
51
52fn exit() -> Result<()> {
53    terminal::terminate()?;
54    Ok(())
55}