diff --git a/README.md b/README.md index 3844e15..c337d87 100644 --- a/README.md +++ b/README.md @@ -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. + +
+Bash + +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/*"` +
+ #### Acknowledgements Commandtrein leverages the iRails API, an open-source API for accessing real-time data from SNCB. diff --git a/completions/bash_completion.sh b/completions/bash_completion.sh new file mode 100644 index 0000000..99d06a2 --- /dev/null +++ b/completions/bash_completion.sh @@ -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