aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 1d9efb44711c9d262fdfa70bf3d17053686a8e98 (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
#![allow(unused_must_use)]

mod app;
mod command;
mod habit;
mod theme;
mod utils;
mod views;

use crate::app::App;
use crate::command::{open_command_window, Command};
use crate::utils::{load_configuration_file, AppConfig};

use clap::{App as ClapApp, Arg};
use cursive::termion;
use cursive::views::{LinearLayout, NamedView};
use lazy_static::lazy_static;

lazy_static! {
    pub static ref CONFIGURATION: AppConfig = load_configuration_file();
}

fn main() {
    let matches = ClapApp::new(env!("CARGO_PKG_NAME"))
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .arg(
            Arg::with_name("command")
                .short("c")
                .long("command")
                .takes_value(true)
                .value_name("CMD")
                .help("run a dijo command"),
        )
        .get_matches();
    if let Some(c) = matches.value_of("command") {
        let command = Command::from_string(c);
        match command {
            Ok(Command::TrackUp(_)) | Ok(Command::TrackDown(_)) => {
                let mut app = App::load_state();
                app.parse_command(command);
                app.save_state();
            }
            Err(e) => {
                eprintln!("{}", e);
            }
            _ => eprintln!(
                "Commands other than `track-up` and `track-down` are currently not supported!"
            ),
        }
    } else {
        let mut s = termion().unwrap();
        let app = App::load_state();
        let layout = NamedView::new(
            "Frame",
            LinearLayout::vertical().child(NamedView::new("Main", app)),
        );
        s.add_layer(layout);
        s.add_global_callback(':', |s| open_command_window(s));

        s.set_theme(theme::theme_gen());
        s.run();

        let app = std::rc::Rc::try_unwrap(s.find_name::<NamedView<App>>("Main").unwrap()
                                .get_mut()
                                .into_owner()
                                .into_owner()).ok().unwrap().into_inner();
        app.save_state();

    }
}