Merge branch 'master' of git+ssh://git.debian.org/git/bash-completion/bash-completion

This commit is contained in:
David Paleino 2009-06-20 09:14:35 +02:00
commit 1ab50e301e
8 changed files with 98 additions and 42 deletions

View File

@ -88,6 +88,8 @@ bash-completion (1.x)
* Improve rpm group completion (displayed completions are still wrong). * Improve rpm group completion (displayed completions are still wrong).
* Change many completions to load in memory only if the completed commands * Change many completions to load in memory only if the completed commands
are available. are available.
* Invoke the actual mplayer/mencoder command being completed (with full path)
to get various completions instead of simply "mplayer" or "mencoder".
[ Todd Zullinger ] [ Todd Zullinger ]
* Make yum complete on filenames after install, deplist, update and upgrade * Make yum complete on filenames after install, deplist, update and upgrade

View File

@ -1084,6 +1084,8 @@ _known_hosts_real()
p) prefix=$OPTARG ;; p) prefix=$OPTARG ;;
esac esac
done done
[ $# -ge $OPTIND ] && echo "error: $FUNCNAME("$@"): unprocessed arguments:"\
$(while [ $# -gt 0 ]; do echo ${!OPTIND}; shift; done)
[[ $cur == *@* ]] && user=${cur%@*}@ && cur=${cur#*@} [[ $cur == *@* ]] && user=${cur%@*}@ && cur=${cur#*@}
kh=() kh=()

View File

@ -22,12 +22,8 @@ _mplayer()
prev=${COMP_WORDS[COMP_CWORD-1]} prev=${COMP_WORDS[COMP_CWORD-1]}
case "$prev" in case "$prev" in
-@(ac|afm|vc|vfm|ao|vo|vop|fstype|demuxer|vf|af)) -@([av][cfo]|[av]fm|vop|fstype|demuxer|o[av]c|of|profile))
_mplayer_options_list mplayer $prev _mplayer_options_list $cmd $prev
return 0
;;
-@(oac|ovc|of))
_mplayer_options_list mencoder $prev
return 0 return 0
;; ;;
-audiofile) -audiofile)
@ -250,10 +246,6 @@ _mplayer()
COMPREPLY=( $( compgen -W 'force= list=' -- $cur ) ) COMPREPLY=( $( compgen -W 'force= list=' -- $cur ) )
return 0 return 0
;; ;;
-profile)
_mplayer_options_list $cmd $prev
return 0
;;
esac esac
case "$cur" in case "$cur" in

View File

@ -94,7 +94,7 @@ _ssh()
fi fi
shift shift
done done
_known_hosts_real -a "$optconfigfile" -h "$cur" _known_hosts_real -a -h "$cur" $optconfigfile
if [ $COMP_CWORD -ne 1 ]; then if [ $COMP_CWORD -ne 1 ]; then
COMPREPLY=( "${COMPREPLY[@]}" $( compgen -c -- $cur ) ) COMPREPLY=( "${COMPREPLY[@]}" $( compgen -c -- $cur ) )
fi fi
@ -144,7 +144,7 @@ _sftp()
fi fi
shift shift
done done
_known_hosts_real -a "$optconfigfile" -h "$cur" _known_hosts_real -a -h "$cur" $optconfigfile
fi fi
return 0 return 0
@ -200,7 +200,7 @@ _scp()
shift shift
done done
[[ "$cur" == */* ]] || _known_hosts_real -c -a "$optconfigfile" -h "$cur" [[ "$cur" == */* ]] || _known_hosts_real -c -a -h "$cur" $optconfigfile
# This approach is used instead of _filedir to get a space appended # This approach is used instead of _filedir to get a space appended
# after local file/dir completions, and $nospace retained for others. # after local file/dir completions, and $nospace retained for others.

View File

@ -24,18 +24,7 @@ sync_after_int
set test "Tab should complete partial username" set test "Tab should complete partial username"
# Build string list of usernames, starting with the character of the assert_complete_partial [exec bash -c "compgen -A user"] "finger"
# first username.
set users {}
set char ""
foreach u [exec bash -c "compgen -A user"] {
if {$char == ""} {set char [string range $u 0 0]}
# Only append username if starting with $char
if {[string range $u 0 0] == "$char"} {
lappend users $u
}; # if
}; # foreach
assert_complete $users "finger $char" $test
sync_after_int sync_after_int

View File

