From 0e5187731a7269a6841d1345cde64ab8b74bd238 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 19 Jun 2020 10:52:13 +0800 Subject: Keep gist_id instead of html_url --- gist | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/gist b/gist index 08f963a..a6b2ee0 100755 --- a/gist +++ b/gist @@ -62,7 +62,7 @@ GITHUB_API=https://api.github.com CONFIG=~/.config/gist.conf; mkdir -p ~/.config per_page=100 -INDEX_FORMAT=('index' 'public' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') +INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') TAG_CHAR='-_[:alnum:]' if [[ ! -t 0 ]]; then INPUT=$(cat) @@ -300,7 +300,7 @@ _print_records() { sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ | while read -r "${INDEX_FORMAT[@]}"; do - local message=$url + local message=$(_site_url $gist_id) if [[ $display == 'tag' ]]; then local tags=( ${tags_string//,/ } ); message="${tags[@]}" [[ $show_untagged == 'false' && ${#tags[@]} == '0' ]] && continue @@ -311,7 +311,7 @@ _print_records() { fi local extra="$(printf "%-4s" "$file_num $comment_num")" - local status=''; status=$(_check_repo_status "$folder/${url##*/}" "$blob_code") + local status=''; status=$(_check_repo_status "${folder}/${gist_id}" "$blob_code") [[ $index =~ ^s ]] && description="$(printf "%-12s" [${author}]) ${description}" raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra ${status:+${status} }$(_color_pattern '^(\[.+\])' <<<"$description")" @@ -337,7 +337,7 @@ _grep_content() { if grep --color=always -iq "$1" <<<"$description"; then hint=false mark="$index " _print_records else - local repo=$folder/${url##*/} + local repo=${folder}/${gist_id} [[ -d $repo ]] && cd $repo || continue # grep from filenames local file=$(ls $repo | grep --color=always -Ei "$1") @@ -373,7 +373,8 @@ for gist in raw: # Parse response from 'gist fetch' to the format for index file _parse_response() { _parse_gists \ - | while read -r "${INDEX_FORMAT[@]:1:2}" file_url_array "${INDEX_FORMAT[@]:5:7}"; do + | while read -r "${INDEX_FORMAT[@]:1:1}" html_url file_url_array "${INDEX_FORMAT[@]:5:7}"; do + local gist_id=${html_url##*/} local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) file_array=${file_array//@None/@Text} @@ -450,7 +451,7 @@ _query_user() { route=users/$1/gists _fetch_gists_with_pagnation $fetched_records tac $fetched_records | nl -s' ' \ | while read -r ${INDEX_FORMAT[@]}; do - echo $index $url $author $file_num $comment_num $description | cut -c -"$(tput cols)" + echo $index $gist_id $author $file_num $comment_num $description | cut -c -"$(tput cols)" done || { echo "Failed to query $1's gists"; exit 1; } } @@ -475,7 +476,7 @@ _pull_if_needed() { # Update local git repos _sync_repos() { comm -1 <(ls -A "$folder" | sort) \ - <(while read -r ${INDEX_FORMAT[@]}; do echo $index $url; done < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \ + <(while read -r ${INDEX_FORMAT[@]}; do echo $index $gist_id; done < "$INDEX" | sed -ne "/^$mark/ p" | sort) \ | { result=$(cat) @@ -491,6 +492,8 @@ _sync_repos() { } } +_site_url() { echo https://gist.github.com/$1; } + # Get the url where to clone repo, take user and repo name as parameters _repo_url() { if [[ $protocol == 'ssh' ]]; then @@ -503,7 +506,7 @@ _repo_url() { # Get gist id from index files _gist_id() { read -r ${INDEX_FORMAT[@]} <<<"$(sed -ne "/^$1 / p" $INDEX)" - GIST_ID=${url##*/} + GIST_ID=${gist_id} if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then echo -e "$(hint=false _print_records | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" @@ -566,14 +569,14 @@ _delete_gist() { _gist_id "$i" &> /dev/null || continue http_method DELETE "$GITHUB_API/gists/$GIST_ID" \ && echo "$i" deleted \ - && sed -E -i'' -e "/^$i / d" $INDEX + && sed -i'' -Ee "/^$i / d" $INDEX done } # Remove repos which are not in index file anymore _clean_repos() { comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ - <(while read -r ${INDEX_FORMAT[@]}; do echo $url; done < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \ + <(while read -r ${INDEX_FORMAT[@]}; do echo $gist_id; done < "$INDEX" | sort 2> /dev/null ) \ | while read -r dir; do mv $folder/"$dir" /tmp && echo $folder/"$dir" is moved to /tmp done @@ -620,7 +623,7 @@ _export_to_github() { _id_to_index() { while read -r ${INDEX_FORMAT[@]}; do - [[ ! $index =~ s && ${url##*/} == $1 ]] && echo $index + [[ ! $index =~ s && ${gist_id} == $1 ]] && echo $index done <$INDEX } @@ -708,8 +711,7 @@ _create_gist() { | index=$index _parse_response \ | tee -a $INDEX \ | while read -r "${INDEX_FORMAT[@]}"; do - local gist_id=${url/##*/} - (git clone "$(_repo_url $gist_id)" $folder/$gist_id &> /dev/null &) + git clone "$(_repo_url $gist_id)" ${folder}/${gist_id} done # shellcheck disable=2181 -- cgit v1.2.3-70-g09d2