aboutsummaryrefslogtreecommitdiff
path: root/src/bibiman.rs
diff options
context:
space:
mode:
authorlukeflo2024-12-16 13:46:24 +0100
committerlukeflo2024-12-23 21:03:19 +0100
commitbf93bbee1b59c9804a01a7476e12264bbbcf5f40 (patch)
treee8b1454a9c85e6932b443a34a4a18a02b71bfccc /src/bibiman.rs
parenta6fca1fcf164142d84d09242b9d95a1da0b2d2d9 (diff)
downloadbibiman-bf93bbee1b59c9804a01a7476e12264bbbcf5f40.tar.gz
bibiman-bf93bbee1b59c9804a01a7476e12264bbbcf5f40.zip
rewrite add-entry via DOI workflow
+ split some functions to fit with different popups + select if append to file (and to which) or create new file + error handling if resolving doi ist not possible + error handling for wront doi patterns
Diffstat (limited to 'src/bibiman.rs')
-rw-r--r--src/bibiman.rs94
1 files changed, 49 insertions, 45 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs
index 4e6e5e8..0dc64e0 100644
--- a/src/bibiman.rs
+++ b/src/bibiman.rs
@@ -22,7 +22,7 @@ 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::Result;
+use color_eyre::eyre::{eyre, Result};
use doi2bib;
use editor_command::EditorBuilder;
use futures::executor::block_on;
@@ -115,32 +115,32 @@ impl Bibiman {
pub fn add_entry(&mut self) {
if let CurrentArea::EntryArea = self.current_area {
self.former_area = Some(FormerArea::EntryArea);
- } else if let CurrentArea::TagArea = self.current_area {
- self.former_area = Some(FormerArea::TagArea);
}
self.popup_area.is_popup = true;
self.current_area = CurrentArea::PopupArea;
self.popup_area.popup_kind = Some(PopupKind::AddEntry);
}
- pub fn handle_new_entry_submission(&mut self, args: &CLIArgs, doi_string: &Input) {
- let new_entry_title = doi_string.value();
+ ///Try to resolve entered DOI. If successfull, choose file where to append
+ ///the new entry via `append_to_file()` function. If not, show error popup
+ ///
+ ///The method needs two arguments: the CLIArgs struct and the `str` containing the DOI
+ pub fn handle_new_entry_submission(&mut self, args: &CLIArgs, doi_string: &str) {
let doi2bib = doi2bib::Doi2Bib::new().unwrap();
- let new_entry_future = doi2bib.resolve_doi(new_entry_title);
+ let new_entry_future = doi2bib.resolve_doi(doi_string);
let new_entry = block_on(new_entry_future);
if let Ok(entry) = new_entry {
- // TODO: Add error handling for failed insert
- let formatted_content = Self::format_bibtex_entry(&entry, "");
-
- if self.append_to_file(args, &formatted_content).is_err() {
- self.popup_area.popup_kind = Some(PopupKind::MessageError);
- self.popup_area.popup_message = "Failed to add new entry".to_string();
- }
- // TODO: Add error handling for failed DOI lookup
+ // Save generated bibtex entry in structs field
+ self.popup_area.popup_sel_item = entry;
+ self.popup_area.popup_kind = Some(PopupKind::SelectFile);
+ self.append_to_file(args);
+ self.former_area = Some(FormerArea::EntryArea);
+ self.current_area = CurrentArea::PopupArea;
+ self.popup_area.popup_state.select(Some(0))
} else {
- self.popup_area.popup_kind = Some(PopupKind::MessageError);
- self.popup_area.popup_message = "Failed to add new entry".to_string();
+ self.popup_area
+ .popup_message("Failed to add new entry", "", false);
}
}
@@ -336,6 +336,26 @@ impl Bibiman {
}
}
+ pub fn select_entry_by_citekey(&mut self, citekey: &str) {
+ // Search for entry by matching citekeys
+ let mut idx_count = 0;
+ loop {
+ if idx_count == self.entry_table.entry_table_items.len() {
+ idx_count = 0;
+ break;
+ } else if self.entry_table.entry_table_items[idx_count]
+ .citekey
+ .contains(citekey)
+ {
+ break;
+ }
+ idx_count += 1
+ }
+
+ // Set selected entry to vec-index of match
+ self.entry_table.entry_table_state.select(Some(idx_count));
+ }
+
pub fn run_editor(&mut self, args: &CLIArgs, tui: &mut Tui) -> Result<()> {
// get filecontent and citekey for calculating line number
let citekey: &str = &self.entry_table.entry_table_items
@@ -404,38 +424,22 @@ impl Bibiman {
// Update the database and the lists to show changes
Self::update_lists(self, args);
- // Search for entry, selected before editing, by matching citekeys
- // Use earlier saved copy of citekey to match
- let mut idx_count = 0;
- loop {
- if self.entry_table.entry_table_items[idx_count]
- .citekey
- .contains(citekey)
- {
- break;
- }
- idx_count += 1
- }
-
- // Set selected entry to vec-index of match
- self.entry_table.entry_table_state.select(Some(idx_count));
+ // Select entry which was selected before entering editor
+ self.select_entry_by_citekey(citekey);
Ok(())
}
- pub fn append_to_file(&mut self, args: &CLIArgs, content: &str) -> Result<()> {
- // Determine the file path to append to
- let file_path = args.files.first().unwrap();
- // Open the file in append mode
- let mut file = OpenOptions::new().append(true).open(file_path).unwrap();
- // Optionally, add a newline before the content
- file.write_all(b"\n")?;
- // Format the content
- // Write the formatted content to the file
- file.write_all(content.as_bytes())?;
- // Update the database and the lists to reflect the new content
- self.update_lists(args);
- Ok(())
+ pub fn append_to_file(&mut self, args: &CLIArgs) {
+ let mut items = vec!["Create new file".to_owned()];
+ if args.files.len() > 1 {
+ for f in args.files.clone() {
+ items.push(f.to_str().unwrap().to_owned());
+ }
+ } else {
+ items.push(args.files.first().unwrap().to_str().unwrap().to_owned());
+ }
+ self.popup_area.popup_selection(items);
}
/// Formats a raw BibTeX entry string for better readability.
@@ -448,7 +452,7 @@ impl Bibiman {
let preamble = &entry[..start_brace_pos + 1];
let preamble = preamble.trim_start();
formatted.push_str(preamble);
- formatted.push('\n'); // Add newline
+ // formatted.push('\n'); // Add newline
// Get the content inside the braces
let rest = &entry[start_brace_pos + 1..];