pay-respects/rules.md

114 lines
4.5 KiB
Markdown
Raw Permalink Normal View History

2024-12-07 16:53:29 +01:00
# Writing Rules
2024-11-15 20:11:26 +01:00
2025-04-22 15:38:16 +02:00
Rule files placed under [rules](./rules) in the project directory are parsed at
compilation, everything is parsed to Rust code before compiling. You don't have
to know the project structure nor Rust to write blazing fast rules!
2024-11-15 20:11:26 +01:00
2025-04-22 15:38:16 +02:00
For compile-time rules, if only rules are changed, cargo won't recompile the
project because Rust code were intact. You will have to notify it manually by:
2024-11-15 20:11:26 +01:00
```shell
2024-12-08 17:46:43 +01:00
touch core/src/rules.rs && cargo build
2024-11-15 20:11:26 +01:00
```
2025-04-22 15:38:16 +02:00
Runtime-rules support is provided by `runtime-rules` module. Directories are
searched with the following priority:
2024-11-16 01:32:42 +01:00
2024-11-21 17:37:33 +01:00
- `XDG_CONFIG_HOME`, defaults to `$HOME/.config`.
- `XDG_CONFIG_DIRS`, defaults to `/etc/xdg`.
- `XDG_DATA_DIRS`, defaults to `/usr/local/share:/usr/share`.
2024-11-16 01:32:42 +01:00
2025-04-22 15:38:16 +02:00
The actual rule file should be placed under `pay-respects/rules/`, for example:
`~/.config/pay-respects/rules/cargo.toml`. Note that for runtime rules, the
name of the file **MUST** match the command name. Except `_PR_GENERAL.toml`
that is always parsed
2024-11-15 20:11:26 +01:00
2024-12-08 19:38:40 +01:00
## Syntax
2024-11-15 20:11:26 +01:00
Syntax of a rule file:
```toml
2024-12-08 19:38:40 +01:00
# the name of the command
command = "helloworld"
2024-11-15 20:11:26 +01:00
# you can add as many `[[match_err]]` section as you want
[[match_err]]
2024-12-08 19:38:40 +01:00
# Note:
2025-04-09 17:36:35 +02:00
# - patterns must be in lowercase without extra space characters
2024-12-08 19:38:40 +01:00
# - patterns should be the output with `LC_ALL=C` environment variable
2025-04-09 17:36:35 +02:00
# - this is a first-pass match. It should be quick so regex is not supported
2024-11-15 20:11:26 +01:00
pattern = [
"pattern 1",
"pattern 2"
]
2024-12-08 19:38:40 +01:00
# if pattern is matched, suggest changing the first argument to fix:
2024-11-15 20:11:26 +01:00
suggest = [
'''
{{command[0]}} fix {{command[2:]}} '''
]
[[match_err]]
pattern = [
"pattern 1"
]
# this will add a `sudo` before the command if:
2024-11-20 10:24:38 +01:00
# - the `sudo` is found as executable
2024-11-15 20:11:26 +01:00
# - the last command does not contain `sudo`
suggest = [
'''
#[executable(sudo), !cmd_contains(sudo)]
sudo {{command}} '''
]
```
The placeholder is evaluated as following:
2025-01-07 18:44:37 +01:00
- `{{command}}`: All the command without any modification
2025-04-22 15:38:16 +02:00
- `{{command[1]}}`: The first argument of the command (the command itself has
index of 0)
2025-01-07 18:44:37 +01:00
- Negative values will count from reverse.
- `{{command[2:5]}}`: The second to fifth arguments
2025-04-22 15:38:16 +02:00
- If any of the side is not specified, then it defaults to the start (if it
is left) or the end (if it is right)
- `{{typo[2](fix1, fix2)}}`: Try to change the second argument to candidates in
the parenthesis.
2025-01-07 18:44:37 +01:00
- The argument in parentheses must have at least 2 values
2025-04-22 15:38:16 +02:00
- Single arguments are reserved for specific matches, for instance, `path` to
search all commands found in the `$PATH` environment, or the `{{shell}}`
placeholder, among others
- `{{select[3](selection1, selection2)}}`: A derivative of `typo` placeholder.
Will create a suggestion for each selection in the parenthesis
2025-01-07 18:44:37 +01:00
- The argument in parentheses also must have at least 2 values
2025-04-22 15:38:16 +02:00
- Single arguments are reserved for specific selections, for instance, `path`
to search all commands found in the `$PATH` environment with the minimum
shared linguistic distance, or the `{{shell}}` placeholder
- To avoid permutations and combinations, only one instance is evaluated. If
you need to apply the same selection in multiple places, use
`{{selection}}`
- Index is optional as it only has effect when using with `path`, and
defaults to `0`
- `{{opt::<Regular Expression>}}`: Optional patterns captured in the command
with RegEx ([see regex crate for syntax])
2025-01-07 18:44:37 +01:00
- All patterns matching this placeholder will be removed from indexing
- `{{cmd::<Regular Expression>}}`: Get the matching captures from the last command
- Unlike `{{opt}}`, this won't remove the string after matching
- `{{err::<Regular Expression}}`: Get the matching captures from the error message
- `{{shell(<shell commands>)}}`: Replace with the output of the shell command
2025-04-22 15:38:16 +02:00
- Can be used along `{{typo}}` or `{{select}}` as its only argument, where
each newline will be evaluated to a candidate/selection
2024-11-15 20:11:26 +01:00
2025-04-22 15:38:16 +02:00
[see regex crate for syntax]: https://docs.rs/regex-lite/latest/regex_lite/#syntax
Suggestions can have additional conditions to check. To specify conditions, add
a `#[...]` at the first line (just like derive macros in Rust). Available
conditions:
2024-11-15 20:11:26 +01:00
2025-04-06 16:58:52 +02:00
- `executable`: Check if the argument can be found in path
2025-04-22 15:38:16 +02:00
- `cmd_contains`: Check if the last user input contains the argument. Regex
supported (you can't use `,` currently because it's used as condition
separator)
2025-04-09 17:36:35 +02:00
- `err_contains`: Same as `cmd_contains` but for error message
2025-04-06 16:58:52 +02:00
- `length`: Check if the given command has the length of the argument
- `min_length`: Check if the given command has at least the length of the argument
- `max_length`: Check if the given command has at most the length of the argument
2025-01-07 18:44:37 +01:00
- `shell`: Check if the current running shell is the argument
2024-11-15 20:11:26 +01:00