summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 1ac434b8ee987de91994440c86e4a2341a23aadd (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
#[cfg(unix)]
use mimalloc::MiMalloc;
#[cfg(unix)]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;

mod app;
mod app_context;
mod browser_states;
mod browser_verbs;
mod cli;
mod command_parsing;
mod commands;
mod conf;
mod displayable_tree;
mod errors;
mod external;
mod event;
mod file_sizes;
mod flat_tree;
mod fuzzy_patterns;
mod git_ignore;
mod help_content;
mod help_states;
mod help_verbs;
mod input;
mod patterns;
mod permissions;
mod regex_patterns;
mod screens;
mod shell_bash;
mod shell_fish;
mod shell_install;
mod skin;
mod skin_conf;
mod spinner;
mod status;
mod task_sync;
mod tree_build;
mod tree_options;
mod verb_invocation;
mod verb_store;
mod verbs;

use log::LevelFilter;
use simplelog;
use std::env;
use std::fs::File;
use std::result::Result;
use std::str::FromStr;

use crate::app::App;
use crate::app_context::AppContext;
use crate::conf::Conf;
use crate::errors::ProgramError;
use crate::external::Launchable;
use crate::verb_store::VerbStore;

/// configure the application log according to env variable.
///
/// There's no log unless the BROOT_LOG environment variable is set to
///  a valid log level (trace, debug, info, warn, error, off)
/// Example:
///      BROOT_LOG=info broot
/// As broot is a terminal application, we only log to a file (dev.log)
fn configure_log() {
    let level = env::var("BROOT_LOG").unwrap_or_else(|_| "off".to_string());
    if level == "off" {
        return;
    }
    if let Ok(level) = LevelFilter::from_str(&level) {
        simplelog::WriteLogger::init(
            level,
            simplelog::Config::default(),
            File::create("dev.log").expect("Log file can't be created"),
        )
        .expect("log initialization failed");
        info!(
            "Starting Broot v{} with log level {}",
            env!("CARGO_PKG_VERSION"),
            level
        );
        #[cfg(unix)]
        info!("allocator: Mimalloc");
    }
}

/// run the application, and maybe return a launchable
/// which must be run after broot
fn run() -> Result<Option<Launchable>, ProgramError> {
    configure_log();
    let launch_args = cli::read_launch_args()?;
    let should_quit = shell_install::init(&launch_args)?;
    if should_quit {
        return Ok(None);
    }
    let mut verb_store = VerbStore::new();
    let config = Conf::from_default_location()?;
    verb_store.init(&config);
    let context = AppContext {
        launch_args,
        verb_store,
    };
    let skin = skin::Skin::create(config.skin);
    App::new().run(&context, skin)
}

fn main() {
    let res = match run() {
        Ok(res) => res,
        Err(e) => {
            // this usually happens when the passed path isn't of a directory
            warn!("Error: {}", e);
            eprintln!("{}", e);
            return;
        }
    };
    if let Some(launchable) = res {
        info!("launching {:?}", &launchable);
        if let Err(e) = launchable.execute() {
            warn!("Failed to launch {:?}", &launchable);
            warn!("Error: {:?}", e);
            eprintln!("{}", e);
        }
    }
    info!("bye");
}