Fork awk only once, simplify.

This commit is contained in:
Jorge Bucaran 2016-02-25 21:50:02 +09:00
parent bb3b2bfa79
commit 7325acd166
No known key found for this signature in database
GPG key ID: E54BA3C0E646DB30

View file

@ -4,6 +4,7 @@ function fisher_search -d "Search Plugins"
set -l index set -l index
set -l join "||" set -l join "||"
set -l quiet 0 set -l quiet 0
set -l stdout /dev/stdout
getopts $argv | while read -l 1 2 3 getopts $argv | while read -l 1 2 3
switch "$1" switch "$1"
@ -59,6 +60,7 @@ function fisher_search -d "Search Plugins"
case q quiet case q quiet
set quiet 1 set quiet 1
set stdout /dev/null
case h case h
printf "Usage: fisher search [<plugins>] [--and|--or] [--quiet] [--help]\n\n" printf "Usage: fisher search [<plugins>] [--and|--or] [--quiet] [--help]\n\n"
@ -86,10 +88,14 @@ function fisher_search -d "Search Plugins"
end end
if test $fisher_last_update -gt $fisher_update_interval -o ! -f $index if test $fisher_last_update -gt $fisher_update_interval -o ! -f $index
debug "Update index" debug "Update index start"
if spin "__fisher_index_update" --error=/dev/null if spin "__fisher_index_update" --error=/dev/null
debug "Update index success"
__fisher_complete_reset __fisher_complete_reset
else
debug "Update index fail"
end end
end end
@ -99,15 +105,14 @@ function fisher_search -d "Search Plugins"
set -e fields[-1] set -e fields[-1]
set -e query[-1] set -e query[-1]
set -l options -v OFS=';'
if test -z "$fields[1]" if test -z "$fields[1]"
set fields '$0' set fields '$0'
set options -v OFS='\n' -v ORS='\n'
end end
awk -F'\n' -v RS='' -v OFS=';' ( awk -v FS='\n' -v RS='' $options "
if test "$fields" = '$0'
printf "%s\nORS=%s" -v '\\n\\n'
end
) "
function tags(tag, _list) { function tags(tag, _list) {
if (!tag) { if (!tag) {
@ -126,9 +131,12 @@ function fisher_search -d "Search Plugins"
return 0 return 0
} }
/^ *#/ { next } { {
delete tag_list delete tag_list
if (\$4) split(\$4, tag_list, \" \")
if (\$4) {
split(\$4, tag_list, \" \")
}
name = \$1 name = \$1
url = \$2 url = \$2
@ -136,25 +144,16 @@ function fisher_search -d "Search Plugins"
author = \$5 author = \$5
} }
$query { print $fields } " $index ^ /dev/null | awk -v quiet=$quiet ' $query {
if (has_records) {
!/^ *$/ { hasRecords = 1 } { print \"\"
if (quiet) {
exit !hasRecords
} else {
records[NR] = $0
}
} }
print $fields
has_records = 1
}
END { END { exit !has_records }
for (i = 1; i <= NR; i++) {
if (i == NR && records[i] ~ /^ *$/) {
break
}
print records[i]
}
exit !hasRecords " $index > $stdout ^ /dev/null
}
'
end end