From 083e04cc9e4fe0b7cb972da45f65725b72d3260d Mon Sep 17 00:00:00 2001 From: typebrook Date: Mon, 16 Mar 2020 22:20:27 +0800 Subject: Reduce repetitive code for index file format --- gist | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/gist b/gist index 432ae62..7725f51 100755 --- a/gist +++ b/gist @@ -48,6 +48,8 @@ configuredClient="" GITHUB_API=https://api.github.com CONFIG=~/.config/gist.conf; mkdir -p ~/.config +INDEX_FORMAT=('index' 'url' 'blob_code' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') + [[ -z $hint ]] && hint=true # default to show hint with list of gist [[ -z $confirm ]] && confirm=true # default to confirm when deleting gists auto_sync=true # automatically clone the gist repo @@ -265,14 +267,14 @@ _show_list() { local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] sed -Ene "/^$prefix/ p" $INDEX \ - | while read -r index link blob_code file_num comment_num author _ _ description; do + | while read -r ${INDEX_FORMAT[@]}; do [[ $index =~ ^s ]] && local name=$author - local repo; repo=$folder/${link##*/} + local repo; repo=$folder/${url##*/} local extra; extra=$(_check_repo_status "$repo" "$blob_code") [[ -z $extra ]] && extra="$file_num $comment_num" description=$(echo $description | sed -E -e 's/\[(.+)\]/\\e[33m[\1]\\e[0m/') - echo -e "$(printf "% 3s" "$index") $link $name $extra $description" \ + echo -e "$(printf "% 3s" "$index") $url $name $extra $description" \ | cut -c -"$(tput cols)" done @@ -337,11 +339,11 @@ for gist in raw: _parse_response() { _parse_gists \ | tac | sed -e 's/, /,/g' | nl -s' ' \ - | while read -r index link file_url_array public file_num comment_num author created_at updated_at description; do + | while read -r ${INDEX_FORMAT[@]:0:2} file_url_array public ${INDEX_FORMAT[@]:3:6}; do local blob_code; blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) local prefix=$mark; [[ $public == 'False' ]] && prefix=p - [[ -n $1 ]] && local index=$1 - echo "$prefix$index $link $blob_code $file_num $comment_num $author $created_at $updated_at $description" | tr -d '"' + [[ -n $1 ]] && local index=${prefix}${1} || local index=${prefix}${index} + eval "echo $(sed -Ee 's/([^ ]+)/$\1/g' <<<${INDEX_FORMAT[@]})" done } @@ -377,8 +379,8 @@ _query_user() { [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 echo "$result" \ - | while read -r index link blob_code file_num comment_num author _ _ description; do - echo "$link $author $file_num $comment_num $description" | cut -c -"$(tput cols)" + | while read -r ${INDEX_FORMAT[@]}; do + echo "$url $author $file_num $comment_num $description" | cut -c -"$(tput cols)" done } -- cgit v1.2.3-70-g09d2