]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
bash-completion: look: rewrite completion logic
authorYao Zi <ziyao@disroot.org>
Mon, 9 Sep 2024 18:08:30 +0000 (18:08 +0000)
committerYao Zi <ziyao@disroot.org>
Wed, 11 Sep 2024 17:04:53 +0000 (17:04 +0000)
look(1) opens /usr/share/dict/words as dictionary by default. Doing TAB
completion for look(1) without the dictionary installed will result in
popups like

$ look look: /usr/share/dict/words: No such file or directory

messing up the terminal.

As /usr/share/dict/words is not shipped by quite a few distros nowadays
and it should not be listed as a hard dependency of util-linux, we make
sure the dictionary exists before calling look(1) when doing completion,
avoiding these error messages.

When completing the word without anything specified on the command line,
alphabets and numbers instead of all words are listed as a hint, for
better interacting experience.

Signed-off-by: Yao Zi <ziyao@disroot.org>
bash-completion/look

index feafe23795c5d79daab4978328c64079b8e24be8..8bae52c6193cbb4abbd2ff3df8c4a39f60692a46 100644 (file)
@@ -22,7 +22,17 @@ _look_module()
        esac
        case $COMP_CWORD in
                1)
-                       COMPREPLY=( $(compgen -W "$(look "$cur")" -- $cur) )
+                       [ -f /usr/share/dict/words ] || return 0
+                       if [ "$cur" ]; then
+                               COMPREPLY=( $(compgen -W "$(look "$cur")" -- $cur) )
+                       else
+                               COMPREPLY=( $(compgen -W \
+                                             "0 1 2 3 4 5 6 7 8 9 \
+                                              a b c d e f g h i j k l m n o p \
+                                              q r s t u v w x y z \
+                                              A B C D E F G H I J K L M N O P \
+                                              Q R S T U V W X Y Z") )
+                       fi
                        ;;
                2)
                        local IFS=$'\n'