From 764b85f65289050d0685bc06f873ea6a05e9521c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Dudr?= Date: Mon, 26 Sep 2022 23:17:30 +0200 Subject: [PATCH] add show command --- src/cli.rs | 6 ++++++ src/main.rs | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index 9607dd1..2818f35 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -17,6 +17,12 @@ pub enum Cmd { paths: Vec, }, + /// Show command to be executed by "open" command + Show { + #[clap(required = true)] + paths: Vec, + }, + /// Set the default handler for mime/extension Set { mime: MimeOrExtension, diff --git a/src/main.rs b/src/main.rs index 4bc38d4..2899966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> = + 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)?; }