kill, killall: Add some option and argument completions, simplify _signals.

This commit is contained in:
Ville Skyttä 2011-10-26 21:01:18 +03:00
parent 79ac9b5921
commit 099b47c66d
6 changed files with 44 additions and 22 deletions

View File

@ -777,20 +777,12 @@ _parse_usage()
done
}
# This function completes on signal names
#
# This function completes on signal names (minus the SIG prefix)
# @param $1 prefix
_signals()
{
local i
# standard signal completion is rather braindead, so we need
# to hack around to get what we want here, which is to
# complete on a dash, followed by the signal name minus
# the SIG prefix
COMPREPLY=( $( compgen -A signal SIG${cur#-} ))
for (( i=0; i < ${#COMPREPLY[@]}; i++ )); do
COMPREPLY[i]=-${COMPREPLY[i]#SIG}
done
local -a sigs=( $( compgen -P "$1" -A signal "SIG${cur#$1}" ) )
COMPREPLY+=( "${sigs[@]/#${1}SIG/${1}}" )
}
# This function completes on known mac addresses

View File

@ -5,9 +5,20 @@ _kill()
local cur prev words cword
_init_completion || return
case $prev in
-s)
_signals
return
;;
-l)
return
;;
esac
if [[ $cword -eq 1 && "$cur" == -* ]]; then
# return list of available signals
_signals
_signals -
COMPREPLY+=( $( compgen -W "-s -l" -- "$cur" ) )
else
# return list of available PIDs
_pids

View File

@ -2,11 +2,27 @@
_killall()
{
local cur prev words cword
_init_completion || return
local cur prev words cword split
_init_completion -s || return
case $prev in
-Z|--context|-o|--older-than|-y|--younger-than|-V|--version)
return
;;
-s|--signal)
[[ $1 == *killall ]] && _signals
return
;;
-u|--user)
_allowed_users
return
;;
esac
$split && return
if [[ $cword -eq 1 && "$cur" == -* ]]; then
_signals
_signals -
else
_pnames
fi

View File

@ -12,14 +12,16 @@ setup
assert_complete_any "kill 1"
sync_after_int
assert_complete [get_signals] "kill -"
assert_complete [get_signals] "kill -s "
sync_after_int
set expected [get_signals -]
lappend expected "-l" "-s"
assert_complete [lsort -unique $expected] "kill -"
sync_after_int

View File

@ -17,7 +17,7 @@ assert_complete_any "killall "
sync_after_int
assert_complete [get_signals] "killall -"
assert_complete [get_signals -] "killall -"
sync_after_int

View File

@ -659,8 +659,9 @@ proc get_hosts_avahi {} {
# Get signals
# This function is written in analogy to the bash function `_signals()' in
# `bash_completion'.
# @param prefix
# @return list Signals starting with `SIG', but with the `SIG' prefix removed.
proc get_signals {} {
proc get_signals {{prefix ""}} {
set signals {}
foreach signal [exec bash -c {compgen -A signal}] {
# Does signal start with `SIG'?
@ -668,7 +669,7 @@ proc get_signals {} {
# Remove `SIG' prefix
set signal [string range $signal 3 end]
# Add signal (with dash (-) prefix) to list
lappend signals -$signal
lappend signals $prefix$signal
}
}
return $signals