diff options
| author | typebrook <typebrook@gmail.com> | 2020-03-19 10:54:25 +0800 |
|---|---|---|
| committer | typebrook <typebrook@gmail.com> | 2020-03-19 15:49:18 +0800 |
| commit | 5e80314c21f0d28d32920d803583ee65bf35b97d (patch) | |
| tree | 4a5df7a80e6254e58333c04b96daefc20c26f82e | |
| parent | 3b6445d497b41b07a3fab19c466cfa37381270f7 (diff) | |
Refactor code
| -rwxr-xr-x | gist | 34 |
1 files changed, 16 insertions, 18 deletions
| @@ -52,8 +52,7 @@ CONFIG=~/.config/gist.conf; mkdir -p ~/.config | |||
| 52 | INDEX_FORMAT=('index' 'url' 'blob_code' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') | 52 | INDEX_FORMAT=('index' 'url' 'blob_code' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') |
| 53 | TAG_CHAR='-_[:alnum:]' | 53 | TAG_CHAR='-_[:alnum:]' |
| 54 | 54 | ||
| 55 | [[ -z $hint ]] && hint=true # default to show hint with list of gist | 55 | hint=${hint:=true} # default to show hint with list of gist |
| 56 | [[ -z $confirm ]] && confirm=true # default to confirm when deleting gists | ||
| 57 | auto_sync=true # automatically clone the gist repo | 56 | auto_sync=true # automatically clone the gist repo |
| 58 | protocol=https | 57 | protocol=https |
| 59 | 58 | ||
| @@ -232,7 +231,7 @@ _validate_config(){ | |||
| 232 | _apply_config() { | 231 | _apply_config() { |
| 233 | _validate_config "$@" || return 1 | 232 | _validate_config "$@" || return 1 |
| 234 | 233 | ||
| 235 | [[ -z $folder || ! -w $(dirname $folder) ]] && folder=~/gist | 234 | [[ -z $folder || ! -w $(dirname "$folder") ]] && folder=~/gist |
| 236 | mkdir -p $folder | 235 | mkdir -p $folder |
| 237 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX | 236 | INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX |
| 238 | } | 237 | } |
| @@ -251,19 +250,20 @@ _check_repo_status() { | |||
| 251 | echo "\e[32m[Not cloned yet]\e[0m"; | 250 | echo "\e[32m[Not cloned yet]\e[0m"; |
| 252 | fi | 251 | fi |
| 253 | else | 252 | else |
| 254 | cd "$1" | 253 | cd "$1" || exit 1 |
| 255 | if [[ -n $(git status --short) || $(git branch | sed -n '/\* / s///p') != 'master' ]] &>/dev/null; then | 254 | if [[ -n $(git status --short) || $(git branch | sed -n '/\* / s///p') != 'master' ]] &>/dev/null; then |
| 256 | echo "\e[36m[working]\e[0m" | 255 | echo "\e[36m[working]\e[0m" |
| 257 | else | 256 | else |
| 258 | [[ $(_blob_code "$1") != "$2" ]] 2>/dev/null && local status="\e[31m[outdated]\e[0m" | 257 | [[ $(_blob_code "$1") != "$2" ]] 2>/dev/null && local status="\e[31m[outdated]\e[0m" |
| 259 | [[ -n $(git cherry) ]] 2>/dev/null && local status="\e[31m[ahead]\e[0m" | 258 | [[ -n $(git cherry) ]] 2>/dev/null && local status="\e[31m[ahead]\e[0m" |
| 260 | echo $status | 259 | echo "$status" |
| 261 | fi | 260 | fi |
| 262 | fi | 261 | fi |
| 263 | } | 262 | } |
| 264 | 263 | ||
| 265 | # Display the list of gist, show username for starred gist | 264 | # Display the list of gist, show username for starred gist |
| 266 | # If hint=true, print hint to tty. If mark=<pattern>, filter index with regex | 265 | # If hint=true, print hint to tty. If mark=<pattern>, filter index with regex |
| 266 | # If tag=true, print tags instead or url | ||
| 267 | # TODO color private/starred mark | 267 | # TODO color private/starred mark |
| 268 | _show_list() { | 268 | _show_list() { |
| 269 | if [[ -n $1 ]]; then | 269 | if [[ -n $1 ]]; then |
| @@ -276,20 +276,19 @@ _show_list() { | |||
| 276 | local input=$INDEX | 276 | local input=$INDEX |
| 277 | fi | 277 | fi |
| 278 | 278 | ||
| 279 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] | 279 | sed -Ene "/^${mark:-[^s]}/ p" $input \ |
| 280 | sed -Ene "/^$prefix/ p" $input \ | ||
| 281 | | while read -r "${INDEX_FORMAT[@]}"; do | 280 | | while read -r "${INDEX_FORMAT[@]}"; do |
| 282 | local repo; repo=$folder/${url##*/} | 281 | local repo; repo=$folder/${url##*/} |
| 283 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") | 282 | local extra; extra=$(_check_repo_status "$repo" "$blob_code") |
| 284 | [[ -z $extra ]] && extra="$file_num $comment_num" | 283 | [[ -z $extra ]] && extra="$file_num $comment_num" |
| 285 | [[ $index =~ ^s ]] && extra=$(printf "%-12s" $author)${extra} | 284 | [[ $index =~ ^s ]] && extra=$(printf "%-12s" $author)${extra} |
| 286 | hashtags=$(_hashtags "$description") | 285 | local hashtags=$(_hashtags "$description") |
| 287 | description=$(sed -E -e 's/\[(.+)\]/\\e[33m[\1]\\e[0m/' <<<"$description" | sed "s/ $hashtags$//") | 286 | local description=$(sed -E -e 's/^\[(.+)\]/\\e[33m[\1]\\e[0m/' <<<"$description" | sed "s/ $hashtags$//") |
| 288 | [[ $tag == 'true' ]] && url="$hashtags" && local width=45 | 287 | [[ $tag == 'true' ]] && url="$hashtags" && local width=45 |
| 289 | 288 | ||
| 290 | raw_output="$(printf "% 3s" "$index") $(printf "%-${width:-56}s" "$url") $extra $description" | 289 | raw_output="$(printf "% 3s" "$index") $(printf "%-${width:-56}s" "$url") $extra $description" |
| 291 | decorator=$(( $(grep -o '\\e\[0m' <<<"$raw_output" | wc -l) *9 )) | 290 | decorator=$(( $(grep -o '\\e\[0m' <<<"$raw_output" | wc -l) *9 )) |
| 292 | echo -e "$raw_output" | cut -c -$(( $(tput cols) +$decorator )) | 291 | echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) |
| 293 | done | 292 | done |
| 294 | 293 | ||
| 295 | [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ | 294 | [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ |
| @@ -304,14 +303,14 @@ _grep_content() { | |||
| 304 | if grep --color=always -iq "$1" <<<"$description"; then | 303 | if grep --color=always -iq "$1" <<<"$description"; then |
| 305 | hint=false mark="$index " _show_list | grep --color=always -Ei "$1" | 304 | hint=false mark="$index " _show_list | grep --color=always -Ei "$1" |
| 306 | else | 305 | else |
| 307 | local repo=$folder/$(echo $url | sed -E -e 's#.*/##') | 306 | local repo=$folder/${url##*/} |
| 308 | [[ -d $repo ]] && cd $repo || continue | 307 | [[ -d $repo ]] && cd $repo || continue |
| 309 | local file=$(ls $repo | grep --color=always -Ei "$1") | 308 | local file=$(ls $repo | grep --color=always -Ei "$1") |
| 310 | local content=$(grep --color=always -EHi "$1" * | head -1) | 309 | local content=$(grep --color=always -EHi "$1" * | head -1) |
| 311 | 310 | ||
| 312 | [[ -n $file && file="$file\n" || -n $content ]] \ | 311 | [[ -n $file && file="$file\n" || -n $content ]] \ |
| 313 | && hint=false mark="$index " _show_list \ | 312 | && hint=false mark="$index " _show_list \ |
| 314 | && echo -e "$file$content\n" | sed -e 's/^/ /' | 313 | && echo -e " $file$content\n" |
| 315 | fi | 314 | fi |
| 316 | done < $INDEX | 315 | done < $INDEX |
| 317 | } | 316 | } |
| @@ -341,7 +340,7 @@ _parse_response() { | |||
| 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 '-' -) | 340 | 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 | 341 | local prefix=$mark; [[ $public == 'False' ]] && prefix=p |
| 343 | [[ -n $1 ]] && local index=${1}; index=${prefix}${index} | 342 | [[ -n $1 ]] && local index=${1}; index=${prefix}${index} |
| 344 | eval echo $(sed -Ee 's/([^ ]+)/$\1/g' <<<"${INDEX_FORMAT[@]}") | 343 | eval echo ${INDEX_FORMAT[@]/#/$} |
| 345 | done | 344 | done |
| 346 | } | 345 | } |
| 347 | 346 | ||
| @@ -361,8 +360,7 @@ _fetch_gists() { | |||
| 361 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | mark=$mark _parse_response) | 360 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | mark=$mark _parse_response) |
| 362 | [[ -z $result ]] && echo 'Not a single valid gist' && return 0 | 361 | [[ -z $result ]] && echo 'Not a single valid gist' && return 0 |
| 363 | 362 | ||
| 364 | local prefix=$mark; [[ -z $prefix ]] && prefix=[^s] | 363 | sed -i'' -Ee "/^${mark:-[^s]}/ d" $INDEX && echo "$result" >> $INDEX |
| 365 | sed -i'' -Ee "/^$prefix/ d" $INDEX && echo "$result" >> $INDEX | ||
| 366 | mark=$mark _show_list | 364 | mark=$mark _show_list |
| 367 | 365 | ||
| 368 | [[ $auto_sync == 'true' ]] && (_sync_repos "$1" &> /dev/null &) | 366 | [[ $auto_sync == 'true' ]] && (_sync_repos "$1" &> /dev/null &) |
| @@ -373,7 +371,7 @@ _fetch_gists() { | |||
| 373 | # TODO pagnation for more than 100 gists | 371 | # TODO pagnation for more than 100 gists |
| 374 | _query_user() { | 372 | _query_user() { |
| 375 | local route="users/$1/gists" | 373 | local route="users/$1/gists" |
| 376 | result=$(http_method GET $GITHUB_API/$route?per_page=100 | _parse_response) | 374 | result="$(http_method GET $GITHUB_API/$route?per_page=100 | _parse_response)" |
| 377 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 | 375 | [[ -z $result ]] && echo "Failed to query $1's gists" && return 1 |
| 378 | 376 | ||
| 379 | echo "$result" \ | 377 | echo "$result" \ |
| @@ -662,8 +660,8 @@ _tag_gist() { | |||
| 662 | _show_detail $1 | sed 3,6d && echo | 660 | _show_detail $1 | sed 3,6d && echo |
| 663 | local desc="$(_get_desc $1)" | 661 | local desc="$(_get_desc $1)" |
| 664 | local hashtags=$(_hashtags "$desc") | 662 | local hashtags=$(_hashtags "$desc") |
| 665 | read -e -p 'Edit tags: ' -i "$(tr -d '#' <<<"$hashtags")" -r new_tags < /dev/tty | 663 | read -e -p 'Edit tags: ' -i "${hashtags//'#'/}" -r -a new_tags < /dev/tty |
| 666 | local new_hashtags=$(sed -Ee 's/^/#/; s/ / #/g' <<<"$new_tags") | 664 | local new_hashtags="${new_tags[@]/#/#}" |
| 667 | local new_desc=$(sed "s/$hashtags$//; s/ *$/ /" <<<"$desc")${new_hashtags} | 665 | local new_desc=$(sed "s/$hashtags$//; s/ *$/ /" <<<"$desc")${new_hashtags} |
| 668 | (_edit_gist $1 "$new_desc" &>/dev/null &) | 666 | (_edit_gist $1 "$new_desc" &>/dev/null &) |
| 669 | # if user want to filter gists with given tags | 667 | # if user want to filter gists with given tags |