pay-respects/README.md

143 lines
6 KiB
Markdown
Raw Normal View History

2023-07-31 21:38:04 +02:00
# Pay Respects
2023-07-30 18:40:18 +02:00
2023-08-04 03:04:26 +02:00
Typed a wrong command? Pay Respects will try to correct your wrong console command by simply pressing `F`!
2023-07-30 18:40:18 +02:00
2023-08-04 03:18:31 +02:00
- 🚀 **Blazing fast suggestion**: You won't notice any delay for asking suggestions!
2023-08-06 02:27:02 +02:00
- ✏️ **Easy to write rules**: You don't need to know Rust. The rules are written in a TOML file that is simple to work with and evaluated to Rust code upon compilation!
2023-08-13 17:51:43 +02:00
- 🎯 **Accurate results**: Suggestions must pass several conditions in order to be prompted to the user, no `sudo` suggestions when you are using `doas`!
2023-08-04 03:18:31 +02:00
- 🪶 **Tiny binary size**: Not even 1MB!
2023-08-13 17:51:43 +02:00
![pacman-fix](img/pacman-fix.png)
2023-08-05 00:56:46 +02:00
2023-08-13 17:51:43 +02:00
![cd-fix](img/cd-fix.png)
2023-07-31 19:25:19 +02:00
2023-07-31 21:38:04 +02:00
## How to Pay Respects
2023-07-30 18:40:18 +02:00
2023-07-31 21:38:04 +02:00
The binary is named `pay-respects`, by adding an alias to your shell
2023-07-30 18:40:18 +02:00
configuration:
``` shell
2023-07-30 22:10:55 +02:00
# Note: You may need to have the binary exposed in your path
2023-08-05 03:22:05 +02:00
alias f="$(pay-respects <your_shell_here>)"
2023-07-31 14:22:20 +02:00
# for example, using `zsh`:
2023-08-05 03:22:05 +02:00
alias f="$(pay-respects zsh)"
2023-07-31 21:12:57 +02:00
2023-08-04 15:40:45 +02:00
# Alternatively, you can also use the following initialization in your config file
# for bash and zsh
2023-08-05 03:22:05 +02:00
eval "$(pay-respects <shell> --alias)"
2023-08-04 15:40:45 +02:00
# for fish
2023-08-05 03:22:05 +02:00
pay-respects fish --alias | source
2023-08-04 15:40:45 +02:00
2023-07-31 21:12:57 +02:00
# for `nushell`, the alias can be added automatically with:
2023-08-05 03:22:05 +02:00
pay-respects nushell
2023-07-30 18:40:18 +02:00
```
2023-07-31 21:38:04 +02:00
You can now **press `F` to Pay Respects**!
2023-07-30 18:40:18 +02:00
2023-07-31 15:06:30 +02:00
Currently, only corrections to `bash`, `zsh`, and `fish` are working flawlessly.
2024-09-24 18:47:31 +02:00
`nushell` is currently usable, but there is no alias expansion, and you will have to put the evaluated initialization command in your config file (added automatically with `pay-respects nushell`). In addition, commands that need to be evaluated in the current working shell (such as `cd`) cannot yet be implemented in `nushell`.
2024-10-19 16:53:16 +02:00
Shell is not obtained automatically through environment variables because it won't work with nested shells, e.g. `fish` inside `zsh` still has `SHELL=zsh`.
2023-07-30 22:10:55 +02:00
## Installing
If you are using Arch Linux, you can install from AUR directly:
```shell
2024-09-18 16:20:15 +02:00
paru -S pay-respects # compile from source
paru -S pay-respects-bin # binary version
2023-07-30 22:10:55 +02:00
```
2024-09-24 18:47:31 +02:00
Or if you have cargo installed:
```shell
# install from crates.io
2024-09-18 16:20:15 +02:00
cargo install pay-respects
2024-09-24 18:47:31 +02:00
# clone from git and install, suitable for adding custom rules
git clone --depth 1 https://github.com/iffse/pay-respects
cd pay-respects
cargo install --path .
2023-07-30 22:10:55 +02:00
2024-09-25 01:22:45 +02:00
# compile without installing
# binary can be found at ./target/release/pay-respects
2024-09-24 18:47:31 +02:00
cargo build --release
```
2024-09-25 01:22:45 +02:00
Alternatively, you can download Linux binary from [releases](https://github.com/iffse/pay-respects/releases).
2024-09-24 18:47:31 +02:00
2023-07-30 20:08:28 +02:00
## Rule Files
2023-08-04 03:04:26 +02:00
Rule files are parsed at compilation. Everything in rule files is converted to Rust code before compiling. You don't have to know the project structure nor Rust to write the rules!
2023-07-30 20:08:28 +02:00
2024-09-25 01:22:45 +02:00
If only rules are changed, cargo won't recompile the project because Rust code were intact. You will have to notify it manually by:
```shell
touch src/rules.rs && cargo build
2024-09-25 01:22:45 +02:00
```
2023-08-04 03:04:26 +02:00
Syntax of a rule file (will be read by simply placing the file under [rules](./rules)):
2023-07-30 20:08:28 +02:00
```toml
# this field should be the name of the command
command = "world"
# you can add as many `[[match_err]]` section as you want
[[match_err]]
# the suggestion of this section will be used for the following patterns of the error output
# note that the error is formatted to lowercase
2023-07-30 20:08:28 +02:00
pattern = [
"pattern 1",
2023-08-04 02:42:16 +02:00
"pattern 2"
2023-07-30 20:08:28 +02:00
]
2023-07-30 20:22:49 +02:00
# this will change the first argument to `fix`, while keeping the rest intact
2023-08-04 02:42:16 +02:00
suggest = [
2023-08-08 22:20:49 +02:00
'''
{{command[0]}} fix {{command[2:]}} '''
2023-08-04 02:42:16 +02:00
]
2023-07-30 20:08:28 +02:00
[[match_err]]
2023-07-30 20:08:28 +02:00
pattern = [
2023-08-08 22:20:49 +02:00
"pattern 1"
2023-07-30 20:08:28 +02:00
]
2023-07-31 10:20:06 +02:00
# this will add a `sudo` before the command if:
2023-08-08 10:40:47 +02:00
# - the `sudo` is found by `command -v`
2023-07-31 10:26:31 +02:00
# - the last command does not contain `sudo`
2023-08-02 03:05:25 +02:00
suggest = [
'''
2023-07-31 10:20:06 +02:00
#[executable(sudo), !cmd_contains(sudo)]
2023-08-08 22:20:49 +02:00
sudo {{command}} '''
2023-08-02 03:05:25 +02:00
]
2023-07-30 20:08:28 +02:00
```
The placeholder is evaluated as following:
- `{{command}}`: All the command without any modification
- `{{command[1]}}`: The first argument of the command (the command itself has index of 0)
2024-09-24 18:47:31 +02:00
- `{{command[2:5]}}`: The second to fifth arguments. 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).
2023-08-04 00:55:43 +02:00
- `{{typo[2](fix1, fix2)}}`: This will try to change the second argument to candidates in the parenthesis. The argument in parentheses must have at least 2 values. 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.
2024-09-24 18:47:31 +02:00
- `{{opt::<Regular Expression>}}`: Optional patterns found in the command with RegEx (see RegEx crate for syntax). Note that all patterns matching this placeholder will not take place when indexing.
2023-08-07 18:40:06 +02:00
- `{{cmd::<Regular Expression>}}`: Get the matching pattern from the last command. Unlike `{{opt}}`, this won't remove the string after matching
2023-08-04 00:55:43 +02:00
- `{{err::<Regular Expression}}`: Get the matching patterns from the error message.
- `{{shell(<shell commands>)}}`: Replace with the output of the shell command. This placeholder can be used along `{{typo}}` as its only argument, where each newline will be evaluated to a candidate.
2023-07-30 20:08:28 +02:00
2024-09-24 18:47:31 +02:00
Suggestions can have additional conditions to check. To specify conditions, add a `#[...]` at the first line (just like derive macros in Rust). Available conditions:
2023-07-31 10:20:06 +02:00
2023-08-16 14:27:40 +02:00
- `executable`: Check whether the argument can be found by `which`
- `cmd_contains`: Check whether the last user input contains the argument
- `err_contains`: Check whether the error of the command contains the argument
- `length`: Check whether the given command has the length of the argument
- `min_length`: Check whether the given command has at least the length of the argument
- `max_length`: Check whether the given command has at most the length of the argument
2023-07-31 10:20:06 +02:00
2024-09-25 15:54:07 +02:00
For locale other than English, be aware that patterns should be the output having `LC_ALL=C` environment variable.
2023-07-30 18:40:18 +02:00
## Current Progress
2023-08-04 03:04:26 +02:00
Current option to write rules should cover most of the cases.
We need more rule files, contributions are welcomed!
2023-07-30 18:40:18 +02:00
2024-10-19 18:30:10 +02:00
This project is hosted at various sites, choose the one that suits you best:
- [Codeberg](https://codeberg.org/iff/pay-respects)
- [GitHub](https://github.com/iffse/pay-respects)
- [GitLab](https://gitlab.com/iffse/pay-respects)