This commit is contained in:
Alex Yeung 2017-05-02 02:17:25 +00:00 committed by GitHub
commit e3b1580705

View file

@ -9,13 +9,13 @@
# #
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# #
# Version : 0.1.2 # Version : 0.2.0
# Author : Yonchu <yuyuchu3333@gmail.com> # Author : Yonchu <yuyuchu3333@gmail.com>
# License : MIT License # License : MIT License
# Repository : https://github.com/yonchu/grunt-zsh-completion # Repository : https://github.com/yonchu/grunt-zsh-completion
# Last Change : 20 Aug 2014. # Last Change : 23 Jun 2015.
# #
# Copyright (c) 2013 Yonchu. # Copyright (c) 2015 Yonchu.
# #
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# USAGE # USAGE
@ -114,7 +114,7 @@ function __grunt() {
# The cache variable name: __grunt_version __grunt_gruntfile __grunt_opts __grunt_tasks # The cache variable name: __grunt_version __grunt_gruntfile __grunt_opts __grunt_tasks
function __grunt_update_cache() { function __grunt_update_cache() {
# TODO # TODO
local version='0.1.2' local version='0.2.0'
local is_updating=0 local is_updating=0
local gruntfile="$1" local gruntfile="$1"
local grunt_info no_update_options cache_path local grunt_info no_update_options cache_path
@ -174,50 +174,76 @@ function __grunt_update_cache() {
} }
function __grunt_get_tasks() { function __grunt_get_tasks() {
echo -E "$1" \ echo -E "$1" | awk \
| grep 'Available tasks' -A 100 \ 'BEGIN {
| grep '^ ' \ TASK = ""
| sed -e 's/^[[:blank:]]*//' -e 's/[[:blank:]]*$//' \ SUB_NR = 0
| sed -e 's/:/\\:/g' \ }
| sed -e 's/ /:/' /Available tasks$/, NF == 0 {
SUB_NR++
if (SUB_NR == 1 || NF == 0) next
if ($0 ~ /^[[:blank:]]+[^[:blank:]]+ [^[:blank:]]/) {
if (TASK != "") {
print TASK
TASK = ""
}
sub(/^[ \t]+/, "")
sub(/[ \t]+$/, "")
gsub(/:/, "\\:")
sub(/[ ][ ]/, ":")
TASK = $0
} else {
sub(/^[ \t]+/, "")
sub(/[ \t]+$/, "")
gsub(/:/, "\\:")
TASK = TASK " " $0
}
}
END {
if (TASK != "") {
print TASK
}
}'
} }
function __grunt_get_opts() { function __grunt_get_opts() {
local opt_hunk opt_sep opt_num line opt # ex.)
opt_hunk=$(echo -E "$1" \ # before:
| grep 'Options$' -A 100 \ # --xxx, -y description....
| sed '1 d' \ # after:
| sed -e 's/[[:blank:]]*$//' \ # (--xxx,-y)--xxx[description]"
) # (--xxx,-y)-y[description]"
echo -E "$1" | awk \
opt_sep=() 'BEGIN {
opt_hunk=(${(f)opt_hunk}) OPT_COMP_NUM = -1
opt_num=0 SUB_NR = 0
for line in "$opt_hunk[@]"; do }
opt=$(echo -E "$line" | sed -e 's/^[[:blank:]]*//') /Options$/, NF == 0 {
if [[ $line == $opt ]]; then SUB_NR++
break if (SUB_NR == 1 || NF == 0) next
fi sub(/^[ \t]+/, "")
if [[ $opt != ${opt#-} ]]; then sub(/[ \t]+$/, "")
# Start with - if ($0 ~ /^-.* [^[:blank:]]/) {
(( opt_num++ )) OPT_COMP_NUM++
opt=$(echo -E "$opt" | sed 's/^\(\(--[^ ]*\)\(, \(-[^ ]*\)\)*\) */\2\\t\4\\\t/') OPT_COMP[OPT_COMP_NUM] = $0
fi } else {
opt_sep[$opt_num]=("${opt_sep[$opt_num]}${opt}") OPT_COMP[OPT_COMP_NUM] = OPT_COMP[OPT_COMP_NUM] " " $0
done }
}
for line in "$opt_sep[@]"; do END {
opt=(${(s:\t:)line}) for (i = 0; i <= OPT_COMP_NUM; i++) {
if [[ ${opt[1]} == '--help' ]]; then split(OPT_COMP[i], opt_desc, " ")
continue opt_hunk = opt_desc[1]
fi desc = opt_desc[2]
if [[ ${#opt} -eq 2 ]]; then gsub(/ /, "", opt_hunk)
echo -E "(${opt[1]})${opt[1]}[${opt[2]}]" if (opt_hunk == "--help,-h") continue
else split(opt_hunk, opts, ",")
echo -E "(${opt[1]},${opt[2]})${opt[1]}[${opt[3]}]" for (opt in opts) {
echo -E "(${opt[1]},${opt[2]})${opt[2]}[${opt[3]}]" printf "(%s)%s[%s]", opt_hunk, opts[opt], desc
fi print ""
done }
}
}'
} }
function __grunt_get_gruntfile() { function __grunt_get_gruntfile() {
@ -252,4 +278,4 @@ function __grunt_caching_policy() {
(( $#oldp )) (( $#oldp ))
} }
compdef __grunt grunt __grunt "$@"