diff options
author | Hsieh Chin Fan <typebrook@gmail.com> | 2020-06-19 10:52:13 +0800 |
---|---|---|
committer | Hsieh Chin Fan <typebrook@gmail.com> | 2020-06-19 10:52:13 +0800 |
commit | 0e5187731a7269a6841d1345cde64ab8b74bd238 (patch) | |
tree | cb0b78456186c0eae577c2be7394c94cc7c1a11a | |
parent | a5086bf8102bdb868d8fac56baaf32153226ea1d (diff) |
Keep gist_id instead of html_url
-rwxr-xr-x | gist | 28 |
1 files changed, 15 insertions, 13 deletions
@@ -62,7 +62,7 @@ GITHUB_API=https://api.github.com | |||
62 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 62 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config |
63 | per_page=100 | 63 | per_page=100 |
64 | 64 | ||
65 | INDEX_FORMAT=('index' 'public' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') | 65 | INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') |
66 | TAG_CHAR='-_[:alnum:]' | 66 | TAG_CHAR='-_[:alnum:]' |
67 | if [[ ! -t 0 ]]; then | 67 | if [[ ! -t 0 ]]; then |
68 | INPUT=$(cat) | 68 | INPUT=$(cat) |
@@ -300,7 +300,7 @@ _print_records() { | |||
300 | 300 | ||
301 | sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ | 301 | sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ |
302 | | while read -r "${INDEX_FORMAT[@]}"; do | 302 | | while read -r "${INDEX_FORMAT[@]}"; do |
303 | local message=$url | 303 | local message=$(_site_url $gist_id) |
304 | if [[ $display == 'tag' ]]; then | 304 | if [[ $display == 'tag' ]]; then |
305 | local tags=( ${tags_string//,/ } ); message="${tags[@]}" | 305 | local tags=( ${tags_string//,/ } ); message="${tags[@]}" |
306 | [[ $show_untagged == 'false' && ${#tags[@]} == '0' ]] && continue | 306 | [[ $show_untagged == 'false' && ${#tags[@]} == '0' ]] && continue |
@@ -311,7 +311,7 @@ _print_records() { | |||
311 | fi | 311 | fi |
312 | 312 | ||
313 | local extra="$(printf "%-4s" "$file_num $comment_num")" | 313 | local extra="$(printf "%-4s" "$file_num $comment_num")" |
314 | local status=''; status=$(_check_repo_status "$folder/${url##*/}" "$blob_code") | 314 | local status=''; status=$(_check_repo_status "${folder}/${gist_id}" "$blob_code") |
315 | [[ $index =~ ^s ]] && description="$(printf "%-12s" [${author}]) ${description}" | 315 | [[ $index =~ ^s ]] && description="$(printf "%-12s" [${author}]) ${description}" |
316 | 316 | ||
317 | raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra ${status:+${status} }$(_color_pattern '^(\[.+\])' <<<"$description")" | 317 | raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra ${status:+${status} }$(_color_pattern '^(\[.+\])' <<<"$description")" |
@@ -337,7 +337,7 @@ _grep_content() { | |||
337 | if grep --color=always -iq "$1" <<<"$description"; then | 337 | if grep --color=always -iq "$1" <<<"$description"; then |
338 | hint=false mark="$index " _print_records | 338 | hint=false mark="$index " _print_records |
339 | else | 339 | else |
340 | local repo=$folder/${url##*/} | 340 | local repo=${folder}/${gist_id} |
341 | [[ -d $repo ]] && cd $repo || continue | 341 | [[ -d $repo ]] && cd $repo || continue |
342 | # grep from filenames | 342 | # grep from filenames |
343 | local file=$(ls $repo | grep --color=always -Ei "$1") | 343 | local file=$(ls $repo | grep --color=always -Ei "$1") |
@@ -373,7 +373,8 @@ for gist in raw: | |||
373 | # Parse response from 'gist fetch' to the format for index file | 373 | # Parse response from 'gist fetch' to the format for index file |
374 | _parse_response() { | 374 | _parse_response() { |
375 | _parse_gists \ | 375 | _parse_gists \ |
376 | | while read -r "${INDEX_FORMAT[@]:1:2}" file_url_array "${INDEX_FORMAT[@]:5:7}"; do | 376 | | while read -r "${INDEX_FORMAT[@]:1:1}" html_url file_url_array "${INDEX_FORMAT[@]:5:7}"; do |
377 | local gist_id=${html_url##*/} | ||
377 | local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 378 | local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
378 | file_array=${file_array//@None/@Text} | 379 | file_array=${file_array//@None/@Text} |
379 | 380 | ||
@@ -450,7 +451,7 @@ _query_user() { | |||
450 | route=users/$1/gists _fetch_gists_with_pagnation $fetched_records | 451 | route=users/$1/gists _fetch_gists_with_pagnation $fetched_records |
451 | tac $fetched_records | nl -s' ' \ | 452 | tac $fetched_records | nl -s' ' \ |
452 | | while read -r ${INDEX_FORMAT[@]}; do | 453 | | while read -r ${INDEX_FORMAT[@]}; do |
453 | echo $index $url $author $file_num $comment_num $description | cut -c -"$(tput cols)" | 454 | echo $index $gist_id $author $file_num $comment_num $description | cut -c -"$(tput cols)" |
454 | done || { echo "Failed to query $1's gists"; exit 1; } | 455 | done || { echo "Failed to query $1's gists"; exit 1; } |
455 | } | 456 | } |
456 | 457 | ||
@@ -475,7 +476,7 @@ _pull_if_needed() { | |||
475 | # Update local git repos | 476 | # Update local git repos |
476 | _sync_repos() { | 477 | _sync_repos() { |
477 | comm -1 <(ls -A "$folder" | sort) \ | 478 | comm -1 <(ls -A "$folder" | sort) \ |
478 | <(while read -r ${INDEX_FORMAT[@]}; do echo $index $url; done < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \ | 479 | <(while read -r ${INDEX_FORMAT[@]}; do echo $index $gist_id; done < "$INDEX" | sed -ne "/^$mark/ p" | sort) \ |
479 | | { | 480 | | { |
480 | result=$(cat) | 481 | result=$(cat) |
481 | 482 | ||
@@ -491,6 +492,8 @@ _sync_repos() { | |||
491 | } | 492 | } |
492 | } | 493 | } |
493 | 494 | ||
495 | _site_url() { echo https://gist.github.com/$1; } | ||
496 | |||
494 | # Get the url where to clone repo, take user and repo name as parameters | 497 | # Get the url where to clone repo, take user and repo name as parameters |
495 | _repo_url() { | 498 | _repo_url() { |
496 | if [[ $protocol == 'ssh' ]]; then | 499 | if [[ $protocol == 'ssh' ]]; then |
@@ -503,7 +506,7 @@ _repo_url() { | |||
503 | # Get gist id from index files | 506 | # Get gist id from index files |
504 | _gist_id() { | 507 | _gist_id() { |
505 | read -r ${INDEX_FORMAT[@]} <<<"$(sed -ne "/^$1 / p" $INDEX)" | 508 | read -r ${INDEX_FORMAT[@]} <<<"$(sed -ne "/^$1 / p" $INDEX)" |
506 | GIST_ID=${url##*/} | 509 | GIST_ID=${gist_id} |
507 | 510 | ||
508 | if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then | 511 | if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then |
509 | echo -e "$(hint=false _print_records | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" | 512 | echo -e "$(hint=false _print_records | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" |
@@ -566,14 +569,14 @@ _delete_gist() { | |||
566 | _gist_id "$i" &> /dev/null || continue | 569 | _gist_id "$i" &> /dev/null || continue |
567 | http_method DELETE "$GITHUB_API/gists/$GIST_ID" \ | 570 | http_method DELETE "$GITHUB_API/gists/$GIST_ID" \ |
568 | && echo "$i" deleted \ | 571 | && echo "$i" deleted \ |
569 | && sed -E -i'' -e "/^$i / d" $INDEX | 572 | && sed -i'' -Ee "/^$i / d" $INDEX |
570 | done | 573 | done |
571 | } | 574 | } |
572 | 575 | ||
573 | # Remove repos which are not in index file anymore | 576 | # Remove repos which are not in index file anymore |
574 | _clean_repos() { | 577 | _clean_repos() { |
575 | comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ | 578 | comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ |
576 | <(while read -r ${INDEX_FORMAT[@]}; do echo $url; done < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \ | 579 | <(while read -r ${INDEX_FORMAT[@]}; do echo $gist_id; done < "$INDEX" | sort 2> /dev/null ) \ |
577 | | while read -r dir; do | 580 | | while read -r dir; do |
578 | mv $folder/"$dir" /tmp && echo $folder/"$dir" is moved to /tmp | 581 | mv $folder/"$dir" /tmp && echo $folder/"$dir" is moved to /tmp |
579 | done | 582 | done |
@@ -620,7 +623,7 @@ _export_to_github() { | |||
620 | 623 | ||
621 | _id_to_index() { | 624 | _id_to_index() { |
622 | while read -r ${INDEX_FORMAT[@]}; do | 625 | while read -r ${INDEX_FORMAT[@]}; do |
623 | [[ ! $index =~ s && ${url##*/} == $1 ]] && echo $index | 626 | [[ ! $index =~ s && ${gist_id} == $1 ]] && echo $index |
624 | done <$INDEX | 627 | done <$INDEX |
625 | } | 628 | } |
626 | 629 | ||
@@ -708,8 +711,7 @@ _create_gist() { | |||
708 | | index=$index _parse_response \ | 711 | | index=$index _parse_response \ |
709 | | tee -a $INDEX \ | 712 | | tee -a $INDEX \ |
710 | | while read -r "${INDEX_FORMAT[@]}"; do | 713 | | while read -r "${INDEX_FORMAT[@]}"; do |
711 | local gist_id=${url/##*/} | 714 | git clone "$(_repo_url $gist_id)" ${folder}/${gist_id} |
712 | (git clone "$(_repo_url $gist_id)" $folder/$gist_id &> /dev/null &) | ||
713 | done | 715 | done |
714 | 716 | ||
715 | # shellcheck disable=2181 | 717 | # shellcheck disable=2181 |