diff options
| author | typebrook <typebrook@gmail.com> | 2020-03-16 22:20:27 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-03-16 22:38:46 +0800 |
| commit | 083e04cc9e4fe0b7cb972da45f65725b72d3260d (patch) | |
| tree | 2bee5928016a0a836216b2efcfbdc8e6dae7b7cc | |
| parent | b8d734b90ab5d76f3286f2e40a27d78f5b426334 (diff) | |
Reduce repetitive code for index file format
| -rwxr-xr-x | gist | 18 |
1 files changed, 10 insertions, 8 deletions
| @@ -48,6 +48,8 @@ configuredClient="" | |||
| 48 | GITHUB_API=https://api.github.com | 48 | GITHUB_API=https://api.github.com |
| 49 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config | 49 | CONFIG=~/.config/gist.conf; mkdir -p ~/.config |
| 50 | 50 | ||
| 51 | INDEX_FORMAT=('index' 'url' 'blob_code' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') | ||
| 52 | |||
| 51 | [[ -z $hint ]] && hint=true # default to show hint with list of gist | 53 | [[ -z $hint ]] && hint=true # default to show hint with list of gist |
| 52 | [[ -z $confirm ]] && confirm=true # default to confirm when deleting gists | 54 | [[ -z $confirm ]] && confirm=true # default to confirm when deleting gists |
| 53 | auto_sync=true # automatically clone the gist repo | 55 | auto_sync=true # automatically clone the gist repo |
| @@ -265,14 +267,14 @@ _show_list() { | |||
| 265 | 267 | ||
| 266 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] | 268 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] |
| 267 | sed -Ene "/^$prefix/ p" $INDEX \ | 269 | sed -Ene "/^$prefix/ p" $INDEX \ |
| 268 | | while read -r index link blob_code file_num comment_num author _ _ description; do | 270 | | while read -r ${INDEX_FORMAT[@]}; do |
| 269 | [[ $index =~ ^s ]] && local name=$author | 271 | [[ $index =~ ^s ]] && local name=$author |
| 270 | local repo; repo=$folder/${link##*/} | 272 | local repo; repo=$folder/${url##*/} |
| 271 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") | 273 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") |
| 272 | [[ -z $extra ]] && extra="$file_num $comment_num" | 274 | [[ -z $extra ]] && extra="$file_num $comment_num" |
| 273 | description=$(echo $description | sed -E -e 's/\[(.+)\]/\\e[33m[\1]\\e[0m/') | 275 | description=$(echo $description | sed -E -e 's/\[(.+)\]/\\e[33m[\1]\\e[0m/') |
| 274 | 276 | ||
| 275 | echo -e "$(printf "% 3s" "$index") $link $name $extra $description" \ | 277 | echo -e "$(printf "% 3s" "$index") $url $name $extra $description" \ |
| 276 | | cut -c -"$(tput cols)" | 278 | | cut -c -"$(tput cols)" |
| 277 | done | 279 | done |
| 278 | 280 | ||
| @@ -337,11 +339,11 @@ for gist in raw: | |||
| 337 | _parse_response() { | 339 | _parse_response() { |
| 338 | _parse_gists \ | 340 | _parse_gists \ |
| 339 | | tac | sed -e 's/, /,/g' | nl -s' ' \ | 341 | | tac | sed -e 's/, /,/g' | nl -s' ' \ |
| 340 | | while read -r index link file_url_array public file_num comment_num author created_at updated_at description; do | 342 | | while read -r ${INDEX_FORMAT[@]:0:2} file_url_array public ${INDEX_FORMAT[@]:3:6}; do |
| 341 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 343 | local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
| 342 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p | 344 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p |
| 343 | [[ -n $1 ]] && local index=$1 | 345 | [[ -n $1 ]] && local index=${prefix}${1} || local index=${prefix}${index} |
| 344 | echo "$prefix$index $link $blob_code $file_num $comment_num $author $created_at $updated_at $description" | tr -d '"' | 346 | eval "echo $(sed -Ee 's/([^ ]+)/$\1/g' <<<${INDEX_FORMAT[@]})" |
| 345 | done | 347 | done |
| 346 | } | 348 | } |
| 347 | 349 | ||
| @@ -377,8 +379,8 @@ _query_user() { | |||
| 377 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 | 379 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 |
| 378 | 380 | ||
| 379 | echo "$result" \ | 381 | echo "$result" \ |
| 380 | | while read -r index link blob_code file_num comment_num author _ _ description; do | 382 | | while read -r ${INDEX_FORMAT[@]}; do |
| 381 | echo "$link $author $file_num $comment_num $description" | cut -c -"$(tput cols)" | 383 | echo "$url $author $file_num $comment_num $description" | cut -c -"$(tput cols)" |
| 382 | done | 384 | done |
| 383 | } | 385 | } |
| 384 | 386 | ||