pay-respects/modules.md

76 lines
4.2 KiB
Markdown
Raw Normal View History

2024-12-08 17:38:07 +01:00
# Modules
2024-12-30 00:20:49 +01:00
`pay-respects` followed a very stupid approach —or better said, *Keep It Simple, Stupid*— when it comes to implementing the module / plugin system:
2024-12-08 17:38:07 +01:00
- Modules interacts with core program by passing **messages through processes**. In other words, we are sending necessary information to the module, so it can return the required suggestion.
- This approach is the most extendable way, as it has the least amount of limitations compared to:
- Dynamic libraries (Safe): Requires the module to be compiled in the same compiler version as the core, which also limits the language available
- FFI (Unsafe): Requires overloading of dynamic libraries, limits to C types, and not extendable as it's overloading a library instead of appending
- Embedding a runtime: Unnecessary binary sizes if never used
- `pay-respects` takes the message passing approach as its core is blazing fast without observable delay so having a small overhead is acceptable. This allows:
- **Modules in any language**: Regardless of compiled binary or interpreted scripts, just make them executable and that's a module!
- **Extendable**: As many modules as you want
- **Performance or ease? Both!**: Write in a compiled language if it's something computational heavy, or just use a shell script as module right away
## Creating a Module
There are 2 types of modules:
- **Standard module**: Will always run to retrieve suggestions
2024-12-08 22:56:43 +01:00
- Naming convention: `_pay-respects-module-<priority>-<your module name>`
2024-12-08 17:38:07 +01:00
- **Fallback module**: Will only be run if no previous suggestion were found
2024-12-08 22:56:43 +01:00
- **CAUTION**: Will immediately return if a suggestion is obtained
- Naming convention: `_pay-respects-fallback-<priority>-<your module name>`
Priority is used to retrieve suggestions in a specific order by an [unstable sort](https://doc.rust-lang.org/std/primitive.slice.html#method.sort_unstable) (although they will always be after compile-time matches). Default modules have a priority of `100`.
2024-12-08 17:38:07 +01:00
When running your module, you will get the following environment variables:
- `_PR_SHELL`: User shell
- `_PR_COMMAND`: The command, without arguments
- `_PR_LAST_COMMAND`: Full command with arguments
- `_PR_ERROR_MSG`: Error message from the command
2024-12-09 01:30:17 +01:00
- `_PR_EXECUTABLES`: A space (` `) separated list of executables in `PATH`
2024-12-08 17:38:07 +01:00
2024-12-09 16:22:20 +01:00
Your module should print:
2024-12-08 17:38:07 +01:00
- **To `stdout`**: Only suggestions.
- At the end of each suggestion, append `<_PR_BR>` so pay-respects knows you are either done or adding another suggestion
- **To `stderr`**: Any relevant information that should display to the user (e.g, warning for AI generated content)
## Adding a Module
Expose your module as executable (`chmod u+x`) in `PATH`, and done!
2024-12-10 02:33:02 +01:00
## `LIB` directories
2024-12-12 16:52:11 +01:00
If exposing modules in `PATH` annoys you, you can set the `_PR_LIB` environment variable to specify directories to find the modules, separated by `:` (analogous to `PATH`):
2024-12-10 02:33:02 +01:00
2024-12-10 15:00:55 +01:00
Example in a [FHS 3.0 compliant system](https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch04s06.html):
2024-12-10 02:33:02 +01:00
```shell
2024-12-12 16:52:11 +01:00
# compile-time
2024-12-30 00:20:49 +01:00
export _DEF_PR_LIB="/usr/lib"
2024-12-12 16:52:11 +01:00
# runtime
2024-12-11 17:48:07 +01:00
export _PR_LIB="/usr/lib:$HOME/.local/share"
2024-12-10 02:33:02 +01:00
```
2024-12-30 00:20:49 +01:00
This is not the default as there is no general way to know its value and depends on distribution (`/usr/lib`, `/usr/libexec`, or NixOS which isn't FHS compliant at all). System programs usually have a hard-coded path looking for `lib`. If you are a package maintainer for a distribution, setting this value when compiling, so it fits into your distribution standard.
2024-12-10 02:33:02 +01:00
2024-12-10 15:00:55 +01:00
If you installed the module with `cargo install`, the binary will be placed in `bin` subdirectory under Cargo's home which should be in the `PATH` anyway. Cargo has no option to place in subdirectories with other names.
2024-12-10 20:59:50 +01:00
2024-12-11 17:48:07 +01:00
The following snippet is what I have included into Arch Linux's package with workflows binaries, adding a `_PR_LIB` declaration along with initialization. The script is `/usr/bin/pay-respects` and the actual executable is located somewhere else.
2024-12-10 20:59:50 +01:00
```sh
2024-12-11 17:48:07 +01:00
#!/bin/sh
if [ "$#" -gt 1 ] && [ -z "$_PR_LIB" ]; then
SHELL=$(basename $SHELL)
LIB="/usr/lib/pay-respects"
if [ "$SHELL" = "nu" ]; then
echo "env:_PR_LIB=$LIB"
elif [[ "$SHELL" = "pwsh" ]]; then
echo "\$env:_PR_LIB=\"$LIB\""
else
echo "export _PR_LIB=$LIB"
2024-12-10 20:59:50 +01:00
fi
fi
/opt/pay-respects/bin/pay-respects "$@"
2024-12-11 17:48:07 +01:00
```