Skip to content

Commit fd79c84

Browse files
committed
add shell integration guide
1 parent b8a91ee commit fd79c84

2 files changed

Lines changed: 90 additions & 13 deletions

File tree

README.md

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ The binary should be available as `rpg-cli` (assuming you have `~/.cargo/bin` in
3030
If you use nix/nixos you can get rpg-cli from nixpkgs, either install it by adding it to your system config, installing it with `nix-env -i rpg-cli` or try it in a ephemeral shell with `nix-shell -p rpg-cli`.
3131
Note that at the current time of writing, the package hasn't hit any of the channels yet. When you try it, check that it's in your channel.
3232

33-
### Use as a cd replacement (recommended)
33+
### Shell integration (recommended)
3434

35-
Once the binary is installed with one of the methods described above, it can be wrapped on a shell function or alias
36-
so the working directory is updated to track to the hero's progress. You can set that up by adding something like this to your `.bashrc`:
35+
The game is designed to integrate with common file system operations, such as changing directories or deleting files.
36+
37+
The most basic type of integration consists in wrapping rpg-cli in a shell function, such that the working directory is updated to track to the hero's progress, effectively working as a `cd` alternative:
3738

3839
```sh
3940
rpg () {
@@ -42,8 +43,6 @@ rpg () {
4243
}
4344
```
4445

45-
This assumes `rpg-cli` is in your path, update with the specific location if not. You can `source ~/.bashrc` to apply the change without opening a new shell.
46-
4746
Or, if you want to go all the way and *really* use it in place of `cd`:
4847

4948
```sh
@@ -53,21 +52,16 @@ cd () {
5352
}
5453
```
5554

56-
If you use fish shell, update `~/.config/fish/config.fish` instead:
57-
58-
```fish
59-
function rpg
60-
rpg-cli $argv
61-
cd (rpg-cli --pwd)
62-
end
63-
```
55+
Other commands like `rm`, `mkdir`, `touch`, etc. can also be aliased. Check the [shell integration guide](shell/README.md) for more sophisticated examples, as well as their fish shell equivalents.
6456

6557
### Troubleshooting
6658

6759
* The release binary for macOS [is not signed](https://github.com/facundoolano/rpg-cli/issues/27). To open it for the first time, right click on the binary and select "Open" from the menu.
6860

6961
## Usage
7062

63+
This example session assumes a basic `rpg` function as described in the previous section.
64+
7165
The first time you run the program, a new hero is created at the user's home directory.
7266

7367
~ $ rpg

shell/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)