From 47697ba48a36e43306fc5a5557d2f546c846ac7b Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 17 May 2020 14:29:09 +0800 Subject: Refactor method of parsing gists - Do not set indices in _parse_response() unless 'index' is set - Change INDEX_FORMAT --- gist | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/gist b/gist index 01c72c7..a3b54ed 100755 --- a/gist +++ b/gist @@ -60,7 +60,7 @@ GITHUB_API=https://api.github.com CONFIG=~/.config/gist.conf; mkdir -p ~/.config per_page=100 -INDEX_FORMAT=('index' 'url' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') +INDEX_FORMAT=('index' 'public' 'url' '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) @@ -360,8 +360,8 @@ _parse_gists() { _process_json ' raw = json.load(sys.stdin) for gist in raw: - print(gist["html_url"], end=" ") print(gist["public"], end=" ") + print(gist["html_url"], end=" ") print(",".join(file["raw_url"] for file in gist["files"].values()), end=" ") print(",".join(file["filename"].replace(" ", "-") + "@" + str(file["language"]).replace(" ", "-") for file in gist["files"].values()), end=" ") print(len(gist["files"]), end=" ") @@ -375,13 +375,8 @@ for gist in raw: # Parse response from 'gist fetch' to the format for index file _parse_response() { - local num=$start_from_num _parse_gists \ - | nl -s' ' \ - | while read -r "${INDEX_FORMAT[@]:0:2}" public file_url_array "${INDEX_FORMAT[@]:4:7}"; do - local private_prefix=''; [[ $public == 'False' ]] && private_prefix=p - [[ -n $num ]] && local index=${num} && num=$(($num+1)); index=${private_prefix}${prefix}${index} - + | while read -r "${INDEX_FORMAT[@]:1:2}" file_url_array "${INDEX_FORMAT[@]:5:7}"; do 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} @@ -397,15 +392,13 @@ _parse_response() { # Get latest list of gists from Github API _fetch_gists() { local route="users/$user/gists" - local prifix='' if [[ $mark == s ]]; then route='gists/starred' - prefix=s fi # set global variable HEADER in http_method, so prevent using pipe HEADER=$(tmp_file HEADER) - HEADER=$HEADER http_method GET $GITHUB_API/$route${1} | start_from_num=$2 prefix=$prefix _parse_response + HEADER=$HEADER http_method GET $GITHUB_API/$route${1} | _parse_response } # consider if HEADER is not exist @@ -422,13 +415,17 @@ _fetch_gists_with_pagnation() { [[ -z $next_page ]] && break printf "%-4s gists fetched\n" $(( ($next_page -1) * $per_page )) - _fetch_gists "?per_page=$per_page&page=$next_page" $(( $(wc -l <$fetched_records) +1 )) >> $fetched_records + _fetch_gists "?per_page=$per_page&page=$next_page" >> $fetched_records done || return 1 [[ ! -s $fetched_records ]] && echo 'Not a single valid gist' && return 0 sed -i'' -Ee "/^$mark/ d" $INDEX - tac <$fetched_records | nl -s' ' | sed -Ee 's/^ *([^ ]+) ([[:alpha:]]?)[[:digit:]]+/\2\1/' >> $INDEX + tac <$fetched_records | nl -s' ' \ + | while read -r "${INDEX_FORMAT[@]:0:2}" extra; do + local prefix=''; [[ $public == False ]] && prefix=p + echo $prefix$index $public $extra + done >> $INDEX hint=$hint _show_list [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) @@ -439,9 +436,8 @@ _fetch_gists_with_pagnation() { # Get a single JSON object of gist from response, and update index file _update_gist() { - local index=$1 - local record=$(sed -e '1 s/^/[/; $ s/$/]/' | start_from_num="${index#[[:alpha:]]}" _parse_response) - [[ -n $record ]] && sed -i'' -Ee "/^$index / s^.+^$record^" $INDEX + local record="$(sed -e '1 s/^/[/; $ s/$/]/' | index=$1 _parse_response)" + [[ -n $record ]] && sed -i'' -Ee "/^$1 / s^.+^$record^" $INDEX } # Fetch gists for a given user @@ -478,7 +474,7 @@ _pull_if_needed() { # Update local git repos _sync_repos() { comm -1 <(ls -A "$folder" | sort) \ - <(cut -d' ' -f1-2 < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \ + <(while read -r ${INDEX_FORMAT[@]}; do echo $index $url; done < "$INDEX" | sed -ne "/^$mark/ s#.*/##p" | sort) \ | { result=$(cat) @@ -505,7 +501,8 @@ _repo_url() { # Get gist id from index files _gist_id() { - GIST_ID=$(sed -En -e "/^$1 / s#^$1 [^ ]+/([[:alnum:]]+) .+#\1#p" $INDEX | head -1) + read -r ${INDEX_FORMAT[@]} <<<"$(sed -ne "/$1 / p" $INDEX)" + GIST_ID=${url##*/} if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" echo @@ -574,7 +571,7 @@ _delete_gist() { # Remove repos which are not in index file anymore _clean_repos() { comm -23 <(find $folder -maxdepth 1 -type d | sed -e '1d; s#.*/##' | sort) \ - <(cut -d' ' -f2 < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \ + <(while read -r ${INDEX_FORMAT[@]}; do echo $url; done < "$INDEX" | sed -e 's#.*/##' | sort 2> /dev/null ) \ | while read -r dir; do mv $folder/"$dir" /tmp && echo $folder/"$dir" is moved to /tmp done @@ -691,17 +688,20 @@ _create_gist() { _set_gist "$@" || return 1 [[ -z ${files[*]} ]] && files+=($(_new_file "$filename")) [[ -z $description ]] && read -e -r -p 'Type description: ' description < /dev/tty + local index=$([[ $public == False ]] && echo p)$(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) echo 'Creating a new gist...' http_data=$(tmp_file PATLOAD.CREATE) echo -e "${files[*]}\n$description" \ | _gist_body > "$http_data" \ && http_method POST $GITHUB_API/gists \ - | xargs -I{} -0 echo "[{}]" \ - | start_from_num=$(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) _parse_response \ + | sed -e '1 s/^/\[/; $ s/$/\]/' \ + | index=$index _parse_response \ | tee -a $INDEX \ - | cut -d' ' -f2 | sed -E -e 's#.*/##' \ - | (xargs -I{} git clone "$(_repo_url {})" $folder/{} &> /dev/null &) + | while read -r "${INDEX_FORMAT[@]}"; do + local gist_id=${url/##*/} + (git clone "$(_repo_url $gist_id)" $folder/$gist_id &> /dev/null &) + done # shellcheck disable=2181 if [[ $? -eq 0 ]]; then -- cgit v1.2.3-70-g09d2