aboutsummaryrefslogtreecommitdiff
path: root/src/bibiman.rs
diff options
context:
space:
mode:
authorlukeflo2024-12-23 20:41:55 +0100
committerlukeflo2024-12-23 21:03:19 +0100
commit8333136cb770cbfbb7be2160fd85687493d96ea4 (patch)
tree26ed2bc6042d0b73e76de16d36993d94215a6408 /src/bibiman.rs
parent6ff6b82e0fcea4344db8b17ea5be2d72b3d9d9f2 (diff)
downloadbibiman-8333136cb770cbfbb7be2160fd85687493d96ea4.tar.gz
bibiman-8333136cb770cbfbb7be2160fd85687493d96ea4.zip
collect code for adding entries and opening files in method
Diffstat (limited to 'src/bibiman.rs')
-rw-r--r--src/bibiman.rs94
1 files changed, 91 insertions, 3 deletions
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:!?")
+ }
}