Skip to content

Commit

Permalink
adds default variables
Browse files Browse the repository at this point in the history
default variables can now be declared via `var_name = "some value"`
  • Loading branch information
nthnd committed Nov 15, 2023
1 parent c2fad7b commit a4b0399
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
3 changes: 2 additions & 1 deletion grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ anon_command = { "cmd" ~ NEWLINE* ~ OPENBR
cmd_body = { ((cmd_settings|vars_def|shell_def) ~ NEWLINE)* ~ quick_command }
vars_def = { "vars" ~ var_def ~ (DEF_SEP* ~ var_def)* }
DEF_SEP = _{"," ~ NEWLINE*}
var_def = { symbol }
var_def = { symbol ~ default_var? }
default_var = { "=" ~ normal_string }
cmd_settings = { "set" ~ symbol ~ (DEF_SEP* ~ symbol)* }

shell_def = {"shell" ~ (string|word)+ }
Expand Down
28 changes: 23 additions & 5 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ fn run_command(
let val = if let Some(val) = arg_vals.get(i) {
val
} else {
history = query_env_var(var, history).context("querying env var")?;
history = query_env_var(&var.name, &var.value, history).context("querying env var")?;
history.last().unwrap()
};
// uppon calling exec, the env vars are kept, so just setting them here
// means setting them for the callee
env::set_var(var, val);
env::set_var(&var.name, val);
}
term.clear_last_lines(cmd.env_vars.len() - arg_vals.len())
.context("Clearing input lines")?;
Expand Down Expand Up @@ -233,16 +233,34 @@ struct RlHelper {
}
impl Highlighter for RlHelper {}

fn query_env_var(name: &str, mut hist: Vec<String>) -> Result<Vec<String>> {
fn query_env_var(
name: &str,
default_val: &Option<String>,
mut hist: Vec<String>,
) -> Result<Vec<String>> {
let mut rl = rustyline::Editor::new()?;
rl.set_helper(Some(RlHelper {
completer: FilenameCompleter::new(),
}));
for h in &hist {
rl.add_history_entry(h)?;
}
let line = rl.readline(&format!("Value for {name}: "))?;
hist.push(line);
let line = rl.readline(&format!(
"Value for {name}{default}: ",
default = if let Some(default_val) = default_val {
format!(" ({default_val})")
} else {
String::new()
}
))?;

if line.is_empty() {
if let Some(default_val) = default_val {
hist.push(default_val.to_string());
}
} else {
hist.push(line);
}
Ok(hist)
}

Expand Down
35 changes: 26 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct Command {
pub settings: Vec<CommandSetting>,
pub name: Option<String>,
pub shell: Option<ShellDef>,
pub env_vars: Vec<String>,
pub env_vars: Vec<VarDef>,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand All @@ -47,6 +47,12 @@ pub struct ShellDef {
pub args: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct VarDef {
pub name: String,
pub value: Option<String>,
}

#[derive(Debug, Clone)]
struct RawMenu<'a> {
display_name: Option<String>,
Expand Down Expand Up @@ -231,7 +237,7 @@ fn parse_anon_command(p: Pair<'_, Rule>) -> Command {
#[derive(Default)]
struct CmdBodyParser {
settings: Option<Vec<CommandSetting>>,
vars: Option<Vec<String>>,
vars: Option<Vec<VarDef>>,
shell_def: Option<ShellDef>,
}

Expand Down Expand Up @@ -278,14 +284,25 @@ fn parse_cmd_settings(p: Pair<'_, Rule>) -> Vec<CommandSetting> {
res
}

fn parse_vars_def(p: Pair<'_, Rule>) -> Vec<String> {
fn parse_vars_def(p: Pair<'_, Rule>) -> Vec<VarDef> {
fn parse_var_def(p: Pair<'_, Rule>) -> VarDef {
assert!(p.as_rule() == Rule::var_def, "unexpected rule: {p:#?}");
let mut p = p.into_inner();
let name_def = p.next().unwrap();
let value_def = p.next();

let name = name_def.as_str().to_string();
let value = value_def.map(|v| {
assert!(v.as_rule() == Rule::default_var, "unexpected rule: {p:#?}");
// v(default_var) -> normal_string -> normal_content
v.inext().inext().as_str().to_string()
});

VarDef { name, value }
}

assert!(p.as_rule() == Rule::vars_def);
p.into_inner()
.map(|p| {
assert!(p.as_rule() == Rule::var_def, "unexpected rule: {p:#?}");
p.inext().as_str().to_string()
})
.collect()
p.into_inner().map(parse_var_def).collect()
}

fn parse_quick_command(pair: Pair<'_, Rule>) -> (Option<String>, StringExpr) {
Expand Down

0 comments on commit a4b0399

Please sign in to comment.