From 65c0c2c6d29834ab148f42cfc5eed40042348815 Mon Sep 17 00:00:00 2001 From: Ben Maddison Date: Sun, 29 Nov 2020 23:49:15 +0200 Subject: [PATCH] handle multiple 'open' paths with their correct handlers --- src/main.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index b7b708d..5f577bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,9 @@ mod error; fn main() -> Result<()> { use clap::Clap; use cli::Cmd; - use common::MimeType; + use common::{MimeType, Handler}; use std::convert::TryFrom; + use std::collections::HashMap; // create config if it doesn't exist Lazy::force(&CONFIG); @@ -36,8 +37,15 @@ fn main() -> Result<()> { apps.show_handler(&mime.0, json)?; } Cmd::Open { paths } => { - let mime = MimeType::try_from(paths[0].as_str())?.0; - apps.get_handler(&mime)?.open(paths)?; + let mut handlers: HashMap> = HashMap::new(); + for path in paths.into_iter() { + let mime = MimeType::try_from(path.as_str())?.0; + let handler = apps.get_handler(&mime)?; + handlers.entry(handler).or_insert(Vec::new()).push(path); + } + for (handler, handled_paths) in handlers.into_iter() { + handler.open(handled_paths)?; + } } Cmd::List { all } => { apps.print(all)?;