mirror of
https://github.com/denisidoro/navi
synced 2024-11-10 14:04:17 +00:00
parent
c5b3b175f6
commit
f6a4fec13d
5 changed files with 35 additions and 8 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -89,7 +89,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
|
||||
[[package]]
|
||||
name = "navi"
|
||||
version = "2.0.9"
|
||||
version = "2.0.10"
|
||||
dependencies = [
|
||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"raw_tty 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "navi"
|
||||
version = "2.0.9"
|
||||
version = "2.0.10"
|
||||
authors = ["Denis Isidoro <denis_isidoro@live.com>"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
12
README.md
12
README.md
|
@ -30,6 +30,7 @@ Table of contents
|
|||
* [Cheatsheet syntax](#cheatsheet-syntax)
|
||||
* [Syntax overview](#syntax-overview)
|
||||
* [Variables](#variables)
|
||||
* [Variable dependency](#variable-dependency)
|
||||
* [Variable options](#variable-options)
|
||||
* [Table formatting](#table-formatting)
|
||||
* [Multiple choice](#multiple-choice)
|
||||
|
@ -162,6 +163,17 @@ The interface prompts for variable names inside brackets (eg `<branch>`).
|
|||
|
||||
Variable names should only include alphanumeric characters and `_`.
|
||||
|
||||
### Variable dependency
|
||||
|
||||
The command for generating possible inputs can refer other variables:
|
||||
```sh
|
||||
# If you select 2 for x, the possible values of y will be 12 and 22
|
||||
echo <x> <y>
|
||||
|
||||
$ x: echo -e '1\n2\n3'
|
||||
$ y: echo -e "$((x+10))\n$((x+20))"
|
||||
```
|
||||
|
||||
### Variable options
|
||||
|
||||
For lines starting with `$` you can add extra options using `---`.
|
||||
|
|
|
@ -55,11 +55,22 @@ fn extract_from_selections(raw_output: &str, contains_key: bool) -> (&str, &str,
|
|||
(key, tags, snippet)
|
||||
}
|
||||
|
||||
fn prompt_with_suggestions(config: &Config, suggestion: &cheat::Value) -> String {
|
||||
fn prompt_with_suggestions(
|
||||
config: &Config,
|
||||
suggestion: &cheat::Value,
|
||||
values: &HashMap<String, String>,
|
||||
) -> String {
|
||||
let mut vars_cmd = String::from("");
|
||||
for (k, v) in values.iter() {
|
||||
vars_cmd.push_str(format!("{}=\"{}\"; ", k, v).as_str());
|
||||
}
|
||||
|
||||
let cmd = format!("{vars} {cmd}", vars = vars_cmd, cmd = &suggestion.0);
|
||||
|
||||
let child = Command::new("bash")
|
||||
.stdout(Stdio::piped())
|
||||
.arg("-c")
|
||||
.arg(&suggestion.0)
|
||||
.arg(cmd)
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
|
@ -117,9 +128,9 @@ fn prompt_without_suggestions(varname: &str) -> String {
|
|||
|
||||
fn gen_replacement(value: &str) -> String {
|
||||
if value.contains(' ') {
|
||||
format!("\"{}\"", &value[..value.len() - 1])
|
||||
format!("\"{}\"", value)
|
||||
} else {
|
||||
value[..value.len() - 1].to_string()
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,6 +141,7 @@ fn replace_variables_from_snippet(
|
|||
config: &Config,
|
||||
) -> String {
|
||||
let mut interpolated_snippet = String::from(snippet);
|
||||
let mut values: HashMap<String, String> = HashMap::new();
|
||||
|
||||
let re = Regex::new(r"<(\w[\w\d\-_]*)>").unwrap();
|
||||
for cap in re.captures_iter(snippet) {
|
||||
|
@ -138,10 +150,12 @@ fn replace_variables_from_snippet(
|
|||
let k = format!("{};{}", tags, varname);
|
||||
|
||||
let value = match variables.get(&k[..]) {
|
||||
Some(suggestion) => prompt_with_suggestions(&config, suggestion),
|
||||
Some(suggestion) => prompt_with_suggestions(&config, suggestion, &values),
|
||||
None => prompt_without_suggestions(varname),
|
||||
};
|
||||
|
||||
values.insert(varname.to_string(), value.clone());
|
||||
|
||||
interpolated_snippet =
|
||||
interpolated_snippet.replace(bracketed_varname, gen_replacement(&value[..]).as_str());
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ where
|
|||
|
||||
let out = child.wait_with_output().unwrap();
|
||||
|
||||
let text = match out.status.code() {
|
||||
let mut text = match out.status.code() {
|
||||
Some(0) | Some(1) => String::from_utf8(out.stdout).unwrap(),
|
||||
Some(130) => process::exit(130),
|
||||
_ => {
|
||||
|
@ -132,6 +132,7 @@ where
|
|||
panic!("External command failed:\n {}", err)
|
||||
}
|
||||
};
|
||||
text.truncate(text.len() - 1);
|
||||
|
||||
(text, result)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue