Skip to content

Commit

Permalink
Add rudimentary search command
Browse files Browse the repository at this point in the history
  • Loading branch information
ingydotnet committed May 13, 2014
1 parent 84c0f80 commit 09bcc9f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
23 changes: 23 additions & 0 deletions cmd/git-hub-search
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -e

command:search() {
get-args query
query="${query// /%20}"
local key_prefix=/items
report-list \
"/search/repositories?q=$query;sort=stars;per_page=PER_PAGE" \
'full_name stargazers_count'
}

format-entry:search() {
if "$raw_output"; then
echo "$2"
else
printf "%d) %-40s (%d stars)\n" "$1" "$2" "$3"
fi
}

# vim: set lisp:

6 changes: 5 additions & 1 deletion lib/git-hub
Expand Up @@ -593,7 +593,11 @@ report-list-process() {
api-get "$url"
OK || return 0
json-var-list "${fields[@]}"
local page_size="$(JSON.cache | tail -n1 | cut -d '/' -f2)"
if [ -n "$key_prefix" ]; then
local page_size="$(JSON.cache | grep "^$key_prefix" | tail -n1 | cut -d '/' -f3)"
else
local page_size="$(JSON.cache | tail -n1 | cut -d '/' -f2)"
fi
[ -z "$page_size" ] && say '--None--' && break
local counter=0
for ((ii = 0; ii <= page_size && counter < list_max; ii++)); do
Expand Down
1 change: 1 addition & 0 deletions lib/git-hub.d/git-hub-search
4 changes: 2 additions & 2 deletions lib/git-hub.d/json-setup.bash
Expand Up @@ -84,7 +84,7 @@ json-var-list() {
local fields="$@"
while IFS='\n' read -r line; do
[[ -z "$line" ]] && break
if [[ "$line" =~ ^/([0-9]+)/([^\ ]+)\ (.*) ]]; then
if [[ "$line" =~ ^$key_prefix/([0-9]+)/([^\ ]+)\ (.*) ]]; then
local value="${BASH_REMATCH[3]}"
[ "$value" == null ] && value=''
value="${value#\"}"
Expand All @@ -97,7 +97,7 @@ json-var-list() {
fi
done < <(
echo "$JSON__cache" |
grep -E "^/[0-9]+/(${fields// /|})\b" || echo ''
grep -E "^$key_prefix/[0-9]+/(${fields// /|})\b" || echo ''
)
}

Expand Down

0 comments on commit 09bcc9f

Please sign in to comment.