Skip to content

Commit

Permalink
Show snippet in variable prompt (#337)
Browse files Browse the repository at this point in the history
Fixes #320
  • Loading branch information
denisidoro authored Apr 11, 2020
1 parent 6b9d97b commit 4451624
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
45 changes: 39 additions & 6 deletions src/flows/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
};

Expand All @@ -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()
};

Expand Down Expand Up @@ -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());
Expand Down
7 changes: 4 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -43,7 +44,7 @@ mod tests {
#[test]
fn test_meta_github_ssh() {
let (actual_uri, user, repo) = meta("[email protected]:denisidoro/navi.git");
assert_eq!(actual_uri, "[email protected]/denisidoro/navi.git".to_string());
assert_eq!(actual_uri, "[email protected]:denisidoro/navi.git".to_string());
assert_eq!(user, "denisidoro".to_string());
assert_eq!(repo, "navi".to_string());
}
Expand Down
5 changes: 4 additions & 1 deletion tests/cheats/more_cases.cheat
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ cat "<file>"
# with map
echo "<mapped>"

# empty
echo "http://google.com?q=<query>"

# fzf
ls / | fzf

Expand All @@ -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

0 comments on commit 4451624

Please sign in to comment.