From 8ba7c9daf6569f88533ef1d166034fe44f022033 Mon Sep 17 00:00:00 2001 From: fritzrehde Date: Sun, 4 Feb 2024 12:14:48 +1100 Subject: [PATCH] Some clean-up --- README.md | 23 ++++++++++++++--------- examples/file-manager.toml | 35 +++++++++++++++++------------------ src/config/keybindings/mod.rs | 8 +++----- src/config/mod.rs | 28 ++++++++++++++-------------- 4 files changed, 48 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 8bc871e..47ca879 100644 --- a/README.md +++ b/README.md @@ -105,20 +105,25 @@ The operations are separated by `+` and executed in succession (one after the ot #### Via TOML Config File -In a TOML config file, specify keybindings like so: +In a TOML config file, there are several different ways you can specify keybindings: ```toml [keybindings] -"KEY" = [ "OP" ] -"KEY" = [ "OP", "OP", "OP" ] -"KEY" = [ - "OP", - "OP" -] +# Single operation, without description +"KEY" = "OP" + +# Single operation, with description +"KEY" = { description = "DESC", operations = "OP" } + +# Multiple operations, without description +"KEY" = [ "OP_1", "OP_2", "OP_3" ] + +# Multiple operations, with description +"KEY" = { description = "DESC", operations = [ "OP_1", "OP_2", "OP_3" ] } ``` This syntax differs from the command-line syntax because using the TOML array feature is more expressive and more native to the TOML file format. -Furthermore, this allows you to use the `+` character in your commands. -It also doesn't require escaping shell specific characters like `$` in (read more [in this section](#subshell)). +Furthermore, this allows you to use the `+` character in your commands, which is not possible with the CLI arguments (since that character is interpreted as a separator). +It also doesn't require escaping shell specific characters like `$` (read more about subshell behaviour [here](#subshell)). You can find some keybinding examples in the [`examples/`](examples/) directory. diff --git a/examples/file-manager.toml b/examples/file-manager.toml index 1f8a401..b4aed2c 100644 --- a/examples/file-manager.toml +++ b/examples/file-manager.toml @@ -3,10 +3,10 @@ # Note: the `printf`s used in some commands are necessary to remove the newlines produced by their nested commands # Executed before the watched command is executed -initial-env = [ '''set-env pwd -- printf "$(pwd)"''' ] +initial-env = ['set-env pwd -- printf "$(pwd)"'] # All env variables are set as env variables in subshell where watched command is executed -watched-command = '''echo "$pwd"; ls "$pwd"''' +watched-command = 'echo "$pwd"; ls "$pwd"' header-lines = 1 # Since we reload after each operation that changes the output, a small interval is not necessary. @@ -14,41 +14,40 @@ header-lines = 1 interval = 3 [keybindings] -# Move cursor up and down -"j" = [ "cursor down 1" ] -"k" = [ "cursor up 1" ] - # Delete (multiple) files -"d" = [ +"d" = { description = "Delete (multiple) files", operations = [ 'exec -- echo "$lines" | xargs -I {} rm "$pwd/{}"', - "reload" -] + "reload", +] } # Open file (blocking) -"o" = [ 'exec -- echo "$lines" | xargs -I {} xdg-open "$pwd/{}"' ] +"o" = { description = "Open file (blocking)", operations = 'exec -- echo "$lines" | xargs -I {} xdg-open "$pwd/{}"' } # Open file (non-blocking in background) -"O" = [ 'exec & -- echo "$lines" | xargs -I {} xdg-open "$pwd/{}"' ] +"O" = { description = "Open file (non-blocking in background)", operations = 'exec & -- echo "$lines" | xargs -I {} xdg-open "$pwd/{}"' } # Edit text file in TUI editor -"e" = [ 'exec tui -- echo "$line" | xargs -I {} $EDITOR "$pwd/{}"' ] +"e" = { description = "Edit text file in TUI editor", operations = 'exec tui -- echo "$line" | xargs -I {} $EDITOR "$pwd/{}"' } # Traverse out of directories -"h" = [ +"h" = { description = "Traverse out of directories", operations = [ # Set $pwd to the parent dir of current dir 'set-env pwd -- printf "$(dirname "$pwd")"', - "reload" -] + "reload", +] } + # Traverse into directories -"l" = [ +"l" = { description = "Traverse into directories", operations = [ # Only update $pwd if it is a directory 'set-env pwd -- new_pwd="$pwd/$line"; [ -d "$new_pwd" ] && pwd="$new_pwd"; printf "$pwd"', - "reload" -] + "reload", +] } # Create a new file (with random name) # "n" = [ "exec -- touch $(mktemp new_file_XXXXXX.txt)", "reload" ] +# TODO: read-into-env not yet supported + # Create a new file # "n" = [ # "read-into-env NAME", diff --git a/src/config/keybindings/mod.rs b/src/config/keybindings/mod.rs index 7ed4caf..e4d6712 100644 --- a/src/config/keybindings/mod.rs +++ b/src/config/keybindings/mod.rs @@ -203,13 +203,11 @@ impl From for Vec { #[derive(Debug, Deserialize, Clone, PartialEq, Eq, Default)] struct Description(Option); -// TODO: not very general impl fmt::Display for Description { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match &self.0 { - Some(description) => write!(f, "{}", description)?, - None => write!(f, " ")?, - }; + if let Some(description) = &self.0 { + write!(f, "{}", description)?; + } Ok(()) } } diff --git a/src/config/mod.rs b/src/config/mod.rs index b2625f0..0e72b4f 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -428,28 +428,28 @@ impl Default for PartialConfig { "update-ui-while-blocking" = false - "keybindings-help-menu-format" = [ "key", "operations", "description" ] + "keybindings-help-menu-format" = [ "key", "description", "operations" ] [keybindings] - "ctrl+c" = { operations = "exit", description = "Exit watchbind." } - "q" = { operations = "exit", description = "Exit watchbind." } - "r" = { operations = "reload", description = "Reload the watched command manually, resets interval timer." } + "ctrl+c" = { description = "Exit watchbind", operations = "exit" } + "q" = { description = "Exit watchbind", operations = "exit" } + "r" = { description = "Reload the watched command manually, resets interval timer", operations = "reload" } # Moving around - "down" = { operations = "cursor down 1", description = "Move cursor down 1 line." } - "up" = { operations = "cursor up 1", description = "Move cursor up 1 line." } - "j" = { operations = "cursor down 1", description = "Move cursor down 1 line." } - "k" = { operations = "cursor up 1", description = "Move cursor up 1 line." } - "g" = { operations = "cursor first", description = "Move cursor to the first line." } - "G" = { operations = "cursor last", description = "Move cursor to the last line." } + "down" = { description = "Move cursor down 1 line", operations = "cursor down 1" } + "up" = { description = "Move cursor up 1 line", operations = "cursor up 1" } + "j" = { description = "Move cursor down 1 line", operations = "cursor down 1" } + "k" = { description = "Move cursor up 1 line", operations = "cursor up 1" } + "g" = { description = "Move cursor to the first line", operations = "cursor first" } + "G" = { description = "Move cursor to the last line" , operations = "cursor last" } # Selecting lines - "space" = { operations = [ "toggle-selection", "cursor down 1" ], description = "Toggle selection of line that cursor is currently on, and move cursor down 1 line." } - "v" = { operations = "select", description = "Select line that cursor is currently on." } - "esc" = { operations = "unselect-all", description = "Unselect all currently selected lines." } + "space" = { description = "Toggle selection of line that cursor is currently on, and move cursor down 1 line", operations = [ "toggle-selection", "cursor down 1" ] } + "v" = { description = "Select line that cursor is currently on", operations = "select" } + "esc" = { description = "Unselect all currently selected lines", operations = "unselect-all" } # Help menu - "?" = { operations = "help-toggle", description = "Toggle the visibility of the help menu." } + "?" = { description = "Toggle the visibility of the help menu", operations = "help-toggle" } "#}; default_toml