From b10615ade6bb2710cf6716f05cc496cb082d24ad Mon Sep 17 00:00:00 2001 From: lukeflo Date: Mon, 30 Jun 2025 10:43:38 +0200 Subject: opening notes in set editor --- src/app.rs | 16 +------ src/bibiman.rs | 47 +++++++++++++++++++-- tests/biblatex-test.bib | 12 +++--- tests/note-files/aristotle:poetics.txt | 0 tests/note-files/aristotle:rhetoric.md | 0 tests/note-files/aristotle_poetics.txt | 1 + tests/note-files/aristotle_rhetoric.md | 0 .../annotated-pdfs/ARIStotle:rheTORIC.PDF | Bin 25294 -> 0 bytes .../annotated-pdfs/ARIStotle_rheTORIC.PDF | Bin 0 -> 25294 bytes tests/pdf-files/aristotle:physics.pdf | Bin 25294 -> 0 bytes tests/pdf-files/aristotle:rhetoric.pdf | Bin 25294 -> 0 bytes tests/pdf-files/aristotle:rhetoric.txt | 0 tests/pdf-files/aristotle_physics.pdf | Bin 0 -> 25294 bytes tests/pdf-files/aristotle_rhetoric.pdf | Bin 0 -> 25294 bytes tests/pdf-files/aristotle_rhetoric.txt | 0 15 files changed, 53 insertions(+), 23 deletions(-) delete mode 100644 tests/note-files/aristotle:poetics.txt delete mode 100644 tests/note-files/aristotle:rhetoric.md create mode 100644 tests/note-files/aristotle_poetics.txt create mode 100644 tests/note-files/aristotle_rhetoric.md delete mode 100644 tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF create mode 100644 tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF delete mode 100644 tests/pdf-files/aristotle:physics.pdf delete mode 100644 tests/pdf-files/aristotle:rhetoric.pdf delete mode 100644 tests/pdf-files/aristotle:rhetoric.txt create mode 100644 tests/pdf-files/aristotle_physics.pdf create mode 100644 tests/pdf-files/aristotle_rhetoric.pdf create mode 100644 tests/pdf-files/aristotle_rhetoric.txt diff --git a/src/app.rs b/src/app.rs index f015494..496896a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -18,6 +18,7 @@ use crate::bibiman::CurrentArea; use crate::config::BibiConfig; use color_eyre::eyre::{Context, Ok, Result}; +use editor_command::EditorBuilder; // use super::Event; use crate::cliargs::CLIArgs; use crate::tui::commands::InputCmdAction; @@ -279,7 +280,7 @@ 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 { - self.bibiman.open_connected_res(cfg)?; + self.bibiman.open_connected_res(cfg, tui)?; } else if let Some(PopupKind::AppendToFile) = self.bibiman.popup_area.popup_kind { self.bibiman.append_entry_to_file(cfg)? @@ -423,19 +424,6 @@ pub fn open_connected_file(cfg: &BibiConfig, file: &OsStr) -> Result<()> { Ok(()) } -pub fn open_connected_note(cfg: &BibiConfig, file: &OsStr) -> Result<()> { - // let cmd = cfg.general.editor.as_ref().unwrap(); - - let _ = Command::new("xdg-open") - .arg(file) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .wrap_err("Opening file not possible"); - - Ok(()) -} - pub fn open_connected_link(cfg: &BibiConfig, link: &str) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard let cmd = &cfg.general.url_opener; diff --git a/src/bibiman.rs b/src/bibiman.rs index 1f19b24..e1e97ed 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -30,11 +30,12 @@ use crossterm::event::KeyCode; use editor_command::EditorBuilder; use ratatui::widgets::ScrollbarState; use regex::Regex; +use std::ffi::OsStr; use std::fs::{self, read_to_string}; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::PathBuf; -use std::process::Command; +use std::process::{Command, Stdio}; use std::result::Result::Ok; use tui_input::Input; @@ -512,6 +513,46 @@ impl Bibiman { Ok(()) } + pub fn open_connected_note( + &mut self, + cfg: &BibiConfig, + tui: &mut Tui, + file: &OsStr, + ) -> Result<()> { + // get filecontent and citekey for calculating line number + let citekey: &str = &self.entry_table.entry_table_items + [self.entry_table.entry_table_state.selected().unwrap()] + .citekey + .clone(); + + // Exit TUI to enter editor + tui.exit()?; + // Use VISUAL or EDITOR. Set "vi" as last fallback + let mut cmd: Command = EditorBuilder::new() + .source(cfg.general.editor.as_ref()) + .environment() + .source(Some("vi")) + .build() + .unwrap(); + // Prepare arguments to open file at specific line + let status = cmd.arg(file).status()?; + if !status.success() { + eprintln!("Spawning editor failed with status {}", status); + } + + // Enter TUI again + tui.enter()?; + tui.terminal.clear()?; + + // Update the database and the lists to show changes + // Self::update_lists(self, cfg); + + // Select entry which was selected before entering editor + self.select_entry_by_citekey(citekey); + + Ok(()) + } + pub fn add_entry(&mut self) { if let CurrentArea::EntryArea = self.current_area { self.former_area = Some(FormerArea::EntryArea); @@ -662,7 +703,7 @@ impl Bibiman { Ok(()) } - pub fn open_connected_res(&mut self, cfg: &BibiConfig) -> Result<()> { + pub fn open_connected_res(&mut self, cfg: &BibiConfig, tui: &mut Tui) -> Result<()> { // Index of selected entry let entry_idx = self.entry_table.entry_table_state.selected().unwrap(); @@ -696,7 +737,7 @@ impl Bibiman { let file = expand_home(&PathBuf::from(popup_entry.clone())); // let object: OsString = popup_entry.into(); if file.is_file() { - app::open_connected_note(cfg, &file.into_os_string())?; + self.open_connected_note(cfg, tui, &file.into_os_string())?; self.close_popup(); } else { self.open_popup( diff --git a/tests/biblatex-test.bib b/tests/biblatex-test.bib index 4071dcb..692375e 100644 --- a/tests/biblatex-test.bib +++ b/tests/biblatex-test.bib @@ -9,7 +9,7 @@ model of particle physics.}, } -@collection{matuz:doody, +@collection{matuz_doody, title = {Contemporary Literary Criticism}, year = {1990}, location = {Detroit}, @@ -54,7 +54,7 @@ field}, } -@book{aristotle:anima, +@book{aristotle_anima, title = {De Anima}, author = {Aristotle}, location = {Cambridge}, @@ -68,7 +68,7 @@ editor}}, } -@book{aristotle:physics, +@book{aristotle_physics, title = {Physics}, shorttitle = {Physics}, author = {Aristotle}, @@ -84,7 +84,7 @@ annotation = {A \texttt{book} entry with a \texttt{translator} field}, } -@book{aristotle:poetics, +@book{aristotle_poetics, title = {Poetics}, shorttitle = {Poetics}, author = {Aristotle}, @@ -100,7 +100,7 @@ editor} as well as a \texttt{series} field}, } -@mvbook{aristotle:rhetoric, +@mvbook{aristotle_rhetoric, title = {The Rhetoric of {Aristotle} with a commentary by the late {Edward Meredith Cope}}, shorttitle = {Rhetoric}, @@ -441,7 +441,7 @@ @string{pup = {Princeton University Press}} -@incollection{westfahl:space, +@incollection{westfahl_space, title = {The True Frontier}, author = {Westfahl, Gary}, pages = {55--65}, diff --git a/tests/note-files/aristotle:poetics.txt b/tests/note-files/aristotle:poetics.txt deleted file mode 100644 index e69de29..0000000 diff --git a/tests/note-files/aristotle:rhetoric.md b/tests/note-files/aristotle:rhetoric.md deleted file mode 100644 index e69de29..0000000 diff --git a/tests/note-files/aristotle_poetics.txt b/tests/note-files/aristotle_poetics.txt new file mode 100644 index 0000000..a156c76 --- /dev/null +++ b/tests/note-files/aristotle_poetics.txt @@ -0,0 +1 @@ +Here some very boring information regarding Aristotle diff --git a/tests/note-files/aristotle_rhetoric.md b/tests/note-files/aristotle_rhetoric.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF b/tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF deleted file mode 100644 index 6aaba88..0000000 Binary files a/tests/pdf-files/annotated-pdfs/ARIStotle:rheTORIC.PDF and /dev/null differ diff --git a/tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF b/tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF new file mode 100644 index 0000000..6aaba88 Binary files /dev/null and b/tests/pdf-files/annotated-pdfs/ARIStotle_rheTORIC.PDF differ diff --git a/tests/pdf-files/aristotle:physics.pdf b/tests/pdf-files/aristotle:physics.pdf deleted file mode 100644 index 6aaba88..0000000 Binary files a/tests/pdf-files/aristotle:physics.pdf and /dev/null differ diff --git a/tests/pdf-files/aristotle:rhetoric.pdf b/tests/pdf-files/aristotle:rhetoric.pdf deleted file mode 100644 index 6aaba88..0000000 Binary files a/tests/pdf-files/aristotle:rhetoric.pdf and /dev/null differ diff --git a/tests/pdf-files/aristotle:rhetoric.txt b/tests/pdf-files/aristotle:rhetoric.txt deleted file mode 100644 index e69de29..0000000 diff --git a/tests/pdf-files/aristotle_physics.pdf b/tests/pdf-files/aristotle_physics.pdf new file mode 100644 index 0000000..6aaba88 Binary files /dev/null and b/tests/pdf-files/aristotle_physics.pdf differ diff --git a/tests/pdf-files/aristotle_rhetoric.pdf b/tests/pdf-files/aristotle_rhetoric.pdf new file mode 100644 index 0000000..6aaba88 Binary files /dev/null and b/tests/pdf-files/aristotle_rhetoric.pdf differ diff --git a/tests/pdf-files/aristotle_rhetoric.txt b/tests/pdf-files/aristotle_rhetoric.txt new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3