From 30fbb305142931ffc352bf1fb7c62052d4289360 Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Thu, 22 Apr 2021 09:44:28 -0300 Subject: [PATCH] Use absolute path for navi when possible (#535) --- src/actor.rs | 20 +++++++++++++++----- src/finder/structures.rs | 2 +- src/fs.rs | 6 +++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 87e9030f..8a411c0e 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -75,7 +75,7 @@ fn prompt_finder( o }; - let exe = fs::exe_string()?; + let exe = fs::exe_string(); let extra = extra_preview.clone().unwrap_or_default(); let preview = if cfg!(target_os = "windows") { @@ -180,6 +180,13 @@ fn replace_variables_from_snippet(snippet: &str, tags: &str, variables: Variable Ok(interpolated_snippet) } +pub fn with_absolute_path(snippet: String) -> String { + if let Some(s) = snippet.strip_prefix("navi ") { + return format!("{} {}", fs::exe_string(), s); + } + snippet +} + pub fn act( extractions: Result, files: Vec, @@ -197,14 +204,17 @@ pub fn act( env_var::set(env_var::PREVIEW_TAGS, &tags); env_var::set(env_var::PREVIEW_COMMENT, &comment); - let interpolated_snippet = writer::with_new_lines( - replace_variables_from_snippet( + let interpolated_snippet = { + let mut s = replace_variables_from_snippet( snippet, tags, variables.expect("No variables received from finder"), ) - .context("Failed to replace variables from snippet")?, - ); + .context("Failed to replace variables from snippet")?; + s = with_absolute_path(s); + s = writer::with_new_lines(s); + s + }; match CONFIG.action() { Action::Print => { diff --git a/src/finder/structures.rs b/src/finder/structures.rs index 1a44d5f8..1321fdb5 100644 --- a/src/finder/structures.rs +++ b/src/finder/structures.rs @@ -54,7 +54,7 @@ pub enum SuggestionType { impl Opts { pub fn from_config(config: &Config) -> Result { let opts = Opts { - preview: Some(format!("{} preview {{}}", filesystem::exe_string()?)), + preview: Some(format!("{} preview {{}}", filesystem::exe_string())), overrides: config.fzf_overrides(), suggestion_type: SuggestionType::SnippetSelection, query: if config.best_match() { diff --git a/src/fs.rs b/src/fs.rs index f627446d..5fd2b4db 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -67,10 +67,14 @@ fn exe_pathbuf() -> Result { follow_symlink(pathbuf) } -pub fn exe_string() -> Result { +fn exe_abs_string() -> Result { pathbuf_to_string(&exe_pathbuf()?) } +pub fn exe_string() -> String { + exe_abs_string().unwrap_or_else(|_| "navi".to_string()) +} + pub fn create_dir(path: &Path) -> Result<()> { create_dir_all(path).with_context(|| { format!(