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