diff options
| -rw-r--r-- | src/app.rs | 91 | ||||
| -rw-r--r-- | src/bibiman.rs | 94 | ||||
| -rw-r--r-- | tests/biblatex-test.bib | 2 |
3 files changed, 95 insertions, 92 deletions
@@ -17,7 +17,6 @@ use crate::bibiman::{CurrentArea, FormerArea}; use color_eyre::eyre::{Context, Ok, Result}; -use regex::Regex; // use super::Event; use crate::cliargs::CLIArgs; use crate::tui::commands::InputCmdAction; @@ -26,9 +25,7 @@ use crate::tui::{self, Tui}; use crate::{bibiman::Bibiman, tui::commands::CmdAction}; use core::panic; use std::ffi::OsStr; -use std::fs::{File, OpenOptions}; -use std::io::Write; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::process::{Command, Stdio}; use tui::Event; use tui_input::backend::crossterm::EventHandler; @@ -271,81 +268,10 @@ impl App { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.close_popup(); } else if let Some(PopupKind::OpenRes) = self.bibiman.popup_area.popup_kind { - // Index of selected entry - let entry_idx = self - .bibiman - .entry_table - .entry_table_state - .selected() - .unwrap(); - - // Index of selected popup field - let popup_idx = self.bibiman.popup_area.popup_state.selected().unwrap(); - - // Choose ressource depending an selected popup field - if self.bibiman.popup_area.popup_list[popup_idx].contains("Weblink") { - let object = - self.bibiman.entry_table.entry_table_items[entry_idx].doi_url(); - let url = prepare_weblink(object); - open_connected_link(&url)?; - } else if self.bibiman.popup_area.popup_list[popup_idx].contains("File") { - let object = - self.bibiman.entry_table.entry_table_items[entry_idx].filepath(); - open_connected_file(object)?; - } else { - eprintln!("Unable to find ressource to open"); - }; - // run command to open file/Url - self.bibiman.close_popup() + self.bibiman.open_connected_res()?; } else if let Some(PopupKind::AppendToFile) = self.bibiman.popup_area.popup_kind { - // Index of selected popup field - let popup_idx = self.bibiman.popup_area.popup_state.selected().unwrap(); - - // regex pattern to match citekey in fetched bibtexstring - let pattern = Regex::new(r"\{([^\{\},]*),").unwrap(); - - let citekey = pattern - .captures(&self.bibiman.popup_area.popup_sel_item) - .unwrap() - .get(1) - .unwrap() - .as_str() - .to_string(); - - // Check if new file or existing file was choosen - let mut file = if self.bibiman.popup_area.popup_list[popup_idx] - .contains("Create new file") - { - let citekey = PathBuf::from(&citekey); - // Get path of current files - let path: PathBuf = if args.files[0].is_file() { - args.files[0].parent().unwrap().to_owned() - } else { - dirs::home_dir().unwrap() // home dir as fallback - }; - - let citekey = citekey.with_extension("bib"); - - let newfile = path.join(citekey); - - args.files.push(newfile.clone()); - - File::create_new(newfile).unwrap() - } else { - let file_path = &args.files[popup_idx - 1]; - OpenOptions::new().append(true).open(file_path).unwrap() - }; - // Optionally, add a newline before the content - file.write_all(b"\n")?; - // Write content to file - file.write_all(self.bibiman.popup_area.popup_sel_item.as_bytes())?; - // Update the database and the lists to reflect the new content - self.bibiman.update_lists(args); - self.bibiman.close_popup(); - - // Select newly created entry - self.bibiman.select_entry_by_citekey(&citekey); + self.bibiman.append_entry_to_file(args)? } } } @@ -517,15 +443,4 @@ mod test { assert_eq!(path, PathBuf::from(full_path)) } - - #[test] - fn regex_capture_citekey() { - let re = Regex::new(r"\{([^\{\},]*),").unwrap(); - - let bibstring = String::from("@article{citekey77_2001:!?, author = {Hanks, Tom}, title = {A great book}, year = {2001}}"); - - let result = re.captures(&bibstring).unwrap(); - - assert_eq!(result.get(1).unwrap().as_str(), "citekey77_2001:!?") - } } diff --git a/src/bibiman.rs b/src/bibiman.rs index 5d6aa30..754e8f6 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -15,6 +15,7 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// +use crate::app; use crate::bibiman::entries::EntryTableColumn; use crate::bibiman::{bibisetup::*, search::BibiSearch}; use crate::cliargs::CLIArgs; @@ -22,14 +23,16 @@ use crate::tui::popup::{PopupArea, PopupKind}; use crate::tui::Tui; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; use arboard::Clipboard; -use color_eyre::eyre::{eyre, Result}; +use color_eyre::eyre::Result; use doi2bib; use editor_command::EditorBuilder; use futures::executor::block_on; use ratatui::widgets::ScrollbarState; +use regex::Regex; use std::fs; -use std::fs::OpenOptions; +use std::fs::{File, OpenOptions}; use std::io::Write; +use std::path::PathBuf; use std::process::Command; use std::result::Result::Ok; use tui_input::Input; @@ -442,6 +445,80 @@ impl Bibiman { self.popup_area.popup_selection(items); } + pub fn append_entry_to_file(&mut self, args: &mut CLIArgs) -> Result<()> { + // Index of selected popup field + let popup_idx = self.popup_area.popup_state.selected().unwrap(); + + // regex pattern to match citekey in fetched bibtexstring + let pattern = Regex::new(r"\{([^\{\},]*),").unwrap(); + + let citekey = pattern + .captures(&self.popup_area.popup_sel_item) + .unwrap() + .get(1) + .unwrap() + .as_str() + .to_string(); + + // Check if new file or existing file was choosen + let mut file = if self.popup_area.popup_list[popup_idx].contains("Create new file") { + let citekey = PathBuf::from(&citekey); + // Get path of current files + let path: PathBuf = if args.files[0].is_file() { + args.files[0].parent().unwrap().to_owned() + } else { + dirs::home_dir().unwrap() // home dir as fallback + }; + + let citekey = citekey.with_extension("bib"); + + let newfile = path.join(citekey); + + args.files.push(newfile.clone()); + + File::create_new(newfile).unwrap() + } else { + let file_path = &args.files[popup_idx - 1]; + OpenOptions::new().append(true).open(file_path).unwrap() + }; + // Optionally, add a newline before the content + file.write_all(b"\n")?; + // Write content to file + file.write_all(self.popup_area.popup_sel_item.as_bytes())?; + // Update the database and the lists to reflect the new content + self.update_lists(args); + self.close_popup(); + + // Select newly created entry + self.select_entry_by_citekey(&citekey); + + Ok(()) + } + + pub fn open_connected_res(&mut self) -> Result<()> { + // Index of selected entry + let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); + + // Index of selected popup field + let popup_idx = self.popup_area.popup_state.selected().unwrap(); + + // Choose ressource depending an selected popup field + if self.popup_area.popup_list[popup_idx].contains("Weblink") { + let object = self.entry_table.entry_table_items[entry_idx].doi_url(); + let url = app::prepare_weblink(object); + app::open_connected_link(&url)?; + } else if self.popup_area.popup_list[popup_idx].contains("File") { + let object = self.entry_table.entry_table_items[entry_idx].filepath(); + app::open_connected_file(object)?; + } else { + eprintln!("Unable to find ressource to open"); + }; + // run command to open file/Url + self.close_popup(); + + Ok(()) + } + /// Formats a raw BibTeX entry string for better readability. pub fn format_bibtex_entry(entry: &str, file_path: &str) -> String { let mut formatted = String::new(); @@ -757,7 +834,7 @@ impl Bibiman { #[cfg(test)] mod tests { - // use super::*; + use super::*; #[test] fn citekey_pattern() { @@ -765,4 +842,15 @@ mod tests { assert_eq!(citekey, "{a_key_2001,") } + + #[test] + fn regex_capture_citekey() { + let re = Regex::new(r"\{([^\{\},]*),").unwrap(); + + let bibstring = String::from("@article{citekey77_2001:!?, author = {Hanks, Tom}, title = {A great book}, year = {2001}}"); + + let result = re.captures(&bibstring).unwrap(); + + assert_eq!(result.get(1).unwrap().as_str(), "citekey77_2001:!?") + } } diff --git a/tests/biblatex-test.bib b/tests/biblatex-test.bib index b366fc3..d0fc0a6 100644 --- a/tests/biblatex-test.bib +++ b/tests/biblatex-test.bib @@ -65,7 +65,7 @@ author = {Aristotle}, location = {New York}, publisher = {G. P. Putnam}, - url = {infobooks.org/authors/classic/aristotle-books/#Physic}, + url = {https://www.infobooks.org/authors/classic/aristotle-books/#Physic}, date = {1929}, translator = {Wicksteed, P. H. and Cornford, F. M.}, keywords = {primary, ancient, philosophy}, |
