diff options
author | typebrook <typebrook@gmail.com> | 2020-03-14 23:05:08 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-03-14 23:05:08 +0800 |
commit | 04bc794c495ea9158c86c624b2a4118442ff6abe (patch) | |
tree | c385aec7d41574b630d80041f7768c965da73b8f | |
parent | bce8cc6b95b639b0c59f61449fd1aba6b0f8c5ae (diff) |
Refactor behavior about subcommand 'detail'
- Store created/updated time inside index file
- gist detail only shows the content from index file
-rwxr-xr-x | gist | 39 |
1 files changed, 15 insertions, 24 deletions
@@ -259,7 +259,7 @@ _show_list() { | |||
259 | 259 | ||
260 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] | 260 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] |
261 | sed -Ene "/^$prefix/ p" $INDEX \ | 261 | sed -Ene "/^$prefix/ p" $INDEX \ |
262 | | 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 |
263 | [[ $index =~ ^s ]] && local name=$author | 263 | [[ $index =~ ^s ]] && local name=$author |
264 | local repo; repo=$folder/${link##*/} | 264 | local repo; repo=$folder/${link##*/} |
265 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") | 265 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") |
@@ -303,6 +303,8 @@ for gist in raw: | |||
303 | print(len(gist["files"]), end=" ") | 303 | print(len(gist["files"]), end=" ") |
304 | print(gist["comments"], end=" ") | 304 | print(gist["comments"], end=" ") |
305 | print(gist["owner"]["login"], end=" ") | 305 | print(gist["owner"]["login"], end=" ") |
306 | print(gist["created_at"], end=" ") | ||
307 | print(gist["updated_at"], end=" ") | ||
306 | print(gist["description"]) | 308 | print(gist["description"]) |
307 | ' | 309 | ' |
308 | } | 310 | } |
@@ -311,11 +313,11 @@ for gist in raw: | |||
311 | _parse_response() { | 313 | _parse_response() { |
312 | _parse_gists \ | 314 | _parse_gists \ |
313 | | tac | sed -e 's/, /,/g' | nl -s' ' \ | 315 | | tac | sed -e 's/, /,/g' | nl -s' ' \ |
314 | | while read -r index link file_url_array public file_num comment_num author description; do | 316 | | while read -r index link file_url_array public file_num comment_num author created_at updated_at description; do |
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 '-' -) | 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 '-' -) |
316 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p | 318 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p |
317 | [[ -n $1 ]] && local index=$1 | 319 | [[ -n $1 ]] && local index=$1 |
318 | echo "$prefix$index $link $blob_code $file_num $comment_num $author $description" | tr -d '"' | 320 | echo "$prefix$index $link $blob_code $file_num $comment_num $author $created_at $updated_at $description" | tr -d '"' |
319 | done | 321 | done |
320 | } | 322 | } |
321 | 323 | ||
@@ -448,22 +450,6 @@ _clean_repos() { | |||
448 | done | 450 | done |
449 | } | 451 | } |
450 | 452 | ||
451 | # parse JSON from gist detail | ||
452 | _parse_gist() { | ||
453 | _process_json ' | ||
454 | raw = json.load(sys.stdin) | ||
455 | print("site:", raw["html_url"]) | ||
456 | print("description:", raw["description"]) | ||
457 | print("public:", raw["public"]) | ||
458 | print("API:", raw["url"]) | ||
459 | print("created_at:", raw["created_at"]) | ||
460 | print("updated_at:", raw["updated_at"]) | ||
461 | print("files:") | ||
462 | for file in raw["files"].keys(): | ||
463 | print(" ", file) | ||
464 | ' | ||
465 | } | ||
466 | |||
467 | # equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' | 453 | # equal to jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' |
468 | _parse_comment() { | 454 | _parse_comment() { |
469 | _process_json ' | 455 | _process_json ' |
@@ -479,11 +465,16 @@ for comment in raw: | |||
479 | 465 | ||
480 | _show_detail() { | 466 | _show_detail() { |
481 | _gist_id "$1" || return 1 | 467 | _gist_id "$1" || return 1 |
482 | http_method GET "$GITHUB_API/gists/$GIST_ID" \ | 468 | record=$(sed -ne "/^$1 / p" $INDEX) |
483 | | _parse_gist | 469 | echo description: $(echo $record | cut -d' ' -f9-) |
484 | 470 | echo site: https://gist.github.com/$GIST_ID | |
485 | http_method GET "$GITHUB_API/gists/$GIST_ID"/comments \ | 471 | echo API: https://api.github.com/gists/$GIST_ID |
486 | | _parse_comment | 472 | echo created_at: $(echo $record | cut -d' ' -f7) |
473 | echo updated_at: $(echo $record | cut -d' ' -f8) | ||
474 | if [[ -d $folder/$GIST_ID ]]; then | ||
475 | echo files | ||
476 | ls | sed -e 's/^/ /' | ||
477 | fi | ||
487 | } | 478 | } |
488 | 479 | ||
489 | # set filename/description/permission for a new gist | 480 | # set filename/description/permission for a new gist |