aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTrim Bresilla2024-12-03 02:23:09 +0100
committerlukeflo2024-12-23 21:03:19 +0100
commit98deb23810ba4142082f82939f25d44d3094a34f (patch)
treeb9c1a4ca4845d83f0882c5397f067f116b1202ae /src
parent86d48aa48b9951b6cbc471113844d16683051f8f (diff)
downloadbibiman-98deb23810ba4142082f82939f25d44d3094a34f.tar.gz
bibiman-98deb23810ba4142082f82939f25d44d3094a34f.zip
feat: finalize basic "AddEntry" to add from DOI
- Modify the `handle_new_entry_submission` method to accept additional arguments. - Add error handling for failed inserts when appending to the file. - Introduce a new method `append_to_file` to handle file appending logic. - Update file handling to ensure new entries are correctly written to the specified file.
Diffstat (limited to 'src')
-rw-r--r--src/app.rs2
-rw-r--r--src/bibiman.rs28
2 files changed, 26 insertions, 4 deletions
diff --git a/src/app.rs b/src/app.rs
index ae45355..a200448 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -108,7 +108,7 @@ impl App {
}
KeyCode::Enter => {
// Handle submission of the new entry
- self.bibiman.handle_new_entry_submission();
+ self.bibiman.handle_new_entry_submission(args);
self.bibiman.close_popup();
self.input_mode = false;
}
diff --git a/src/bibiman.rs b/src/bibiman.rs
index ee52207..e176bfa 100644
--- a/src/bibiman.rs
+++ b/src/bibiman.rs
@@ -28,6 +28,8 @@ use editor_command::EditorBuilder;
use futures::executor::block_on;
use ratatui::widgets::ScrollbarState;
use std::fs;
+use std::fs::OpenOptions;
+use std::io::Write;
use std::process::Command;
use std::result::Result::Ok as AOk;
use tui_input::Input;
@@ -121,20 +123,25 @@ impl Bibiman {
self.popup_area.popup_kind = Some(PopupKind::AddEntry);
}
- pub fn handle_new_entry_submission(&mut self) {
+ pub fn handle_new_entry_submission(&mut self, args: &CLIArgs) {
let new_entry_title = self.popup_area.add_entry_input.trim();
let doi2bib = doi2bib::Doi2Bib::new().unwrap();
let new_entry_future = doi2bib.resolve_doi(new_entry_title);
let new_entry = block_on(new_entry_future);
if let AOk(entry) = new_entry {
- println!("New entry: {:?}", entry);
- // Add logic here to integrate the new entry into your application
+ // TODO: Add error handling for failed insert
+ if self.append_to_file(args, &entry.to_string()).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
} else {
self.popup_area.popup_kind = Some(PopupKind::MessageError);
self.popup_area.popup_message = "Failed to add new entry".to_string();
}
}
+
pub fn close_popup(&mut self) {
// Reset all popup fields to default values
self.popup_area = PopupArea::default();
@@ -414,6 +421,21 @@ impl Bibiman {
Ok(())
}
+ /// Appends a string to the appropriate BibLaTeX file.
+ 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")?;
+ // Write the 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(())
+ }
+
// Search entry list
pub fn search_entries(&mut self) {
// Use snapshot of entry list saved when starting the search