Skip to content

Commit

Permalink
Use absolute path for navi when possible (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 22, 2021
1 parent baa477c commit 30fbb30
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
20 changes: 15 additions & 5 deletions src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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<extractor::Output>,
files: Vec<String>,
Expand All @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion src/finder/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum SuggestionType {
impl Opts {
pub fn from_config(config: &Config) -> Result<Opts> {
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() {
Expand Down
6 changes: 5 additions & 1 deletion src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ fn exe_pathbuf() -> Result<PathBuf> {
follow_symlink(pathbuf)
}

pub fn exe_string() -> Result<String> {
fn exe_abs_string() -> Result<String> {
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!(
Expand Down

0 comments on commit 30fbb30

Please sign in to comment.