Skip to content

Commit

Permalink
shell-completion/*/insmod: add bash/fish/zsh completion
Browse files Browse the repository at this point in the history
A few inline todos and some odd fish behaviour as mentioned inline.
Otherwise things just work :-)

v2:
 - use e(x)clusive answers for fish, tweak force string

Signed-off-by: Emil Velikov <[email protected]>
  • Loading branch information
evelikov committed Sep 18, 2024
1 parent 25f8198 commit d98f3a9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ foreach tuple : _completiondirs
endif

_completions = [
'insmod',
'lsmod',
'rmmod',
]
Expand Down
35 changes: 35 additions & 0 deletions shell-completion/bash/insmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# insmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright 2024 Emil Velikov
#
# Formatted using:
# shfmt --language-dialect bash --indent 4 --func-next-line

_insmod()
{
# long/short opt pairs
local -A opts=(
['force']='f'
['syslog']='s'
['verbose']='v'
['version']='V'
['help']='h'
)

local cur="${COMP_WORDS[COMP_CWORD]}"

if [[ $cur == --* ]]; then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
elif [[ $cur == -* ]]; then
if (( ${#cur} != 2 )); then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
fi
COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
else
# TODO: match only one file
_filedir '@(ko?(.gz|.xz|.zst))'
# TODO: add module parameter completion, based of modinfo
fi
} &&
complete -F _insmod insmod
21 changes: 21 additions & 0 deletions shell-completion/fish/insmod.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# insmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright 2024 Emil Velikov

# globally disable file completions
complete -c insmod -f

complete -c insmod -s f -l force -d "DANGEROUS: forces a module load, may cause data corruption and crash your machine"
complete -c insmod -s s -l syslog -d 'print to syslog, not stderr'
complete -c insmod -s v -l verbose -d 'enables more messages'
complete -c insmod -s V -l version -d 'show version'
complete -c insmod -s h -l help -d 'show this help'

# provide an exclusive (x) list of required (r) answers (a), keeping (k) the
# matches at the top.
# TODO: match only one file
# BUG: fish lists everything, even when only the given suffix is requested. Plus
# we need to explicitly keep them sorted.
complete -c insmod -x -ra "(__fish_complete_suffix .ko .ko.gz .ko.xz .ko.zst)" -k
# TODO: add module parameter completion, based of modinfo
14 changes: 14 additions & 0 deletions shell-completion/zsh/_insmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#compdef insmod

# insmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright 2024 Emil Velikov

_arguments \
{-f,--force}'[DANGEROUS: forces a module load, may cause data corruption and crash your machine]' \
{-s,--syslog}'[print to syslog, not stderr]' \
{-v,--verbose}'[enables more messages]' \
{-V,--version}'[show version]' \
{-h,--help}'[show this help]' \
'1::module:_files -g "*.ko(|.gz|.xz|.zst)"'

0 comments on commit d98f3a9

Please sign in to comment.