diff options
| author | Trim Bresilla | 2024-12-03 16:08:25 +0100 |
|---|---|---|
| committer | lukeflo | 2024-12-23 21:03:19 +0100 |
| commit | 9c0ad228a34d842cf21e88f8d99e5c2b5f51e93c (patch) | |
| tree | 26fbfe5f7d85cfe3f5124d7737bc3fec0f30073b /src/bibiman.rs | |
| parent | 0c32c96fcfb8daa1c5061596542f76d355f27acd (diff) | |
| download | bibiman-9c0ad228a34d842cf21e88f8d99e5c2b5f51e93c.tar.gz bibiman-9c0ad228a34d842cf21e88f8d99e5c2b5f51e93c.zip | |
feat: add 'file={}' entry in BibTex
- Improve error handling for failed file appends by formatting content before appending.
- Modify the `format_bibtex_entry` function to accept a file path argument.
- Add a new `file` field to the formatted BibTeX entry.
- Ensure the formatted content is written to the file correctly.
Diffstat (limited to 'src/bibiman.rs')
| -rw-r--r-- | src/bibiman.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs index 778048f..19eaf4b 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -131,7 +131,9 @@ impl Bibiman { if let AOk(entry) = new_entry { // TODO: Add error handling for failed insert - if self.append_to_file(args, &entry.to_string()).is_err() { + 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(); } @@ -429,21 +431,21 @@ impl Bibiman { // Optionally, add a newline before the content file.write_all(b"\n")?; // Format the content - let formatted_content = Self::format_bibtex_entry(content); // Write the formatted content to the file - file.write_all(formatted_content.as_bytes())?; + file.write_all(content.as_bytes())?; // Update the database and the lists to reflect the new content self.update_lists(args); Ok(()) } /// Formats a raw BibTeX entry string for better readability. - pub fn format_bibtex_entry(entry: &str) -> String { + pub fn format_bibtex_entry(entry: &str, file_path: &str) -> String { let mut formatted = String::new(); // Find the position of the first '{' if let Some(start_brace_pos) = entry.find('{') { // Copy the preamble (e.g., '@article{') let preamble = &entry[..start_brace_pos + 1]; + let preamble = preamble.trim_start(); formatted.push_str(preamble); formatted.push('\n'); // Add newline // Now get the content inside the braces @@ -490,6 +492,10 @@ impl Bibiman { fields.push(current_field.trim().to_string()); } + // Add the new 'file' field + let file_field = format!("file = {{{}}}", file_path); + fields.push(file_field); + // Now reconstruct the entry with proper indentation for (i, field) in fields.iter().enumerate() { formatted.push_str(" "); |