@ -42,4 +42,31 @@ expect {
sync_after_int sync_after_int
set test "First argument shouldn't complete with commands"
# NOTE: This test assumes the machine running this test has a command "bash"
# but no host named "bash" ...
set cmd "ssh bas"
send "$cmd\t"
expect -ex "$cmd"
expect {
-timeout 1
# In case multiple commands `bas*' are completed
-re "^\r\n.*bash.*\r\n/@$cmd$" { fail "$test" }
# In case the single command `bash' is completed
-re "h $" { fail "$test" }
-re ".+" { unresolved "$test" }
timeout { pass "$test" }
}; # expect
sync_after_int
set test "Tab should complete partial hostname"
assert_complete_partial [get_hosts] "ssh"
sync_after_int
teardown teardown

View File

@ -56,9 +56,9 @@ proc assert_complete {expected cmd {test ""} {prompt /@} {size 20}} {
send "$cmd\t" send "$cmd\t"
if {[llength $expected] == 1} { if {[llength $expected] == 1} {
expect -ex "$cmd" expect -ex "$cmd"
# Assume second word is word to complete on. # Assume second word is word to complete on.
set cur [lindex [split $cmd] 1] set cur [lindex [split $cmd] 1]
# Remove second word from beginning of single item $expected # Remove second word from beginning of single item $expected
if {[string first $cur $expected] == 0} { if {[string first $cur $expected] == 0} {
set expected [string range $expected [string length $cur] end] set expected [string range $expected [string length $cur] end]
}; # if }; # if
@ -82,6 +82,33 @@ proc assert_complete {expected cmd {test ""} {prompt /@} {size 20}} {
}; # assert_complete() }; # assert_complete()
# Make sure a partial argument is completed.
# A completion is tried with `$partial', or if this is empty, the first
# character of the first item of `$expected'. Only the items from $expected,
# starting with this character are then expected as completions.
# @param list $expected List of all completions.
# @param string $cmd Command given to generate items
# @param string $test (optional) Test titel. Default is "$cmd<TAB> should show completions"
# @param integer $size (optional) Chunk size. Default is 20.
# @result boolean True if successful, False if not
proc assert_complete_partial {expected cmd {partial ""} {test ""} {prompt /@} {size 20}} {
if {$test == ""} {set test "$cmd should complete partial argument"}
if {[llength $expected] == 0} {
unresolved "$test"
} else {
set pick {}
foreach item $expected {
if {$partial == ""} {set partial [string range $item 0 0]}
# Only append item if starting with $partial
if {[string range $item 0 [expr [string length $partial] - 1]] == "$partial"} {
lappend pick $item
}; # if
}; # foreach
assert_complete $pick "$cmd $partial" $test $prompt $size
}; # if
}; # assert_complete_partial()
# Make sure any completions are returned # Make sure any completions are returned
proc assert_complete_any {cmd {test ""} {prompt /@}} { proc assert_complete_any {cmd {test ""} {prompt /@}} {
if {$test == ""} {set test "$cmd should show completions"} if {$test == ""} {set test "$cmd should show completions"}
@ -186,6 +213,18 @@ proc assert_exec {cmd {stdout ''} {test ''}} {
}; # assert_exec() }; # assert_exec()
# Get known hostnames
# @return list Hostnames
proc get_hosts {} {
set hosts [exec bash -c "compgen -A hostname"]
lappend hosts [exec bash -c {
type avahi-browse >&/dev/null &&
avahi-browse -kcpr _workstation._tcp | grep ^= | cut -d\; -f7 | sort -u
}]
return $hosts
}; # get_hosts()
# Expect items. # Expect items.
# Break items into chunks because `expect' seems to have a limited buffer size # Break items into chunks because `expect' seems to have a limited buffer size
# @param list $items # @param list $items
@ -201,13 +240,24 @@ proc match_items {items test {size 20}} {
# Escape special regexp characters # Escape special regexp characters
regsub -all {([\[\]\(\)\.\\\+])} $item {\\\1} item regsub -all {([\[\]\(\)\.\\\+])} $item {\\\1} item
append expected $item append expected $item
if {[llength $items] > 1} {append expected {\s+}} if {[llength $items] > 1} {append expected {\s+}};
}; # for }; # for
expect { if {[llength $items] == 1} {
-re "$expected" { set result true } expect {
default { set result false; break } -timeout 1
timeout { set result false; break } -re "$expected" { set result true }
}; # expect "\r\n" { set result false; break }
default { set result false; break }
timeout { set result false; break }
}; # expect
} else {
expect {
-timeout 1
-re "$expected" { set result true }
default { set result false; break }
timeout { set result false; break }
}; # expect
}; # if
}; # for }; # for
return $result return $result
}; # match_items() }; # match_items()

View File

@ -12,11 +12,7 @@ setup
set test "Hosts should be put in COMPREPLY" set test "Hosts should be put in COMPREPLY"
set hosts [get_hosts]
# Build string list of hostnames, separated by regexp whitespace marker (\s+).
# Example string: host1\s+host2\s+host3
set hosts [exec bash -c "compgen -A hostname"]
# Hosts `gee' and `hus' are defined in ./fixtures/_known_hosts_real/config # Hosts `gee' and `hus' are defined in ./fixtures/_known_hosts_real/config
# Hosts `doo' and `ike' are defined in ./fixtures/_known_hosts_real/known_hosts # Hosts `doo' and `ike' are defined in ./fixtures/_known_hosts_real/known_hosts
lappend hosts doo gee hus ike lappend hosts doo gee hus ike
@ -37,9 +33,7 @@ sync_after_int
set test "Config file containing space should work" set test "Config file containing space should work"
# Build string list of hostnames, separated by regexp whitespace marker (\s+). set hosts [get_hosts]
# Example string: host1\s+host2\s+host3
set hosts [exec bash -c "compgen -A hostname"]
# Hosts `gee' and `hus' are defined in ./fixtures/_known_hosts_real/spaced conf # Hosts `gee' and `hus' are defined in ./fixtures/_known_hosts_real/spaced conf
# Hosts `doo' and `ike' are defined in ./fixtures/_known_hosts_real/known_hosts # Hosts `doo' and `ike' are defined in ./fixtures/_known_hosts_real/known_hosts
# Host `two' is defined in ./fixtures/_known_hosts_real/known_hosts2 # Host `two' is defined in ./fixtures/_known_hosts_real/known_hosts2
@ -47,7 +41,7 @@ lappend hosts gee hus doo ike two
set hosts [lsort -ascii $hosts] set hosts [lsort -ascii $hosts]
set hosts [join $hosts "\\s+"] set hosts [join $hosts "\\s+"]
# Call _known_hosts # Call _known_hosts
set cmd {_known_hosts -aF 'fixtures/_known_hosts_real/spaced conf'; echo_array COMPREPLY} set cmd {_known_hosts_real -aF 'fixtures/_known_hosts_real/spaced conf'; echo_array COMPREPLY}
send "$cmd\r" send "$cmd\r"
expect -ex "$cmd\r\n" expect -ex "$cmd\r\n"
expect { expect {