-
Notifications
You must be signed in to change notification settings - Fork 29
feat: add previous responses as a default #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add previous responses as a default #58
Conversation
@@ -0,0 +1,19 @@ | |||
name = "Super basic" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default_from_variable
would be a better name for the file
src/definition.rs
Outdated
} | ||
|
||
fn has_template_variables<'a>(s: &'a String, _already_vars: &HashMap<String, Value>) -> Option<HashSet<&'a str>> { | ||
let re = Regex::new(r"\{\{(?:[a-zA-Z][0-9a-zA-Z_]*)\}\}").unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's going to compile that regex every time this function is called, you can lift it in some lazy static or once_cell, whatever this crate is using (or add one of these libs to it)
Some(variables) => replace_with_previous_responses( | ||
variables, &vals, &s), | ||
None => s.clone(), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think doing it that way is more complicated than it actually requires.
A default value will always be a valid Tera template as it's just a string so you can just check whether {{
and }}
are in the string and handle both cases:
- no: just use that string a default, no need to do anything
- yes: render that string as a one off template with the variables so far added to the Tera context.
got it! never used Tera before, I will address your comments asap! |
src/definition.rs
Outdated
Some(variables) => replace_with_previous_responses( | ||
variables, &vals, &s), | ||
None => s.clone(), | ||
let contains_template = has_template_variables(&s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can just check if s.contains("{{") && s.contains("}}")
rather than using regex
src/utils.rs
Outdated
context: &Context, | ||
path: Option<PathBuf>, | ||
) -> Result<String> { | ||
let mut tera = Tera::default(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's actually a better way to do that now: https://docs.rs/tera/latest/tera/struct.Tera.html#method.one_off
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: should we set the autoscape
to true
or transform it into a function parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no autoescape needed there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Keats autoscape set to false
!
Thanks, I'll merge it and tweak a bit later this week |
* feat: use previous responses as default values * chore: split in different functions * chore: update examples folder * chore: include unit test * chore: add lazy_static crate to avoid regex eval evey time * chore: change to `default-fro-variable` * chore: use tera and render template to replace values * chore: remove regex and use `s.contains` instead * chore: use `tera::one_off` function * chore: set `autoscape` to false
Description
Use a previous response/default value as value to another variable into toml file
Example
The following toml file describes the feature included in this PR, basically will be possible to use a variable template to get already assigned values from other variables. eg will be assigned to the
manifest
variable the valuemy_project-other_project-manifest.md
if the user leave the all the inputs empty, however if the user populate the variableproject_one
withtree
and variableproject_two
withleaf
and leavemanifest
variable empty then the value assigned to manifest will betree-leaf-manifest.md
.The prompt default value is updated too!

Issue
#22