From cc2007c6744de467c91038e4ccd65f0985f2e89b Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Mon, 20 Apr 2020 18:29:48 -0300 Subject: [PATCH] Refactor variable dependency code (#350) --- scripts/fix | 2 +- src/flows/core.rs | 66 +++++++++++++++++------------------------------ 2 files changed, 25 insertions(+), 43 deletions(-) diff --git a/scripts/fix b/scripts/fix index afa82a7..4f5eb14 100755 --- a/scripts/fix +++ b/scripts/fix @@ -7,7 +7,7 @@ source "${NAVI_HOME}/scripts/install" cd "$NAVI_HOME" header "Cargo nighly fix..." -cargo +nightly fix --clippy -Z unstable-options || true +cargo +nightly clippy --fix -Z unstable-options || true header "Cargo fix..." cargo fix || true diff --git a/src/flows/core.rs b/src/flows/core.rs index a63bfc8..bacd7aa 100644 --- a/src/flows/core.rs +++ b/src/flows/core.rs @@ -11,7 +11,7 @@ use crate::structures::{error::command::BashSpawnError, option::Config}; use anyhow::Context; use anyhow::Error; use regex::Regex; -use std::collections::HashMap; +use std::env; use std::fs; use std::io::Write; use std::process::{Command, Stdio}; @@ -83,22 +83,16 @@ fn prompt_with_suggestions( variable_name: &str, config: &Config, suggestion: &Suggestion, - values: &HashMap, _snippet: String, ) -> Result { - let mut vars_cmd = String::from(""); - for (key, value) in values.iter() { - vars_cmd.push_str(format!("{}=\"{}\"; ", key, value).as_str()); - } let (suggestion_command, suggestion_opts) = suggestion; - let command = format!("{} {}", vars_cmd, suggestion_command); let child = Command::new("bash") .stdout(Stdio::piped()) .arg("-c") - .arg(&command) + .arg(&suggestion_command) .spawn() - .map_err(|e| BashSpawnError::new(command, e))?; + .map_err(|e| BashSpawnError::new(suggestion_command, e))?; let suggestions = String::from_utf8( child @@ -109,12 +103,6 @@ fn prompt_with_suggestions( .context("Suggestions are invalid utf8")?; let opts = suggestion_opts.clone().unwrap_or_default(); - /* if opts.preview.is_none() { - opts.preview = gen_opts_preview(&snippet, &variable_name); - } - if opts.preview_window.is_none() { - opts.preview_window = Some("up:1".to_string()); - } */ let opts = FinderOpts { autoselect: !config.no_autoselect, overrides: config.fzf_overrides_var.clone(), @@ -164,39 +152,33 @@ fn replace_variables_from_snippet( config: &Config, ) -> Result { let mut interpolated_snippet = String::from(snippet); - let mut values: HashMap = HashMap::new(); for captures in VAR_REGEX.captures_iter(snippet) { let bracketed_variable_name = &captures[0]; let variable_name = &bracketed_variable_name[1..bracketed_variable_name.len() - 1]; - let value = values - .get(variable_name) - .map(|s| s.to_string()) - .ok_or_else(|| anyhow!(format!("No value for variable `{}`", variable_name))) - .or_else(|_| { - variables - .get(&tags, &variable_name) - .ok_or_else(|| anyhow!("No suggestions")) - .and_then(|suggestion| { - prompt_with_suggestions( - variable_name, - &config, - suggestion, - &values, - interpolated_snippet.clone(), - ) - }) - .or_else(|_| { - prompt_without_suggestions( - variable_name, - &config, - interpolated_snippet.clone(), - ) - }) - })?; + let env_value = env::var(variable_name); - values.insert(variable_name.to_string(), value.clone()); + let value = if let Ok(e) = env_value { + e + } else { + variables + .get(&tags, &variable_name) + .ok_or_else(|| anyhow!("No suggestions")) + .and_then(|suggestion| { + prompt_with_suggestions( + variable_name, + &config, + suggestion, + interpolated_snippet.clone(), + ) + }) + .or_else(|_| { + prompt_without_suggestions(variable_name, &config, interpolated_snippet.clone()) + })? + }; + + env::set_var(variable_name, &value); interpolated_snippet = if value.as_str() == "\n" { interpolated_snippet.replacen(bracketed_variable_name, "", 1)