From 584f91c5f17da07acc4b96fc9682625ee733da13 Mon Sep 17 00:00:00 2001 From: BWindey <93756910+BWindey@users.noreply.github.com> Date: Sun, 29 Sep 2024 18:51:28 +0200 Subject: [PATCH] Bash tab completion (#25) * Create bash_completion.sh * Update README.md with completion info * Update README.md, fixed typos --- README.md | 18 ++++++++++++++++++ completions/bash_completion.sh | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 completions/bash_completion.sh 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