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 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 17 deletions(-) (limited to 'src') 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( -- cgit v1.2.3