Skip to content

Commit

Permalink
Bash tab completion (#25)
Browse files Browse the repository at this point in the history
* Create bash_completion.sh

* Update README.md with completion info

* Update README.md, fixed typos
  • Loading branch information
BWindey authored Sep 29, 2024
1 parent 0bfed11 commit 584f91c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@ commandtrein search
To filter results, you will have to use tools like grep. Filtering is planned in upcoming releases


#### Tab completion
Tab completion is currently only implemented for bash.

<details>
<summary>Bash</summary>

To get tab completion for bash, download the file located here in `completions/bash_completion.sh`, and source it in your .bashrc:
```sh
source /PATH/TO/bash_completion.sh
```
> ⚠️
> The script currently assumes that your executable is called `commandtrein`.
> You can either rename your binary, or change the bash-file at line 14 and 19.
The completion uses the `mkdir`, `mapfile`, `grep` and `complete` commands, which should all be installed by default on your system.
The completions are sourced from the `commandtrein search` command, and are cached in "$HOME/.config/commandtrein/" to prevent having to query for the data (~160ms) every time. Caches are updated once a month, but you can update it forcefully by removing all the cache-files: `rm "$HOME/.config/commandtrein/*"`
</details>

#### Acknowledgements

Commandtrein leverages the iRails API, an open-source API for accessing real-time data from SNCB.
19 changes: 19 additions & 0 deletions completions/bash_completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Bash completion script for commandtrein, needs to be sourced to work

# $1 = name of command -> commandtrein
# $2 = current word being completed
# $3 = word before word being completed

_commandtrein(){
# Use a cache that will update every week
file="$HOME/.config/commandtrein/$(date +'%m-%Y').txt"

if ! [ -f "$file" ]; then
mkdir -p "$HOME/.config/commandtrein/"
# Assumes that the binary is called commandtrein
commandtrein search > "$file"
fi
mapfile -t COMPREPLY < <(grep "$2" "$file")
}

complete -F _commandtrein commandtrein

0 comments on commit 584f91c

Please sign in to comment.