diff options
author | Ophir LOJKINE <ophir.lojkine@auto-grid.com> | 2019-03-12 11:07:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-12 11:07:20 +0100 |
commit | cc5a7e0ed34783560c727898d75120e865999860 (patch) | |
tree | ecf67bfae2e7e4e834d4625471701544e0b9ccc8 /src/external.rs | |
parent | df17d05ec2445594e1bca869542d2a6b3ec81e3b (diff) | |
parent | 866fec4c714a1fc94f91e558b8d357adcec1a732 (diff) |
Merge branch 'master' into master
Diffstat (limited to 'src/external.rs')
-rw-r--r-- | src/external.rs | 75 |
1 files changed, 45 insertions, 30 deletions
diff --git a/src/external.rs b/src/external.rs index 188feef..99f9c59 100644 --- a/src/external.rs +++ b/src/external.rs @@ -3,57 +3,73 @@ use std::fs::OpenOptions; use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::process::Command; +use opener; use crate::app::AppStateCmdResult; use crate::app_context::AppContext; use crate::errors::LaunchError; +use crate::errors::ProgramError; /// description of a possible launch of an external program /// (might be more complex, and a sequence of things to try, in the future). /// A launchable can only be executed on end of life of broot. #[derive(Debug)] -pub struct Launchable { - exe: String, - args: Vec<String>, - pub just_print: bool, // this part of the API will change +pub enum Launchable { + Printer { // just print something on stdout on end of broot + to_print: String, + }, + Program { // execute an external program + exe: String, + args: Vec<String>, + }, + SystemOpen { // open a path + path: PathBuf, + } } impl Launchable { - pub fn opener(path: &PathBuf) -> io::Result<Launchable> { - Launchable::from(vec![ - "xdg-open".to_string(), - path.to_string_lossy().to_string(), - ]) + pub fn opener(path: PathBuf) -> Launchable { + Launchable::SystemOpen { + path + } + } + pub fn printer(to_print: String) -> Launchable { + Launchable::Printer { + to_print + } } - pub fn from(mut parts: Vec<String>) -> io::Result<Launchable> { + pub fn program(mut parts: Vec<String>) -> io::Result<Launchable> { let mut parts = parts.drain(0..); match parts.next() { - Some(exe) => Ok(Launchable { + Some(exe) => Ok(Launchable::Program { exe, args: parts.collect(), - just_print: false, }), None => Err(io::Error::new(io::ErrorKind::Other, "Empty launch string")), } } - pub fn execute(&self) -> Result<(), LaunchError> { - if self.just_print { - print!("{}", &self.exe); - for arg in &self.args { - print!(" {}", &arg); + + pub fn execute(&self) -> Result<(), ProgramError> { + match self { + Launchable::Printer { to_print } => Ok(println!("{}", to_print)), + Launchable::Program { exe, args } => { + Command::new(&exe) + .args(args.iter()) + .spawn() + .and_then(|mut p| p.wait()) + .map_err(|source| LaunchError { + program: self.exe.clone().into(), + source, + })?; + Ok(()) + } + Launchable::SystemOpen { path } => { + match opener::open(&path) { + Ok(_) => Ok(()), + Err(err) => Err(ProgramError::OpenError{err}), + } } - println!(); - } else { - Command::new(&self.exe) - .args(self.args.iter()) - .spawn() - .and_then(|mut p| p.wait()) - .map_err(|source| LaunchError { - program: self.exe.clone().into(), - source, - })?; } - Ok(()) } } @@ -87,8 +103,7 @@ pub fn print_path(path: &Path, con: &AppContext) -> io::Result<AppStateCmdResult } else { // no output path provided. We write on stdout, but we must // do it after app closing to have the normal terminal - let mut launchable = Launchable::from(vec![path])?; - launchable.just_print = true; + let launchable = Launchable::printer(path); AppStateCmdResult::Launch(launchable) }, ) |