diff options
| -rw-r--r-- | src/bibiman.rs | 47 | ||||
| -rw-r--r-- | src/bibiman/search.rs | 14 | ||||
| -rw-r--r-- | tests/multi-files/bibfile2.bib | 1 |
3 files changed, 50 insertions, 12 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs index 88019d6..b8f04c8 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -25,6 +25,7 @@ use arboard::Clipboard; use color_eyre::eyre::{Ok, Result}; use editor_command::EditorBuilder; use ratatui::widgets::ScrollbarState; +use std::fs; use std::process::Command; use tui_input::Input; @@ -302,23 +303,37 @@ impl Bibiman { // get filecontent and citekey for calculating line number let citekey: &str = &self.entry_table.entry_table_items [self.entry_table.entry_table_state.selected().unwrap()] - .citekey; - // create independent copy of citekey for finding entry after updating list - let saved_key = citekey.to_owned(); - // TODO: Only for testing purposes, needs better logic to find correct file - // when using multi file approach - let filepath = args.fileargs[0].as_os_str(); + .citekey + .clone(); + + // Add curly brace as prefix and comma as suffix that only + // main citekeys are matched, not other fields like crossref + let citekey_pattern: String = format!("{{{},", citekey); + + // Check if multiple files were passed to bibiman and + // return the correct file path let filepath = if args.fileargs.len() == 1 { args.fileargs.first().unwrap().as_os_str() + } else { + let mut idx = 0; + for f in &args.fileargs { + if search::search_pattern_in_file(&citekey_pattern, &f).is_some() { + break; + } + idx += 1; + } + args.fileargs[idx].as_os_str() }; - let filecontent: &str = &self.main_biblio.bibfilestring; + let filecontent = fs::read_to_string(&filepath).unwrap(); + + // Search the line number to place the cursor at let mut line_count = 0; for line in filecontent.lines() { line_count += 1; // if reaching the citekey break the loop // if reaching end of lines without match, reset to 0 - if line.contains(citekey) { + if line.contains(&citekey_pattern) { break; } else if line_count == filecontent.len() { eprintln!( @@ -350,7 +365,7 @@ impl Bibiman { tui.terminal.clear()?; // Update the database and the lists to show changes - self.update_lists(args); + Self::update_lists(self, args); // Search for entry, selected before editing, by matching citekeys // Use earlier saved copy of citekey to match @@ -358,7 +373,7 @@ impl Bibiman { loop { if self.entry_table.entry_table_items[idx_count] .citekey - .contains(&saved_key) + .contains(citekey) { break; } @@ -576,3 +591,15 @@ impl Bibiman { } } } + +#[cfg(test)] +mod tests { + // use super::*; + + #[test] + fn citekey_pattern() { + let citekey = format!("{{{},", "a_key_2001"); + + assert_eq!(citekey, "{a_key_2001,") + } +} diff --git a/src/bibiman/search.rs b/src/bibiman/search.rs index b6b1b79..f848e82 100644 --- a/src/bibiman/search.rs +++ b/src/bibiman/search.rs @@ -20,7 +20,7 @@ use nucleo_matcher::{ pattern::{CaseMatching, Normalization, Pattern}, Config, Matcher, }; -use std::collections::HashMap; +use std::{collections::HashMap, ffi::OsStr, fs, path::PathBuf}; #[derive(Debug, Default)] pub struct BibiSearch { @@ -111,6 +111,16 @@ impl BibiSearch { } } +pub fn search_pattern_in_file<'a>(pattern: &str, file: &'a PathBuf) -> Option<&'a OsStr> { + let content = fs::read_to_string(file).unwrap(); + + if content.contains(pattern) { + Some(file.as_os_str()) + } else { + None + } +} + #[cfg(test)] mod tests { use super::*; @@ -127,7 +137,7 @@ mod tests { citekey: "author_1999".to_string(), abstract_text: "An abstract with multiple sentences. Here is the second".to_string(), doi_url: Some("https://www.bibiman.org".to_string()), - filepath: Some("/home/file/path.pdf".to_string()), + filepath: Some("/home/file/path.pdf".to_string().into()), subtitle: None, }; diff --git a/tests/multi-files/bibfile2.bib b/tests/multi-files/bibfile2.bib index ae491a1..869450a 100644 --- a/tests/multi-files/bibfile2.bib +++ b/tests/multi-files/bibfile2.bib @@ -4,6 +4,7 @@ volume = {691}, number = {13}, pages = {3027--3036}, + keywords = {mint}, date = {2006}, indextitle = {Effect of immobilization on catalytic characteristics}, } |
