|
| 1 | +# Shell integration |
| 2 | + |
| 3 | +To get the most out of rpg-cli, it is suggested to define aliases or wrapper functions so the game can be integrated into a regular shell session, with enemies appearing along the way. |
| 4 | + |
| 5 | +This guide describes the basic building blocks to write such functions and shows some examples. |
| 6 | + |
| 7 | +## Basic `cd` alternative |
| 8 | + |
| 9 | +The default rpg-cli command works as `cd`, changing the hero's location from |
| 10 | +one directory to another. Since the program itself can't affect your shell session, |
| 11 | +you need to write a function so the working directory is changed to match that of the hero: |
| 12 | + |
| 13 | +```sh |
| 14 | +rpg () { |
| 15 | + rpg-cli "$@" |
| 16 | + cd "$(rpg-cli --pwd)" |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +This assumes `rpg-cli` is in your path, update with the specific location if not. You can define it directly in your current session, add it to `~/.bashrc`, source it from another script, etc. |
| 21 | + |
| 22 | +If you use fish shell, update `~/.config/fish/config.fish` instead: |
| 23 | + |
| 24 | +```fish |
| 25 | +function rpg |
| 26 | + rpg-cli $argv |
| 27 | + cd (rpg-cli --pwd) |
| 28 | +end |
| 29 | +``` |
| 30 | + |
| 31 | +## Full `cd` override |
| 32 | + |
| 33 | +If you like having enemies popping up while using `cd`, you can override that instead of using a separate function: |
| 34 | + |
| 35 | +```sh |
| 36 | +cd () { |
| 37 | + rpg-cli "$@" |
| 38 | + builtin cd "$(rpg-cli --pwd)" |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Custom integration commands |
| 43 | + |
| 44 | +To better adapt for different usage patterns, finer-grained commands are provided: |
| 45 | + |
| 46 | +* `rpg-cli --mv <path>` will set the hero's location to `<path>` without initiating battles. |
| 47 | +* `rpg-cli --pwd` will print the hero's current location. |
| 48 | +* `rpg-cli --battle` will initiate a battle with a probability that changes based on the distance from home. If the battle is lost the exit code of the program will be non-negative. |
| 49 | + |
| 50 | +## Prevent intermediate battles |
| 51 | + |
| 52 | +Note that the logic of the default rpg command is this: the hero moves one directory at a time, and enemies can appear at each step: |
| 53 | + |
| 54 | +* If the hero dies, the game is restarted and you go back home. |
| 55 | +* If the hero wins the battle, it will stop at the battle's location instead of keep moving to the initial destination. The rationale for this behavior is that you may need to adjust your strategy after each battle: use a potion, return home, try to escape battles, etc. |
| 56 | + |
| 57 | +Having `cd` not consistently set the pwd to the intended destination may not be acceptable if the program is used casually while doing other work. |
| 58 | +A better alternative for this usage pattern is enabled by the other integration commands, for example: |
| 59 | + |
| 60 | +```sh |
| 61 | +cd () { |
| 62 | + builtin cd "$@" |
| 63 | + rpg-cli --mv . |
| 64 | + rpg-cli --battle |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +## Aliasing other commands |
| 69 | + |
| 70 | +Another way to use rpg-cli is to initiate battles when attempting to execute file-modifying operations. Only when the battle is won the operation is allowed: |
| 71 | + |
| 72 | +```sh |
| 73 | +alias rpg-battle="rpg-cli --mv . && rpg-cli --battle" |
| 74 | + |
| 75 | +alias rm="rpg-battle && rm" |
| 76 | +alias rmdir="rpg-battle && rmdir" |
| 77 | +alias mkdir="rpg-battle && mkdir" |
| 78 | +alias touch="rpg-battle && touch" |
| 79 | +alias mv="rpg-battle && mv" |
| 80 | +alias cp="rpg-battle && cp" |
| 81 | +alias chown="rpg-battle && chown" |
| 82 | +alias chmod="rpg-battle && chmod" |
| 83 | +``` |
0 commit comments