From 7e6e41204da6833247d844806f9e654b6e7b18ab Mon Sep 17 00:00:00 2001 From: Pete Dietl Date: Fri, 19 Jul 2024 19:09:43 -0700 Subject: [PATCH] Fix Bash completions The previous completion script was including extra completions that were not actually commands. This commit also moves the completion file and renames it such that on Ubuntu one can simple download the release zip, unpack it, cd into the unpacked directory, and run `cp -r bin share /usr/local` to install with bash-completions in the proper place to get automatically sourced --- .github/workflows/ci-linux.yml | 4 +- .../bash-completion/completions/alr | 39 ++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) rename scripts/alr-completion.bash => share/bash-completion/completions/alr (72%) mode change 100755 => 100644 diff --git a/.github/workflows/ci-linux.yml b/.github/workflows/ci-linux.yml index 7992d2199..41f43c8dc 100644 --- a/.github/workflows/ci-linux.yml +++ b/.github/workflows/ci-linux.yml @@ -66,7 +66,9 @@ jobs: - name: Package binaries if: (github.event_name == 'release') - run: zip alr-bin-linux.zip bin/alr LICENSE.txt + run: | + echo 'sudo cp -r bin share /usr/local/' > INSTALL.md + zip alr-bin-linux.zip bin/alr share/bash-completion/completions/alr LICENSE.txt INSTALL.md - name: Retrieve upload URL for the release if: (github.event_name == 'release') diff --git a/scripts/alr-completion.bash b/share/bash-completion/completions/alr old mode 100755 new mode 100644 similarity index 72% rename from scripts/alr-completion.bash rename to share/bash-completion/completions/alr index 9337e6b71..9116e2ddd --- a/scripts/alr-completion.bash +++ b/share/bash-completion/completions/alr @@ -13,8 +13,43 @@ else fi alr settings --global --set index.auto_update 0 -# Commands/Topics: all line-first words not starting with capital letter, after # COMMANDS -_alr_commands=$(alr | grep COMMANDS -A 99 | awk '{print $1}' | grep -v '[[:upper:]]' | xargs) +# The help output is roughly structured into sections like so: +# SECTION_NAME_1 +# Subsection +# word description +# +# Note that the section names are in ALL_CAPS and the subsection names start +# with a capital letter. The good stuff is the lowercase words in the COMMANDS and ALIASES sections + +# First, we collect all commands (words not starting with an uppercase letter) in the COMMANDS section. +# We start matching on the line "COMMANDS" and collect all first words in lines that don't start with a +# capital letter. We stop when we find a line comprised of entirely UPPERCASE letters, which delimits +# the start of another section. +# +# Because Awk range matches are inclusive, and the end pattern would also match the start pattern, +# we have to add a bit of ugliness. + +_alr_commands=$(alr | awk ' + $0 == "COMMANDS", ($0 ~ /^[A-Z]+$/ && $0 != "COMMANDS") { + if ($0 == "COMMANDS" || $1 ~ /^[A-Z]/ || !NF) + skip + else + print $1 + } +') + +_alr_commands+=' ' + +# Now do the same, but for the ALIASES section. + +_alr_commands+=$(alr | awk ' + $0 == "ALIASES", ($0 ~ /^[A-Z]+$/ && $0 != "ALIASES") { + if ($0 == "ALIASES" || $1 ~ /^[A-Z]/ || !NF) + skip + else + print $1 + } +') # Long global switches _alr_global_switches=$(alr -h | grep -Eo -- '--[[:alnum:]-]+' | xargs)