Skip to content

Commit

Permalink
adds @ opeator to toggle echoing. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
KnorrFG committed Nov 19, 2023
1 parent 671fbc6 commit d61c144
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dotree"
version = "0.7.0"
version = "0.8.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,26 @@ menu brightnessctl {
...
```

### Echoing Commands

By default, dotree will echo the command it executes to stderr (this behavior change was introduced in 0.8.0).
If you don't like that, you can add `echo off` to the top of the file, like this:

```
echo off
menu root {
n: "echo don\'t show me"
y: "echo 'show me' without echoing the command" - @"echo show me"
}
```

This will switch the default behavior to not echoing the executed command. Before a command, you can
add an `@`, which will toggle the default behavior, i.e. if you have an `echo off` at the top of
your file, then commands with `@` will be echoed. If you do not have `echo off` at the top of the file
`@` will supress echoing.


### Naming Menus

You can also assign a different display name to a menu, like this:
Expand Down
4 changes: 3 additions & 1 deletion grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ QUOTE = _{ "\"" }
protected_string = ${(sep_start ~ protected_content ~ sep_end)}
protected_content = @{ (!("\"" ~ PEEK ~ "!") ~ ANY)* }

quick_command = {(string ~ "-")? ~ string_expr}
quick_command = {command_name? ~ ECHO_TOGGLE_TOKEN? ~ string_expr}
command_name = { (string ~ "-") }
ECHO_TOGGLE_TOKEN = {"@"}

sep_start = _{ EXCL ~ PUSH((!"\"" ~ ANY)*) ~ QUOTE}
sep_end = _{ QUOTE ~ POP ~ EXCL }
Expand Down
2 changes: 1 addition & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn run_command(
cmd.settings.contains(&CommandSetting::IgnoreResult),
)
} else {
if rt_conf::settings().echo_by_default {
if rt_conf::settings().echo_by_default != cmd.toggle_echo_setting {
eprintln!("{arg}");
}
exec_cmd(&shell.name, args)
Expand Down
39 changes: 27 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Command {
pub name: Option<String>,
pub shell: Option<ShellDef>,
pub env_vars: Vec<VarDef>,
pub toggle_echo_setting: bool,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
Expand Down Expand Up @@ -225,13 +226,14 @@ fn parse_menu(name: &str, menus: &HashMap<&str, RawMenu<'_>>) -> Result<Menu> {
)
}
Rule::quick_command => {
let (display_name, exec_str) = parse_quick_command(child_pair);
let (display_name, toggle_echo_setting, exec_str) = parse_quick_command(child_pair);
Node::Command(Command {
exec_str,
name: display_name,
settings: vec![],
env_vars: vec![],
shell: None,
toggle_echo_setting,
})
}
Rule::anon_command => Node::Command(parse_anon_command(child_pair)),
Expand Down Expand Up @@ -283,13 +285,14 @@ impl CmdBodyParser {
None
}
Rule::quick_command => {
let (display_name, exec_str) = parse_quick_command(p);
let (display_name, toggle_echo_setting, exec_str) = parse_quick_command(p);
Some(Command {
exec_str,
settings: self.settings.take().unwrap_or_default(),
name: display_name,
env_vars: self.vars.take().unwrap_or_default(),
shell: self.shell_def.take(),
toggle_echo_setting,
})
}
_ => panic!("unexpected rule: {p:#?}"),
Expand Down Expand Up @@ -330,17 +333,21 @@ fn parse_vars_def(p: Pair<'_, Rule>) -> Vec<VarDef> {
p.into_inner().map(parse_var_def).collect()
}

fn parse_quick_command(pair: Pair<'_, Rule>) -> (Option<String>, StringExpr) {
fn parse_quick_command(pair: Pair<'_, Rule>) -> (Option<String>, bool, StringExpr) {
assert!(pair.as_rule() == Rule::quick_command);
let elems: Vec<_> = pair.into_inner().collect();
match elems.len() {
1 => (None, parse_string_expr(elems[0].clone())),
2 => (
Some(from_string(elems[0].clone())),
parse_string_expr(elems[1].clone()),
),
_ => panic!("unexpected amount of string"),
let mut name = None;
let mut toggle_echo = false;
let mut str_expr = None;

for elem in pair.into_inner() {
match elem.as_rule() {
Rule::command_name => name = Some(from_string(elem.inext())),
Rule::ECHO_TOGGLE_TOKEN => toggle_echo = true,
Rule::string_expr => str_expr = Some(parse_string_expr(elem)),
_ => panic!("unexpected pair: {elem:#?}"),
}
}
(name, toggle_echo, str_expr.unwrap())
}

fn parse_string_expr(p: Pair<'_, Rule>) -> StringExpr {
Expand Down Expand Up @@ -472,7 +479,7 @@ mod tests {
menu custom_commands {
h: "print hi" - !"echo hi"!
c: "echo ciao"
c: @"echo ciao"
}
"#;

Expand Down Expand Up @@ -567,6 +574,7 @@ Config {
name: None,
shell: None,
env_vars: [],
toggle_echo_setting: true,
},
),
[
Expand All @@ -586,6 +594,7 @@ Config {
),
shell: None,
env_vars: [],
toggle_echo_setting: false,
},
),
},
Expand All @@ -606,6 +615,7 @@ Config {
name: None,
shell: None,
env_vars: [],
toggle_echo_setting: false,
},
),
},
Expand Down Expand Up @@ -679,6 +689,7 @@ Ok(
name: None,
shell: None,
env_vars: [],
toggle_echo_setting: false,
},
),
},
Expand Down Expand Up @@ -730,6 +741,7 @@ Config {
value: None,
},
],
toggle_echo_setting: false,
},
),
},
Expand Down Expand Up @@ -780,6 +792,7 @@ Config {
name: None,
shell: None,
env_vars: [],
toggle_echo_setting: false,
},
),
},
Expand Down Expand Up @@ -826,6 +839,7 @@ Config {
name: None,
shell: None,
env_vars: [],
toggle_echo_setting: false,
},
),
},
Expand Down Expand Up @@ -870,6 +884,7 @@ Config {
name: None,
shell: None,
env_vars: [],
toggle_echo_setting: false,
},
),
},
Expand Down
3 changes: 2 additions & 1 deletion tests/bash_tests/echo_setting_off.dt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
echo off

menu root {
f: "echo foo"
n: "echo don\'t show me"
y: "echo 'show me' without echoing the command" - @"echo show me"
}
3 changes: 2 additions & 1 deletion tests/bash_tests/echo_setting_on.dt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
echo on

menu root {
f: "echo foo"
y: "echo show me"
n: @"echo don\'t show me"
}
6 changes: 4 additions & 2 deletions tests/bash_tests/echo_settings.bash
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
$DT -c echo_setting_on.dt f
$DT -c echo_setting_off.dt f
$DT -c echo_setting_on.dt y
$DT -c echo_setting_on.dt n
$DT -c echo_setting_off.dt y
$DT -c echo_setting_off.dt n
9 changes: 6 additions & 3 deletions tests/bash_tests/echo_settings.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[?25l[?25hecho foo
foo
[?25l[?25hfoo
[?25l[?25hecho show me
show me
[?25l[?25hdon't show me
[?25l[?25hecho show me
show me
[?25l[?25hdon't show me

0 comments on commit d61c144

Please sign in to comment.