diff options
| -rw-r--r-- | src/app.rs | 8 | ||||
| -rw-r--r-- | src/bibiman.rs | 15 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 117 | ||||
| -rw-r--r-- | src/bibiman/entries.rs | 2 | ||||
| -rw-r--r-- | src/bibiman/search.rs | 1 |
5 files changed, 111 insertions, 32 deletions
@@ -343,7 +343,8 @@ impl App { .unwrap(); let entry = self.bibiman.entry_table.entry_table_items[idx].clone(); let mut items: Vec<(String, String)> = vec![]; - if entry.filepath.is_some() || entry.doi_url.is_some() { + if entry.filepath.is_some() || entry.doi_url.is_some() || entry.notes.is_some() + { if entry.doi_url.is_some() { items.push(( "Weblink (DOI/URL): ".into(), @@ -370,6 +371,11 @@ impl App { )) }); } + if entry.notes.is_some() { + entry.notes.unwrap().iter().for_each(|n| { + items.push(("Note: ".into(), n.clone().into_string().unwrap())); + }); + } self.bibiman .open_popup(PopupKind::OpenRes, None, None, Some(items))?; diff --git a/src/bibiman.rs b/src/bibiman.rs index 21601e3..96a733c 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -692,6 +692,21 @@ impl Bibiman { None, )?; } + } + if self.popup_area.popup_list[popup_idx].0.contains("Note") { + let file = expand_home(&PathBuf::from(popup_entry.clone())); + // let object: OsString = popup_entry.into(); + if file.is_file() { + app::open_connected_file(cfg, &file.into_os_string())?; + self.close_popup(); + } else { + self.open_popup( + PopupKind::MessageError, + Some("No valid file path: "), + Some(file.to_str().unwrap()), + None, + )?; + } } else { eprintln!("Unable to find ressource to open"); }; diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index 8d8de53..e79960c 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -201,10 +201,26 @@ impl BibiSetup { cfg: &BibiConfig, ) -> Vec<BibiData> { let mut pdf_files = if cfg.general.pdf_path.is_some() { - collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), Some(vec!["pdf"])) + collect_file_paths(cfg.general.pdf_path.as_ref().unwrap(), &Some(vec!["pdf"])) } else { None }; + let ext: Option<Vec<&str>> = + if cfg.general.note_path.is_some() && cfg.general.note_extensions.is_some() { + let mut ext: Vec<&str> = Vec::new(); + for e in cfg.general.note_extensions.as_ref().unwrap().iter() { + ext.push(e); + } + Some(ext) + } else { + None + }; + let mut note_files = + if cfg.general.note_path.is_some() && cfg.general.note_extensions.is_some() { + collect_file_paths(cfg.general.note_path.as_ref().unwrap(), &ext) + } else { + None + }; citekeys .iter() .enumerate() @@ -226,7 +242,11 @@ impl BibiSetup { filepath: filepaths.0, file_field: filepaths.1, subtitle: Self::get_subtitle(k, bibliography), - notes: None, + notes: if note_files.is_some() { + Self::get_notepath(k, &mut note_files, &ext) + } else { + None + }, } }) .collect() @@ -384,11 +404,17 @@ impl BibiSetup { } } - // pub fn get_notepath( - // citekey: &str, - // note_files: &mut Option<HashMap<String, Vec<PathBuf>>>, - // ) -> Option<Vec<OsString>> { - // } + pub fn get_notepath( + citekey: &str, + note_files: &mut Option<HashMap<String, Vec<PathBuf>>>, + ext: &Option<Vec<&str>>, + ) -> Option<Vec<OsString>> { + if let Some(e) = ext { + Self::merge_filepath_or_none_two(citekey, note_files, e.to_vec()) + } else { + None + } + } pub fn get_subtitle(citekey: &str, biblio: &Bibliography) -> Option<String> { if biblio.get(citekey).unwrap().subtitle().is_ok() { @@ -405,29 +431,9 @@ impl BibiSetup { } } - fn merge_filepath_or_none( - citekey: &str, - pdf_files: &mut Option<HashMap<String, Vec<PathBuf>>>, - ) -> Option<Vec<OsString>> { - let pdf_file = { - let citekey = citekey.to_owned().to_ascii_lowercase() + ".pdf"; - - if pdf_files.as_ref().unwrap().contains_key(&citekey) { - let path_vec = pdf_files - .as_ref() - .unwrap() - .get(&citekey) - .unwrap() - .to_owned(); - Some(path_vec.into_iter().map(|p| p.into_os_string()).collect()) - } else { - None - } - }; - - pdf_file - } - + /// Check if there exists files with the basename of the format + /// "citekey.extension" in the passed hashmap. If so, return all matches + /// as `Option<Vec>`, otherwise return `None` fn merge_filepath_or_none_two( citekey: &str, files: &mut Option<HashMap<String, Vec<PathBuf>>>, @@ -469,7 +475,7 @@ impl BibiSetup { /// from the given directory and its subdirectories! pub fn collect_file_paths( file_dir: &PathBuf, - extensions: Option<Vec<&str>>, + extensions: &Option<Vec<&str>>, ) -> Option<HashMap<String, Vec<PathBuf>>> { let mut files: HashMap<String, Vec<PathBuf>> = HashMap::new(); @@ -531,3 +537,52 @@ pub fn collect_file_paths( Some(files) } } + +#[cfg(test)] +mod tests { + use std::{collections::HashMap, ffi::OsString, path::PathBuf}; + + use super::BibiSetup; + + #[test] + fn check_file_matching() { + let mut files: HashMap<String, Vec<PathBuf>> = HashMap::new(); + files.insert( + "citekey.md".to_string(), + vec![ + PathBuf::from("/one/note/citekey.md"), + PathBuf::from("/one/other/citekey.md"), + ], + ); + files.insert( + "citekey.pdf".to_string(), + vec![ + PathBuf::from("/one/note/citekey.pdf"), + PathBuf::from("/one/other/citekey.pdf"), + ], + ); + files.insert( + "citekey2.pdf".to_string(), + vec![ + PathBuf::from("/one/note/citekey2.pdf"), + PathBuf::from("/one/other/citekey2.pdf"), + ], + ); + + let matches = + BibiSetup::merge_filepath_or_none_two("citekey", &mut Some(files), vec!["md", "pdf"]); + + assert_eq!( + matches.clone().unwrap().iter().next().unwrap().to_owned(), + OsString::from("/one/note/citekey.md") + ); + assert_eq!( + matches.clone().unwrap().last().unwrap().to_owned(), + OsString::from("/one/other/citekey.pdf") + ); + assert!(!matches + .clone() + .unwrap() + .contains(&OsString::from("/one/other/citekey2.pdf"))); + } +} diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index 88a1583..e0c230b 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -159,6 +159,7 @@ mod tests { filepath: None, file_field: false, subtitle: None, + notes: None, }; let entry_vec = BibiData::ref_vec(&mut entry); @@ -177,6 +178,7 @@ mod tests { filepath: None, file_field: false, subtitle: None, + notes: None, }; let entry_vec_editors = BibiData::ref_vec(&mut entry_editors); diff --git a/src/bibiman/search.rs b/src/bibiman/search.rs index f391aed..0e32f63 100644 --- a/src/bibiman/search.rs +++ b/src/bibiman/search.rs @@ -137,6 +137,7 @@ mod tests { filepath: Some(vec![OsString::from("/home/file/path.pdf")]), file_field: true, subtitle: None, + notes: None, }; let joined_vec = BibiSearch::convert_to_string(&bibvec); |
