-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.rs
88 lines (80 loc) · 2.4 KB
/
main.rs
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
use config::CONFIG;
use error::{Error, Result};
use once_cell::sync::Lazy;
mod apps;
mod cli;
mod common;
mod config;
mod error;
mod utils;
fn main() -> Result<()> {
use clap::Clap;
use cli::Cmd;
use common::Handler;
use std::collections::HashMap;
// create config if it doesn't exist
Lazy::force(&CONFIG);
let mut apps = (*apps::APPS).clone();
let res = || -> Result<()> {
match Cmd::parse() {
Cmd::Set { mime, handler } => {
apps.set_handler(mime.0, handler);
apps.save()?;
}
Cmd::Add { mime, handler } => {
apps.add_handler(mime.0, handler);
apps.save()?;
}
Cmd::Launch { mime, args } => {
apps.get_handler(&mime.0)?.launch(args.into_iter().map(|a| a.to_string()).collect())?;
}
Cmd::Get { mime, json } => {
apps.show_handler(&mime.0, json)?;
}
Cmd::Open { paths } => {
let mut handlers: HashMap<Handler, Vec<String>> =
HashMap::new();
for path in paths.into_iter() {
handlers
.entry(apps.get_handler(&path.get_mime()?.0)?)
.or_default()
.push(path.to_string());
}
for (handler, paths) in handlers.into_iter() {
handler.open(paths)?;
}
}
Cmd::List { all } => {
apps.print(all)?;
}
Cmd::Unset { mime } => {
apps.remove_handler(&mime.0)?;
}
Cmd::Autocomplete {
desktop_files,
mimes,
} => {
if desktop_files {
apps::MimeApps::list_handlers()?;
} else if mimes {
common::db_autocomplete()?;
}
}
}
Ok(())
}();
match (res, atty::is(atty::Stream::Stdout)) {
(Err(e), _) if matches!(e, Error::Cancelled) => {
std::process::exit(1);
}
(Err(e), true) => {
eprintln!("{}", e);
std::process::exit(1);
}
(Err(e), false) => {
utils::notify("handlr error", &e.to_string())?;
std::process::exit(1);
}
_ => Ok(()),
}
}