Skip to content

Commit

Permalink
Some clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzrehde committed Feb 4, 2024
1 parent a70c730 commit 8ba7c9d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
35 changes: 17 additions & 18 deletions examples/file-manager.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,51 @@
# 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.
# But we enable need periodic reloads in case some other processes manipulated the filesystem.
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",
Expand Down
8 changes: 3 additions & 5 deletions src/config/keybindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,11 @@ impl From<TomlOperations> for Vec<String> {
#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Default)]
struct Description(Option<String>);

// 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(())
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8ba7c9d

Please sign in to comment.