bash-completion: Add non-canonical device fallback

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>
This commit is contained in:
Kevin Locke 2019-09-19 07:45:54 -06:00
parent 6ead91ce1e
commit 0c8865f843
9 changed files with 9 additions and 0 deletions

View 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)

View File

@ -29,6 +29,7 @@ _blkdiscard_module()
return 0
;;
esac
compopt -o bashdefault -o default
COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) )
return 0
}

View File

@ -93,6 +93,7 @@ _blkid_module()
return 0
;;
esac
compopt -o bashdefault -o default
COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) )
return 0
}

View File

@ -46,6 +46,7 @@ _blkzone_module()
return 0
;;
esac
compopt -o bashdefault -o default
COMPREPLY=( $(compgen -W "$(lsblk -pnro name)" -- $cur) )
return 0
}

View File

@ -38,6 +38,7 @@ _blockdev_module()
return 0
;;
esac
compopt -o bashdefault -o default
COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) )
return 0
}

View 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)

View File

@ -58,6 +58,7 @@ _eject_module()
IFS=$OLD_IFS
fi
done)"
compopt -o bashdefault -o default
COMPREPLY=( $(compgen -W "$DEVS" $cur) )
return 0
}

View File

@ -85,6 +85,7 @@ _lsblk_module()
return 0
;;
esac
compopt -o bashdefault -o default
COMPREPLY=( $(compgen -W "$($1 -pnro name)" -- $cur) )
return 0
}

View 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)