diff options
author | Denis Isidoro <denisidoro@users.noreply.github.com> | 2020-04-11 11:06:34 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-11 11:06:34 -0300 |
commit | 4451624ce0179823c9bd616362f24f2dc75501b8 (patch) | |
tree | 00d3a176f2c79f259d7fc07f3c790bf92a1f9f67 | |
parent | 6b9d97bc090a5ddf22c152b8e05fb09063ab6cef (diff) |
Show snippet in variable prompt (#337)v2.5.0
Fixes #320
-rw-r--r-- | src/flows/core.rs | 45 | ||||
-rw-r--r-- | src/git.rs | 7 | ||||
-rw-r--r-- | tests/cheats/more_cases.cheat | 5 |
3 files changed, 47 insertions, 10 deletions
diff --git a/src/flows/core.rs b/src/flows/core.rs index f78d026..951c716 100644 --- a/src/flows/core.rs +++ b/src/flows/core.rs @@ -72,10 +72,11 @@ fn extract_from_selections(raw_snippet: &str, contains_key: bool) -> (&str, &str } fn prompt_with_suggestions( - varname: &str, + variable_name: &str, config: &Config, suggestion: &Suggestion, values: &HashMap<String, String>, + snippet: String, ) -> Result<String, Error> { let mut vars_cmd = String::from(""); for (key, value) in values.iter() { @@ -99,11 +100,21 @@ fn prompt_with_suggestions( ) .context("Suggestions are invalid utf8")?; - let opts = suggestion_opts.clone().unwrap_or_default(); + let mut opts = suggestion_opts.clone().unwrap_or_default(); + if opts.preview.is_none() { + opts.preview = Some(format!( + "echo '{}' | sed 's/<{}>/{{}}/g'", + snippet.replace('\'', "\""), + 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(), - prompt: Some(display::variable_prompt(varname)), + prompt: Some(display::variable_prompt(variable_name)), ..opts }; @@ -120,11 +131,21 @@ fn prompt_with_suggestions( Ok(output) } -fn prompt_without_suggestions(variable_name: &str, config: &Config) -> Result<String, Error> { +fn prompt_without_suggestions( + variable_name: &str, + config: &Config, + snippet: String, +) -> Result<String, Error> { let opts = FinderOpts { autoselect: false, prompt: Some(display::variable_prompt(variable_name)), suggestion_type: SuggestionType::Disabled, + preview: Some(format!( + "echo '{}' | sed 's/<{}>/{{}}/g'", + snippet.replace('\'', "\""), + variable_name + )), + preview_window: Some("up:1".to_string()), ..Default::default() }; @@ -158,9 +179,21 @@ fn replace_variables_from_snippet( .get(&tags, &variable_name) .ok_or_else(|| anyhow!("No suggestions")) .and_then(|suggestion| { - prompt_with_suggestions(variable_name, &config, suggestion, &values) + prompt_with_suggestions( + variable_name, + &config, + suggestion, + &values, + interpolated_snippet.clone(), + ) + }) + .or_else(|_| { + prompt_without_suggestions( + variable_name, + &config, + interpolated_snippet.clone(), + ) }) - .or_else(|_| prompt_without_suggestions(variable_name, &config)) })?; values.insert(variable_name.to_string(), value.clone()); @@ -21,11 +21,12 @@ pub fn meta(uri: &str) -> (String, String, String) { format!("https://github.com/{}", uri) }; - let parts: Vec<&str> = actual_uri.split('/').collect(); + let uri_to_split = actual_uri.replace(':', "/"); + let parts: Vec<&str> = uri_to_split.split('/').collect(); let user = parts[parts.len() - 2]; let repo = parts[parts.len() - 1].replace(".git", ""); - (actual_uri.clone(), user.to_string(), repo) + (actual_uri, user.to_string(), repo) } #[cfg(test)] @@ -43,7 +44,7 @@ mod tests { #[test] fn test_meta_github_ssh() { let (actual_uri, user, repo) = meta("git@github.com:denisidoro/navi.git"); - assert_eq!(actual_uri, "git@github.com/denisidoro/navi.git".to_string()); + assert_eq!(actual_uri, "git@github.com:denisidoro/navi.git".to_string()); assert_eq!(user, "denisidoro".to_string()); assert_eq!(repo, "navi".to_string()); } diff --git a/tests/cheats/more_cases.cheat b/tests/cheats/more_cases.cheat index 898a052..46e3992 100644 --- a/tests/cheats/more_cases.cheat +++ b/tests/cheats/more_cases.cheat @@ -38,6 +38,9 @@ cat "<file>" # with map echo "<mapped>" +# empty +echo "http://google.com?q=<query>" + # fzf ls / | fzf @@ -51,7 +54,7 @@ $ langs: echo 'clojure rust javascript' | tr ' ' '\n' --- --multi $ mapped: echo 'true false' | tr ' ' '\n' --- --map "[[ $0 == t* ]] && echo 1 || echo 0" $ examples: echo -e 'foo bar\nlorem ipsum\ndolor sit' --- --multi $ multiword: echo -e 'foo bar\nlorem ipsum\ndolor sit\nbaz'i -$ file: ls . --- --preview 'cat {}' --preview-window '50%' +$ file: ls . --- --preview 'cat {}' --preview-window 'right:50%' # this should be displayed echo hi
\ No newline at end of file |