diff options
Diffstat (limited to 'src/bibiman.rs')
| -rw-r--r-- | src/bibiman.rs | 96 |
1 files changed, 68 insertions, 28 deletions
diff --git a/src/bibiman.rs b/src/bibiman.rs index f95bbc0..200db96 100644 --- a/src/bibiman.rs +++ b/src/bibiman.rs @@ -25,7 +25,7 @@ use crate::tui::Tui; use crate::{app, cliargs}; use crate::{bibiman::entries::EntryTable, bibiman::keywords::TagList}; use arboard::Clipboard; -use color_eyre::eyre::{Error, Result}; +use color_eyre::eyre::{Context, Error, Result}; use crossterm::event::KeyCode; use editor_command::EditorBuilder; use ratatui::widgets::ScrollbarState; @@ -35,7 +35,7 @@ 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; @@ -520,35 +520,63 @@ impl Bibiman { 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); - } + match std::env::var("TERM") { + Ok(sh) => { + let editor = if let Some(e) = cfg.general.editor.clone() { + e + } else if let Ok(e) = std::env::var("VISUAL") { + e + } else if let Ok(e) = std::env::var("EDITOR") { + e + } else { + String::from("vi") + }; + let _ = Command::new(sh) + .arg("-e") + .arg(editor) + .arg(file) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .wrap_err("Couldn't run editor"); + // Prepare arguments to open file at specific line + // let status = note_cmd.status()?; + // if !status.success() { + // eprintln!("Spawning editor failed with status {}", status); + // } + } + Err(_e) => { + 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 note_cmd: Command = EditorBuilder::new() + .source(cfg.general.editor.clone()) + .environment() + .source(Some("vi")) + .build() + .unwrap(); + // Prepare arguments to open file at specific line + let status = note_cmd.arg(file).status()?; + if !status.success() { + eprintln!("Spawning editor failed with status {}", status); + } - // Enter TUI again - tui.enter()?; - tui.terminal.clear()?; + // Enter TUI again + tui.enter()?; + tui.terminal.clear()?; - // Update the database and the lists to show changes - // Self::update_lists(self, cfg); + // 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); + // Select entry which was selected before entering editor + self.select_entry_by_citekey(citekey); + } + } Ok(()) } @@ -808,7 +836,19 @@ impl Bibiman { .filepath .clone(); if file.is_some() { - let file = expand_home(&PathBuf::from(file.unwrap()[0].clone())); + let file = if self.entry_table.entry_table_items[entry_idx].file_field + && cfg.general.file_prefix.is_some() + { + cfg.general + .file_prefix + .clone() + .unwrap() + .join(&file.unwrap()[0]) + .into_os_string() + } else { + file.unwrap()[0].clone() + }; + let file = expand_home(&PathBuf::from(file)); // let object: OsString = popup_entry.into(); if file.is_file() { app::open_connected_file(cfg, &file.into_os_string())?; |
