From fa1da9e0116400c952b79b6d0444d3eca8cef045 Mon Sep 17 00:00:00 2001 From: BWindey <93756910+BWindey@users.noreply.github.com> Date: Mon, 7 Oct 2024 10:21:06 +0200 Subject: [PATCH] Improve bash tab completion (#26) * Update bash completion to remove older caches * Update completions to support XDG-spec, and suppress 'rm' errors when no files were found * Updated bash completions for smarter deletion, now only deletes files with the right names --- completions/bash_completion.sh | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/completions/bash_completion.sh b/completions/bash_completion.sh index 634d3e4..73187b0 100644 --- a/completions/bash_completion.sh +++ b/completions/bash_completion.sh @@ -5,12 +5,19 @@ # $3 = word before word being completed _commandtrein(){ - # Use a cache that will update every week - file="$HOME/.cache/commandtrein/$(date +'%m-%Y').txt" + # Use a cache that will update every week + cache_dir="${XDG_CACHE_DIR:-$HOME/.cache}/commandtrein" + file="${cache_dir}/$(date +'%m-%Y').txt" if ! [ -f "$file" ]; then - mkdir -p "$HOME/.cache/commandtrein/" - # Assumes that the binary is called commandtrein + mkdir -p "${cache_dir}" + # Remove older caches + find "${cache_dir}" \ + -maxdepth 1 \ + -type f \ + -name "[0-9][0-9]-2[0-9][0-9][0-9].txt" \ + -delete + # Assumes that the binary is called commandtrein commandtrein search > "$file" fi mapfile -t COMPREPLY < <(grep "$2" "$file")