mirror of https://github.com/akelge/zsh
Better autocompletion
This commit is contained in:
parent
6b9bbdadb9
commit
ec13969e0f
|
@ -2,14 +2,6 @@
|
||||||
# Start autocomplete
|
# Start autocomplete
|
||||||
autoload -U compinit && compinit -u
|
autoload -U compinit && compinit -u
|
||||||
autoload bashcompinit && bashcompinit
|
autoload bashcompinit && bashcompinit
|
||||||
# autoload -Uz compinit
|
|
||||||
#if [ $(date +'%j') != $(stat -f '%Sm' -t '%j' ~/.zcompdump) ]; then
|
|
||||||
# compinit -C
|
|
||||||
# else
|
|
||||||
# compinit -C
|
|
||||||
# fi
|
|
||||||
autoload -U compsys;
|
|
||||||
compctl -g "*(-/)" + -g ".*(-/)" cd
|
|
||||||
|
|
||||||
# allow approximate
|
# allow approximate
|
||||||
zstyle ':completion:*' completer _complete _match _approximate
|
zstyle ':completion:*' completer _complete _match _approximate
|
||||||
|
@ -23,6 +15,4 @@ zstyle ':completion:*:kill:*' force-list always
|
||||||
# # cd not select parent dir
|
# # cd not select parent dir
|
||||||
zstyle ':completion:*:cd:*' ignore-parents parent pwd
|
zstyle ':completion:*:cd:*' ignore-parents parent pwd
|
||||||
|
|
||||||
# compctl -g "*(-/)" + -g ".*(-/)" cd
|
|
||||||
#
|
|
||||||
# vim: set ts=4 sw=4 tw=0 ft=zsh :
|
# vim: set ts=4 sw=4 tw=0 ft=zsh :
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,117 +0,0 @@
|
||||||
#compdef salt
|
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
# Redistribution and use in source and binary forms, with or without
|
|
||||||
# modification, are permitted provided that the following conditions are met:
|
|
||||||
# * Redistributions of source code must retain the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer.
|
|
||||||
# * Redistributions in binary form must reproduce the above copyright
|
|
||||||
# notice, this list of conditions and the following disclaimer in the
|
|
||||||
# documentation and/or other materials provided with the distribution.
|
|
||||||
# * Neither the name of the zsh-users nor the
|
|
||||||
# names of its contributors may be used to endorse or promote products
|
|
||||||
# derived from this software without specific prior written permission.
|
|
||||||
#
|
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
||||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
|
|
||||||
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
||||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
||||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
||||||
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
||||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
# Description
|
|
||||||
# -----------
|
|
||||||
#
|
|
||||||
# Completion script for salt (http://saltstack.com/).
|
|
||||||
#
|
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
# Authors
|
|
||||||
# -------
|
|
||||||
#
|
|
||||||
# * Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
|
|
||||||
#
|
|
||||||
# ------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
_minions() {
|
|
||||||
ls /etc/salt/pki/master/minions | while read minion; do
|
|
||||||
_wanted 'target' expl 'target' compadd $minion
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
_modules() {
|
|
||||||
for module in $(salt-call sys.list_modules 2&>/dev/null --output=key | tail -n+2); do
|
|
||||||
_wanted 'module' expl 'module' compadd $module
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
_functions() {
|
|
||||||
for fn in $(salt-call sys.list_functions 2&>/dev/null --output=key | tail -n+2); do
|
|
||||||
_wanted 'function' expl 'function' compadd $fn
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
_salt() {
|
|
||||||
local _loglevels
|
|
||||||
_loglevels=(all garbage trace debug info warning error quiet)
|
|
||||||
|
|
||||||
_arguments -s \
|
|
||||||
"--version[show program's version number and exit]" \
|
|
||||||
"--versions-report[show program's dependencies version number and exit]" \
|
|
||||||
"(-h --help)"{-h,--help}"[show help message and exit]" \
|
|
||||||
"(-c --config-dir)"{-c+,--config-dir=}"[Pass in an alternative configuration directory.]:configuration directory:_files -/" \
|
|
||||||
"(-t --timeout)"{-t+,--timeout=}"[Change the timeout, if applicable, for the running command]:timeout" \
|
|
||||||
"(-s --static)"{-s,--static}"[Return the data from minions as a group after they all return]" \
|
|
||||||
"--async[Run the salt command but don't wait for a reply]" \
|
|
||||||
"(--state-output --state_output)"{--state-output=,--state_output=}"[Override the configured state_output value for minion output]:state output" \
|
|
||||||
"--subset=[Execute the routine on a random subset of the targeted minions]:subset" \
|
|
||||||
"(-v --verbose)"{-v,--verbose}"[Turn on command verbosity, display jid and active job queries]" \
|
|
||||||
"--show-timeout[Display minions that timeout]" \
|
|
||||||
"(-b --batch --batch-size)"{-b+,--batch=,--batch-size=}"[Execute the salt job in batch mode]:batch" \
|
|
||||||
"(-a --auth --eauth --extended-auth)"{-a+,--auth=,--eauth=,--extended--auth=}"[Specify an extended authentication system to use]:eauth" \
|
|
||||||
"(-T --make-token)"{-T,--make-token}"[Generate and save an authentication token for re-use]" \
|
|
||||||
"--return=[Set an alternative return method]:returner" \
|
|
||||||
"(-d --doc --documentation)"{-d,--doc,--documentation}"[Return the documentation for the specified module or for all modules]::function:_functions" \
|
|
||||||
"--args-separator=[Set the special argument used as a delimiter between command arguments of compound commands]:args separator" \
|
|
||||||
\
|
|
||||||
"(-l --log-level)"{-l+,--log-level=}"[Console logging log level]:level:($_loglevels[@])" \
|
|
||||||
"--log-file=[Log file path]:log file:_files" \
|
|
||||||
"--log-file-level=[Logfile logging log level]:level:($_loglevels[@])" \
|
|
||||||
\
|
|
||||||
"(-E --pcre)"{-E,--pcre}"[Target servers using pcre regular expressions]" \
|
|
||||||
"(-L --list)"{-L,--list}"[Target servers using a comma or space delimited list of servers]" \
|
|
||||||
"(-G --grain)"{-G,--grain}"[Target servers using a grain value]" \
|
|
||||||
"--grain-pcre[Target servers using a grain value matched by a pcre regular expression]" \
|
|
||||||
"(-N --nodegroup)"{-N,--nodegroup}"[Target servers using a predefined nodegroup]" \
|
|
||||||
"(-R --range)"{-R,--range}"[Target servers using range expression]" \
|
|
||||||
"(-C --compound)"{-C,--compound}"[Target servers using compound selectors]" \
|
|
||||||
"(-X --exsel)"{-X,--exsel}"[Target servers using the return code of a function]" \
|
|
||||||
"(-I --pillar)"{-I,--pillar}"[Target servers using a pillar value]" \
|
|
||||||
"(-S --ipcidr)"{-S,--ipcidr}"[Match servers based on subnet (CIDR or IPv4 address)]" \
|
|
||||||
\
|
|
||||||
"(--out --output)"{--out=,--output=}"[Print the output using the specified outputter]:outputter:(no_return grains yaml overstatestage json pprint nested raw highstate quiet key txt virt_query)" \
|
|
||||||
"(--out-indent --output-indent)"{--out-indent=,--output-indent=}"[Print the output indented by the provided value in spaces]:nr spaces" \
|
|
||||||
"(--out-file --output-file)"{--out-file=,--output-file=}"[Write the output to the specified file]:output:_files" \
|
|
||||||
"(--no-color --no-colour)"{--no-color,--no-colour}"[Disable all colored output]" \
|
|
||||||
"(--force-color --force-colour)"{--force-color,--force-colour}"[Force colored output]" \
|
|
||||||
\
|
|
||||||
'1:target:_minions' \
|
|
||||||
'2:function:_functions' \
|
|
||||||
'*::arguments'
|
|
||||||
}
|
|
||||||
|
|
||||||
case "$service" in
|
|
||||||
salt)
|
|
||||||
_salt "@"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
# Local Variables:
|
|
||||||
# mode: Shell-Script
|
|
||||||
# sh-indentation: 2
|
|
||||||
# indent-tabs-mode: nil
|
|
||||||
# sh-basic-offset: 2
|
|
||||||
# End:
|
|
||||||
# vim: ft=zsh sw=2 ts=2 et
|
|
|
@ -1,178 +0,0 @@
|
||||||
#compdef _stern stern
|
|
||||||
|
|
||||||
# zsh completion for stern -*- shell-script -*-
|
|
||||||
|
|
||||||
__stern_debug()
|
|
||||||
{
|
|
||||||
local file="$BASH_COMP_DEBUG_FILE"
|
|
||||||
if [[ -n ${file} ]]; then
|
|
||||||
echo "$*" >> "${file}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
_stern()
|
|
||||||
{
|
|
||||||
local shellCompDirectiveError=1
|
|
||||||
local shellCompDirectiveNoSpace=2
|
|
||||||
local shellCompDirectiveNoFileComp=4
|
|
||||||
local shellCompDirectiveFilterFileExt=8
|
|
||||||
local shellCompDirectiveFilterDirs=16
|
|
||||||
|
|
||||||
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace
|
|
||||||
local -a completions
|
|
||||||
|
|
||||||
__stern_debug "\n========= starting completion logic =========="
|
|
||||||
__stern_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
|
|
||||||
|
|
||||||
# The user could have moved the cursor backwards on the command-line.
|
|
||||||
# We need to trigger completion from the $CURRENT location, so we need
|
|
||||||
# to truncate the command-line ($words) up to the $CURRENT location.
|
|
||||||
# (We cannot use $CURSOR as its value does not work when a command is an alias.)
|
|
||||||
words=("${=words[1,CURRENT]}")
|
|
||||||
__stern_debug "Truncated words[*]: ${words[*]},"
|
|
||||||
|
|
||||||
lastParam=${words[-1]}
|
|
||||||
lastChar=${lastParam[-1]}
|
|
||||||
__stern_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
|
|
||||||
|
|
||||||
# For zsh, when completing a flag with an = (e.g., stern -n=<TAB>)
|
|
||||||
# completions must be prefixed with the flag
|
|
||||||
setopt local_options BASH_REMATCH
|
|
||||||
if [[ "${lastParam}" =~ '-.*=' ]]; then
|
|
||||||
# We are dealing with a flag with an =
|
|
||||||
flagPrefix="-P ${BASH_REMATCH}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Prepare the command to obtain completions
|
|
||||||
requestComp="${words[1]} __complete ${words[2,-1]}"
|
|
||||||
if [ "${lastChar}" = "" ]; then
|
|
||||||
# If the last parameter is complete (there is a space following it)
|
|
||||||
# We add an extra empty parameter so we can indicate this to the go completion code.
|
|
||||||
__stern_debug "Adding extra empty parameter"
|
|
||||||
requestComp="${requestComp} \"\""
|
|
||||||
fi
|
|
||||||
|
|
||||||
__stern_debug "About to call: eval ${requestComp}"
|
|
||||||
|
|
||||||
# Use eval to handle any environment variables and such
|
|
||||||
out=$(eval ${requestComp} 2>/dev/null)
|
|
||||||
__stern_debug "completion output: ${out}"
|
|
||||||
|
|
||||||
# Extract the directive integer following a : from the last line
|
|
||||||
local lastLine
|
|
||||||
while IFS='\n' read -r line; do
|
|
||||||
lastLine=${line}
|
|
||||||
done < <(printf "%s\n" "${out[@]}")
|
|
||||||
__stern_debug "last line: ${lastLine}"
|
|
||||||
|
|
||||||
if [ "${lastLine[1]}" = : ]; then
|
|
||||||
directive=${lastLine[2,-1]}
|
|
||||||
# Remove the directive including the : and the newline
|
|
||||||
local suffix
|
|
||||||
(( suffix=${#lastLine}+2))
|
|
||||||
out=${out[1,-$suffix]}
|
|
||||||
else
|
|
||||||
# There is no directive specified. Leave $out as is.
|
|
||||||
__stern_debug "No directive found. Setting do default"
|
|
||||||
directive=0
|
|
||||||
fi
|
|
||||||
|
|
||||||
__stern_debug "directive: ${directive}"
|
|
||||||
__stern_debug "completions: ${out}"
|
|
||||||
__stern_debug "flagPrefix: ${flagPrefix}"
|
|
||||||
|
|
||||||
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
|
|
||||||
__stern_debug "Completion received error. Ignoring completions."
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
while IFS='\n' read -r comp; do
|
|
||||||
if [ -n "$comp" ]; then
|
|
||||||
# If requested, completions are returned with a description.
|
|
||||||
# The description is preceded by a TAB character.
|
|
||||||
# For zsh's _describe, we need to use a : instead of a TAB.
|
|
||||||
# We first need to escape any : as part of the completion itself.
|
|
||||||
comp=${comp//:/\\:}
|
|
||||||
|
|
||||||
local tab=$(printf '\t')
|
|
||||||
comp=${comp//$tab/:}
|
|
||||||
|
|
||||||
__stern_debug "Adding completion: ${comp}"
|
|
||||||
completions+=${comp}
|
|
||||||
lastComp=$comp
|
|
||||||
fi
|
|
||||||
done < <(printf "%s\n" "${out[@]}")
|
|
||||||
|
|
||||||
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
|
|
||||||
__stern_debug "Activating nospace."
|
|
||||||
noSpace="-S ''"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
|
|
||||||
# File extension filtering
|
|
||||||
local filteringCmd
|
|
||||||
filteringCmd='_files'
|
|
||||||
for filter in ${completions[@]}; do
|
|
||||||
if [ ${filter[1]} != '*' ]; then
|
|
||||||
# zsh requires a glob pattern to do file filtering
|
|
||||||
filter="\*.$filter"
|
|
||||||
fi
|
|
||||||
filteringCmd+=" -g $filter"
|
|
||||||
done
|
|
||||||
filteringCmd+=" ${flagPrefix}"
|
|
||||||
|
|
||||||
__stern_debug "File filtering command: $filteringCmd"
|
|
||||||
_arguments '*:filename:'"$filteringCmd"
|
|
||||||
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
|
|
||||||
# File completion for directories only
|
|
||||||
local subDir
|
|
||||||
subdir="${completions[1]}"
|
|
||||||
if [ -n "$subdir" ]; then
|
|
||||||
__stern_debug "Listing directories in $subdir"
|
|
||||||
pushd "${subdir}" >/dev/null 2>&1
|
|
||||||
else
|
|
||||||
__stern_debug "Listing directories in ."
|
|
||||||
fi
|
|
||||||
|
|
||||||
local result
|
|
||||||
_arguments '*:dirname:_files -/'" ${flagPrefix}"
|
|
||||||
result=$?
|
|
||||||
if [ -n "$subdir" ]; then
|
|
||||||
popd >/dev/null 2>&1
|
|
||||||
fi
|
|
||||||
return $result
|
|
||||||
else
|
|
||||||
__stern_debug "Calling _describe"
|
|
||||||
if eval _describe "completions" completions $flagPrefix $noSpace; then
|
|
||||||
__stern_debug "_describe found some completions"
|
|
||||||
|
|
||||||
# Return the success of having called _describe
|
|
||||||
return 0
|
|
||||||
else
|
|
||||||
__stern_debug "_describe did not find completions."
|
|
||||||
__stern_debug "Checking if we should do file completion."
|
|
||||||
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
|
|
||||||
__stern_debug "deactivating file completion"
|
|
||||||
|
|
||||||
# We must return an error code here to let zsh know that there were no
|
|
||||||
# completions found by _describe; this is what will trigger other
|
|
||||||
# matching algorithms to attempt to find completions.
|
|
||||||
# For example zsh can match letters in the middle of words.
|
|
||||||
return 1
|
|
||||||
else
|
|
||||||
# Perform file completion
|
|
||||||
__stern_debug "Activating file completion"
|
|
||||||
|
|
||||||
# We must return the result of this command, so it must be the
|
|
||||||
# last command, or else we must store its result to return it.
|
|
||||||
_arguments '*:filename:_files'" ${flagPrefix}"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# don't run the completion function when being source-ed or eval-ed
|
|
||||||
if [ "$funcstack[1]" = "_stern" ]; then
|
|
||||||
_stern
|
|
||||||
fi
|
|
||||||
compdef _stern stern
|
|
Loading…
Reference in New Issue