diff options
-rw-r--r-- | src/fail.rs | 9 | ||||
-rw-r--r-- | src/file_browser.rs | 35 | ||||
-rw-r--r-- | src/files.rs | 7 |
3 files changed, 46 insertions, 5 deletions
diff --git a/src/fail.rs b/src/fail.rs index 2c01885..1aa1d42 100644 --- a/src/fail.rs +++ b/src/fail.rs @@ -50,6 +50,8 @@ pub enum HError { HBoxWrongRatioError{ wnum: usize, ratio: Vec<usize> }, #[fail(display = "Got wrong widget: {}! Wanted: {}", got, wanted)] WrongWidgetError{got: String, wanted: String}, + #[fail(display = "Strip Prefix Error: {}", error)] + StripPrefixError{#[cause] error: std::path::StripPrefixError}, } impl HError { @@ -145,3 +147,10 @@ impl From<std::option::NoneError> for HError { HError::NoneError } } + +impl From<std::path::StripPrefixError> for HError { + fn from(error: std::path::StripPrefixError) -> Self { + dbg!(&error); + HError::StripPrefixError{error: error} + } +} diff --git a/src/file_browser.rs b/src/file_browser.rs index 49758b9..5f4e5fb 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -110,7 +110,14 @@ impl Tabbable for TabView<FileBrowser> { Key::Char('!') => { let tab_dirs = self.widgets.iter().map(|w| w.cwd.clone()) .collect::<Vec<_>>(); - self.widgets[self.active].exec_cmd(tab_dirs) + let selected_files = self.widgets.iter().fold(HashMap::new(), + |mut f, w| { + let dir = w.cwd().unwrap().clone(); + let selected_files = w.selected_files().unwrap(); + f.insert(dir, selected_files); + f + }); + self.widgets[self.active].exec_cmd(tab_dirs, selected_files) } _ => { self.active_tab_mut().on_key(key) } } @@ -424,6 +431,14 @@ impl FileBrowser { Ok(file) } + pub fn selected_files(&self) -> HResult<Vec<File>> { + let widget = self.main_widget()?.widget()?; + let files = widget.lock()?.as_ref()?.content.get_selected().into_iter().map(|f| { + f.clone() + }).collect(); + Ok(files) + } + pub fn main_widget(&self) -> HResult<&WillBeWidget<ListView<Files>>> { let widget = match self.columns.get_main_widget()? { FileBrowserWidgets::FileList(filelist) => Ok(filelist), @@ -521,11 +536,12 @@ impl FileBrowser { Ok(()) } - fn exec_cmd(&mut self, tab_dirs: Vec<File>) -> HResult<()> { + fn exec_cmd(&mut self, + tab_dirs: Vec<File>, + tab_files: HashMap<File, Vec<File>>) -> HResult<()> { + let cwd = self.cwd()?; let filename = self.selected_file()?.name.clone(); - let widget = self.main_widget()?.widget()?; - let widget = widget.lock()?; - let selected_files = widget.as_ref()?.content.get_selected(); + let selected_files = self.selected_files()?; let file_names = selected_files.iter().map(|f| f.name.clone()).collect::<Vec<String>>(); @@ -544,6 +560,15 @@ impl FileBrowser { }; for (i, tab_dir) in tab_dirs.iter().enumerate() { + if let Some(tab_files) = tab_files.get(tab_dir) { + let tab_file_identifier = format!("${}s", i); + let args = tab_files.iter().map(|f| { + let file_path = f.strip_prefix(&cwd); + format!(" \"{}\" ", file_path.to_string_lossy()) + }).collect::<String>(); + cmd = cmd.replace(&tab_file_identifier, &args); + } + let tab_identifier = format!("${}", i); let tab_path = tab_dir.path.to_string_lossy(); cmd = cmd.replace(&tab_identifier, &tab_path); diff --git a/src/files.rs b/src/files.rs index 9774f40..a5e2daa 100644 --- a/src/files.rs +++ b/src/files.rs @@ -441,6 +441,13 @@ impl File { Files::new_from_path(&self.path) } + pub fn strip_prefix(&self, base: &File) -> PathBuf { + let base_path = base.path.clone(); + match self.path.strip_prefix(base_path) { + Ok(path) => PathBuf::from(path), + Err(_) => self.path.clone() + } + } pub fn path(&self) -> PathBuf { self.path.clone() |