diff options
| author | typebrook <typebrook@gmail.com> | 2020-03-13 23:11:59 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-03-13 23:12:10 +0800 |
| commit | bce8cc6b95b639b0c59f61449fd1aba6b0f8c5ae (patch) | |
| tree | 14e32b0e20f4f63939b24ee1081e4c957cd70461 | |
| parent | 14e8e168215405c7235964b0d242cb6206b6bf24 (diff) | |
Refactor code for marking star/not-star gist
| -rwxr-xr-x | gist | 20 |
1 files changed, 8 insertions, 12 deletions
| @@ -256,13 +256,11 @@ _show_list() { | |||
| 256 | echo ' gist update' | 256 | echo ' gist update' |
| 257 | return 0 | 257 | return 0 |
| 258 | fi | 258 | fi |
| 259 | local filter='/^ *s/ d; /^$/ d' | ||
| 260 | [[ $mark == 's' ]] && filter='/^ *[^ s]/ d; /^$/ d' | ||
| 261 | 259 | ||
| 262 | sed -e "$filter" $INDEX \ | 260 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] |
| 261 | sed -Ene "/^$prefix/ p" $INDEX \ | ||
| 263 | | while read -r index link blob_code file_num comment_num author description; do | 262 | | while read -r index link blob_code file_num comment_num author description; do |
| 264 | [[ $mark == 's' ]] && local name=$author | 263 | [[ $index =~ ^s ]] && local name=$author |
| 265 | #local repo; repo=$folder/$(echo $link | sed 's#.*/##') | ||
| 266 | local repo; repo=$folder/${link##*/} | 264 | local repo; repo=$folder/${link##*/} |
| 267 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") | 265 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") |
| 268 | [[ -z $extra ]] && extra="$file_num $comment_num" | 266 | [[ -z $extra ]] && extra="$file_num $comment_num" |
| @@ -278,7 +276,7 @@ _show_list() { | |||
| 278 | 276 | ||
| 279 | # TODO support filenames, file contents | 277 | # TODO support filenames, file contents |
| 280 | _grep_content() { | 278 | _grep_content() { |
| 281 | _show_list | grep -i "$1" | 279 | hint=false mark=. _show_list | grep -Ei "^ *[^ ]+ [^ ]+ .*$1.*" |
| 282 | } | 280 | } |
| 283 | 281 | ||
| 284 | # Open Github repository import page | 282 | # Open Github repository import page |
| @@ -315,8 +313,7 @@ _parse_response() { | |||
| 315 | | tac | sed -e 's/, /,/g' | nl -s' ' \ | 313 | | tac | sed -e 's/, /,/g' | nl -s' ' \ |
| 316 | | while read -r index link file_url_array public file_num comment_num author description; do | 314 | | while read -r index link file_url_array public file_num comment_num author description; do |
| 317 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 315 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
| 318 | local prefix=$mark | 316 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p |
| 319 | [[ $public == 'False' ]] && prefix=p | ||
| 320 | [[ -n $1 ]] && local index=$1 | 317 | [[ -n $1 ]] && local index=$1 |
| 321 | echo "$prefix$index $link $blob_code $file_num $comment_num $author $description" | tr -d '"' | 318 | echo "$prefix$index $link $blob_code $file_num $comment_num $author $description" | tr -d '"' |
| 322 | done | 319 | done |
| @@ -329,17 +326,16 @@ _fetch_gists() { | |||
| 329 | echo "fetching $user's gists from $GITHUB_API..." | 326 | echo "fetching $user's gists from $GITHUB_API..." |
| 330 | echo | 327 | echo |
| 331 | local route="users/$user/gists" | 328 | local route="users/$user/gists" |
| 332 | local filter='/^[^s]/ d; /^$/ d' | ||
| 333 | if [[ $1 =~ ^(star|s)$ ]];then | 329 | if [[ $1 =~ ^(star|s)$ ]];then |
| 334 | route='gists/starred' | 330 | route='gists/starred' |
| 335 | local mark="s" | 331 | local mark=s |
| 336 | filter='/^[s]/ d; /^$/ d' | ||
| 337 | fi | 332 | fi |
| 338 | 333 | ||
| 339 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | mark=$mark _parse_response) | 334 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | mark=$mark _parse_response) |
| 340 | [[ -z $result ]] && echo 'Not a single valid gist' && return 0 | 335 | [[ -z $result ]] && echo 'Not a single valid gist' && return 0 |
| 341 | 336 | ||
| 342 | sed -i'' -e "$filter" $INDEX && echo "$result" >> $INDEX | 337 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] |
| 338 | sed -i'' -Ee "/^$prefix/ d" $INDEX && echo "$result" >> $INDEX | ||
| 343 | mark=$mark _show_list | 339 | mark=$mark _show_list |
| 344 | 340 | ||
| 345 | [[ $auto_sync == 'true' ]] && (_sync_repos "$1" > /dev/null 2>&1 &) | 341 | [[ $auto_sync == 'true' ]] && (_sync_repos "$1" > /dev/null 2>&1 &) |