add show command

This commit is contained in:
Vladimír Dudr 2022-09-26 23:17:30 +02:00
parent 90e78ba92d
commit 764b85f652
2 changed files with 30 additions and 0 deletions

View file

@ -17,6 +17,12 @@ pub enum Cmd {
paths: Vec<UserPath>,
},
/// Show command to be executed by "open" command
Show {
#[clap(required = true)]
paths: Vec<UserPath>,
},
/// Set the default handler for mime/extension
Set {
mime: MimeOrExtension,

View file

@ -1,3 +1,5 @@
use std::io::Write;
use config::CONFIG;
use error::{Error, Result};
use once_cell::sync::Lazy;
@ -51,6 +53,28 @@ fn main() -> Result<()> {
handler.open(paths)?;
}
}
Cmd::Show { 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() {
let stdout = std::io::stdout();
let (cmd, cmd_args) = handler.get_entry()?.get_cmd(paths)?;
write!(&stdout, "{}", cmd)?;
for arg in cmd_args.into_iter() {
write!(&stdout, " {}", arg)?;
}
write!(&stdout, "\n")?
}
}
Cmd::List { all } => {
apps.print(all)?;
}