]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
bash-completion: Add non-canonical device fallback
authorKevin Locke <kevin@kevinlocke.name>
Thu, 19 Sep 2019 13:45:54 +0000 (07:45 -0600)
committerKevin Locke <kevin@kevinlocke.name>
Thu, 19 Sep 2019 14:17:09 +0000 (08:17 -0600)
It is desirable for bash-completion to complete block devices via
symlinks (e.g. under /dev/disk) and with non-canonical locations (e.g.
./sda if $PWD is /dev, and /chroot/dev/sda).

Unfortunately, this is a non-trivial task due to how bash-completion
works.  It is necessary to un-escape the last partial argument, search,
then escape the results, ideally handling tilde and variable
expansion/completion.  See [_get_comp_words_by_ref] and [_filedir] in
the bash-completion project for details.

Given the development costs of a complete and correct implementation,
the annoyance/frustration which would result from an incomplete/buggy
implementation, and the trade-offs between under- and over-completion,
this commit adds fallback to bash default completion if the argument
does not match any canonical device names.  This correctly completes in
the cases mentioned above, although it incorrectly completes on
non-block-device files as well.

[_filedir]: https://github.com/scop/bash-completion/blob/2.9/bash_completion#L552
[_get_comp_words_by_ref]: https://github.com/scop/bash-completion/blob/2.9/bash_completion#L365

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
bash-completion/addpart
bash-completion/blkdiscard
bash-completion/blkid
bash-completion/blkzone
bash-completion/blockdev
bash-completion/delpart
bash-completion/eject
bash-completion/lsblk
bash-completion/resizepart

index 31796390886e462880f8c9e4ae53d060c376fd4e..b7e93d10297f1e9fbe688b53d576cbdbd71edbb5 100644 (file)
@@ -6,6 +6,7 @@ _addpart_module()
        case $COMP_CWORD in
                1)
                        OPTS="--help --version $(lsblk -pnro name)"
+                       compopt -o bashdefault -o default
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
                        ;;
                2)
index 64a5c34d5f2b16e58e2cb804737bc9967f593aad..f28e5d4c262b574c25b9457b86f49beac00c266e 100644 (file)
@@ -29,6 +29,7 @@ _blkdiscard_module()
                        return 0
                        ;;
        esac
+       compopt -o bashdefault -o default
        COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) )
        return 0
 }
index 129a677cb0d2363de9dd8f991d0bc694a3dc5acb..74de34284ca2163df14abde8083e919fc4fd7dda 100644 (file)
@@ -93,6 +93,7 @@ _blkid_module()
                        return 0
                        ;;
        esac
+       compopt -o bashdefault -o default
        COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) )
        return 0
 }
index a50684df974fab236e6cc9bd2573d023535f3435..b642953485e9c74a76f5527361c43278346bc0fe 100755 (executable)
@@ -46,6 +46,7 @@ _blkzone_module()
                        return 0
                        ;;
        esac
+       compopt -o bashdefault -o default
        COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) )
        return 0
 }
index e7aca26de723c1a5a2bebfdbc3144969d3e6b6c2..e673acf5431d817c857b4e1ba147ca8f42b94f44 100644 (file)
@@ -38,6 +38,7 @@ _blockdev_module()
                        return 0
                        ;;
        esac
+       compopt -o bashdefault -o default
        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
        return 0
 }
index d5a92ef288eb6d55270b8ccee54e4a8dee8d566e..3855812089a2438020d7db071e36194c41b2bcd2 100644 (file)
@@ -12,6 +12,7 @@ _delpart_module()
        case $COMP_CWORD in
                1)
                        OPTS="--help --version $(lsblk -pnro name)"
+                       compopt -o bashdefault -o default
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
                        ;;
                2)
index 327ef1b713aa443338b11f0cac0517b063ec5621..27d04656c56a8a593016c520d7e88c81d5e540a0 100644 (file)
@@ -58,6 +58,7 @@ _eject_module()
                        IFS=$OLD_IFS
                fi
        done)"
+       compopt -o bashdefault -o default
        COMPREPLY=( $(compgen -W "$DEVS" $cur) )
        return 0
 }
index a1e9e61b62ff18635ff4f2bee35ade7a2534bb7d..e54fc4d5e58078f2d30e1c75533bd47bdf1913fc 100644 (file)
@@ -85,6 +85,7 @@ _lsblk_module()
                        return 0
                        ;;
        esac
+       compopt -o bashdefault -o default
        COMPREPLY=( $(compgen -W "$($1 -pnro name)" -- $cur) )
        return 0
 }
index 7e385036613b0b8713f4316ba081d6e51612a781..63ca5336b6bdebe399f81e9fe888e31d39682792 100644 (file)
@@ -12,6 +12,7 @@ _resizepart_module()
        case $COMP_CWORD in
                1)
                        OPTS="--help --version $(lsblk -pnro name)"
+                       compopt -o bashdefault -o default
                        COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
                        ;;
                2)