fisher/functions/fisher_search.fish
Jorge Bucaran 916ea28c1e
Power up search Close #115,#128
* Search now has a color display mode enabled by default when listing
  records for human consumption, but continues to produce easy to parse
  output when selecting specific fields.

* The following example queries:

  * fisher search --name
  * fisher search --url
  * fisher search --name --url
  * fisher search --tag=prompt --name

  and so forth continue to display easy to parse output.

* The following example queries:

  * fisher search
  * fisher search --name=fishtape
  * fisher search --tag=prompt
  * fisher search --author=joe

  display in color by default and support multiple formats using the
  --format option described below. The colors used are selected from
  $fish_color_* variables for best results.

* To disable color output, use --no-color. To customize the display
  format use any of the following keywords:

  * --format=*oneline* (default)
  * --format=*short*
  * --format=*verbose*
  * --format=*longline*
  * --format=*raw*

* Search now shows unique records when listing --authors only. #128
2016-02-26 16:59:47 +09:00

268 lines
7.2 KiB
Fish

function fisher_search -d "Search Plugins"
set -l fields
set -l query
set -l index
set -l join "||"
set -l format
set -l option
set -l stdout /dev/stdout
getopts $argv | while read -l 1 2 3
switch "$1"
case fmt format
set format "$2"
case _
switch "$2"
case \*/\*
set -l url (__fisher_plugin_validate $2)
if test ! -z "$url"
set 2 $url
end
set query $query "url==\"$2\"" $join
case \*
set query $query "name==\"$2\"" $join
end
case name url info author
if test -z "$2"
set fields $fields $1 ,
else
switch "$2"
case ~\*
set query $query "$1$3$2" $join
case \*
if test -z "$3"
set 3 =
end
set query $query "$1$3=\"$2\"" $join
end
end
case tag{,s}
if test -z "$2"
set fields $fields "tags(0)" ,
else
set query $query "$3 tags(\"$2\")" $join
end
case a and
set join "&&"
case o or
set join "||"
case no-color
set option no-color
case query
set query $query $2
case index
set index $2
case q quiet
set stdout /dev/null
case h
printf "Usage: fisher search [<plugins>] [--format=<format>] [--and|--or]\n"
printf " [--no-color] [--quiet] [--help]\n\n"
printf " -a --and Join query with AND operator\n"
printf " -o --or Join query with OR operator\n"
printf " --no-color Turn off color display\n"
printf " --format=<format> Use format to display results\n"
printf " -q --quiet Enable quiet mode\n"
printf " -h --help Show usage help\n"
return
case \*
printf "fisher: '%s' is not a valid option.\n" $1 > /dev/stderr
fisher_search -h > /dev/stderr
return 1
end
end
if test -z "$index"
set index $fisher_cache/.index
set fisher_last_update (math (date +%s) - "0$fisher_last_update")
if not set -q fisher_update_interval
set -g fisher_update_interval 500000
end
if test $fisher_last_update -gt $fisher_update_interval -o ! -f $index
debug "Update index start"
if spin "__fisher_index_update" --error=/dev/null
debug "Update index success"
__fisher_complete_reset
else
debug "Update index fail"
end
end
set -U fisher_last_update (date +%s)
end
set -e fields[-1]
set -e query[-1]
set -l options -v OFS=';' -v compact=1
if test -z "$fields[1]"
set options -v OFS='\n'
if test -z "$format"
if test -z "$fish_search_format"
set format default
else
set format "$fish_search_format"
end
end
set -l name_color (set_color $fish_color_command)
set -l url_color (set_color $fish_color_cwd -u)
set -l tag_color (set_color $fish_color_cwd)
set -l weak_color (set_color white)
set -l author_color (set_color -u)
set -l normal (set_color $fish_color_normal)
if contains -- no-color $option
set name_color
set url_color
set tag_color
set weak_color
set author_color
set normal
end
set legend
set local (fisher -l | awk '
!/^@/ {
if (append) {
printf("|")
}
printf("%s", substr($0, 2))
append++
}
'
)
if test ! -z "$local"
set legend " "
end
set fields 'if ("'"$local"'" && $1~/'"$local"'/) {'
switch "$format"
case default oneline
set fields $fields '
printf("* '"$weak_color"'%-18s'"$normal"' %s\n", $1, $3)
} else {
printf("'"$legend$name_color"'%-18s'"$normal"' %s\n", $1, $3)
}
'
set options $options -v compact=1
case longline
set fields $fields '
printf("%-40s * '"$weak_color"'%-18s'"$normal"' %s\n", humanize_url($2), $1, $3)
} else {
printf("'"$tag_color"'%-40s'"$normal"' '"$legend$name_color"'%-18s'"$normal"' %s\n", humanize_url($2), $1, $3)
}
'
set options $options -v compact=1
case short
set fields $fields '
printf("'"$weak_color"'*%s by %s\n%s'"$normal"'\n%s\n", $1, $5, $3, humanize_url($2))
} else {
printf("'"$name_color"'%s'"$normal"' by '"$author_color"'%s'"$normal"'\n%s\n'"$url_color"'%s'"$normal"'\n", $1, $5, $3, humanize_url($2))
}
'
case verbose
set fields $fields '
printf("'"$weak_color"'*%s by %s\n%s'"$normal"'\n%s\n%s\n", $1, $5, $3, $4, humanize_url($2))
} else {
printf("'"$name_color"'%s'"$normal"' by '"$author_color"'%s'"$normal"'\n%s\n'"$tag_color"'%s'"$normal"'\n'"$url_color"'%s'"$normal"'\n", $1, $5, $3, $4, humanize_url($2))
}
'
case raw
set fields print
end
else
if test "$fields" = author
set options $options -v unique=1
end
set fields print $fields
end
awk -v FS='\n' -v RS='' $options "
function humanize_url(url) {
gsub(\"(https?://)?(www.)?|/\$\", \"\", url)
return url
}
function tags(tag, _list) {
if (!tag) {
for (i in tag_list) {
if (!seen[tag_list[i]]++) {
_list = tag_list[i] \"\n\" _list
}
}
return substr(_list, 1, length(_list) - 1)
}
for (i in tag_list) {
if (tag == tag_list[i]) {
return 1
}
}
return 0
}
{
delete tag_list
if (\$4) {
split(\$4, tag_list, \" \")
}
name = \$1
url = \$2
info = \$3
author = \$5
}
$query {
if (has_records && !compact) {
print \"\"
}
if (!shit[\$5] || !unique) {
shit[\$5] = 1
$fields
}
has_records = 1
}
END { exit !has_records }
" $index > $stdout ^ /dev/null
end