diff options
author | typebrook <typebrook@gmail.com> | 2020-04-15 15:55:25 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-04-15 18:25:01 +0800 |
commit | ce47133db50258eacc574a07fdf87070231d4c32 (patch) | |
tree | 10c7cd542b8f58d0db4a23df9fc1556a25236e16 | |
parent | ea39f1779891c868eceb8d3c8e3fa310f07f128c (diff) |
Fix/Refactor the way to process description
-rwxr-xr-x | gist | 43 |
1 files changed, 22 insertions, 21 deletions
@@ -248,8 +248,8 @@ _apply_config() { | |||
248 | } | 248 | } |
249 | 249 | ||
250 | # extract trailing hashtags from description | 250 | # extract trailing hashtags from description |
251 | _hashtags() { | 251 | _trailing_hashtags() { |
252 | grep -Eo " #[$TAG_CHAR #]+$" <<<"$1" | sed -Ee "s/.* [$TAG_CHAR]+//g" | xargs | 252 | grep -Eo " #[$TAG_CHAR #]+$" <<<"$1" | sed -Ee "s/.* [$TAG_CHAR]+//g" |
253 | } | 253 | } |
254 | 254 | ||
255 | _color_pinned_tags() { | 255 | _color_pinned_tags() { |
@@ -258,6 +258,10 @@ _color_pinned_tags() { | |||
258 | sed -E -e "s/$pattern/\\\e[33m\1\\\e[0m/g" <<<"$1 " | 258 | sed -E -e "s/$pattern/\\\e[33m\1\\\e[0m/g" <<<"$1 " |
259 | } | 259 | } |
260 | 260 | ||
261 | _color_description_title() { | ||
262 | sed -E -e 's/^\[(.+)\]/\\e[33m[\1]\\e[0m/' <<<"$1" | ||
263 | } | ||
264 | |||
261 | # Return git status of a given repo | 265 | # Return git status of a given repo |
262 | _check_repo_status() { | 266 | _check_repo_status() { |
263 | if [[ ! -d $1 ]]; then | 267 | if [[ ! -d $1 ]]; then |
@@ -317,7 +321,7 @@ _show_list() { | |||
317 | 321 | ||
318 | [[ $index =~ ^s ]] && description="$(printf "%-12s" [${author}]) ${description}" | 322 | [[ $index =~ ^s ]] && description="$(printf "%-12s" [${author}]) ${description}" |
319 | 323 | ||
320 | raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra $description" | 324 | raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra $(_color_description_title "$description")" |
321 | [[ -n $pin ]] && raw_output="$(_color_pinned_tags "$raw_output")" | 325 | [[ -n $pin ]] && raw_output="$(_color_pinned_tags "$raw_output")" |
322 | decorator=$(( $(grep -o '\\e\[0m' <<<"$raw_output" | wc -l) *9 )) | 326 | decorator=$(( $(grep -o '\\e\[0m' <<<"$raw_output" | wc -l) *9 )) |
323 | echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) | 327 | echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) |
@@ -389,9 +393,10 @@ _parse_response() { | |||
389 | local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) | 393 | local blob_code=$(echo "$file_url_array" | tr ',' '\n' | sed -E -e 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -s -d '-' -) |
390 | file_array=${file_array//@None/@Text} | 394 | file_array=${file_array//@None/@Text} |
391 | 395 | ||
392 | local hashtags="$(_hashtags "$description")" | 396 | local hashtags_suffix="$(_trailing_hashtags "$description")" |
393 | local tags_string=${hashtags// /,}; [[ -z $tags_string ]] && tags_string=',' | 397 | description="${description%"$hashtags_suffix"}" |
394 | description="$(sed -E -e 's/^\[(.+)\]/\\e[33m[\1]\\e[0m/' <<<"${description%$hashtags}")" | 398 | local hashtags="$(echo $hashtags_suffix | xargs)" |
399 | local tags_string="${hashtags// /,}"; [[ -z $tags_string ]] && tags_string=',' | ||
395 | 400 | ||
396 | eval echo "${INDEX_FORMAT[@]/#/$}" | 401 | eval echo "${INDEX_FORMAT[@]/#/$}" |
397 | done | 402 | done |
@@ -567,14 +572,14 @@ _show_detail() { | |||
567 | 572 | ||
568 | sed -En -e "/[^ ]+ [^ ]+$GIST_ID / p" $INDEX \ | 573 | sed -En -e "/[^ ]+ [^ ]+$GIST_ID / p" $INDEX \ |
569 | | while read -r "${INDEX_FORMAT[@]}"; do | 574 | | while read -r "${INDEX_FORMAT[@]}"; do |
570 | echo -e desc: $description | 575 | echo -e desc: $(_color_description_title "$description") |
571 | echo -e tags: ${tags_string//,/ } | 576 | echo -e tags: ${tags_string//,/ } |
572 | echo -e site: https://gist.github.com/$GIST_ID | 577 | echo -e site: https://gist.github.com/$GIST_ID |
573 | echo -e API : https://api.github.com/gists/$GIST_ID | 578 | echo -e API : https://api.github.com/gists/$GIST_ID |
574 | echo -e created_at: $created_at | 579 | echo -e created_at: $created_at |
575 | echo -e updated_at: $updated_at | 580 | echo -e updated_at: $updated_at |
576 | echo -e files: | 581 | echo -e files: |
577 | echo -e $file_array | tr ',' '\n' | tr '@' ' ' \ | 582 | echo $file_array | tr ',' '\n' | tr '@' ' ' \ |
578 | | column -t | sed -e 's/^/ /' | 583 | | column -t | sed -e 's/^/ /' |
579 | done | 584 | done |
580 | } | 585 | } |
@@ -680,19 +685,16 @@ _create_gist() { | |||
680 | fi | 685 | fi |
681 | } | 686 | } |
682 | 687 | ||
683 | # get the gist description with a given index (color is removed) | ||
684 | _get_desc() { | ||
685 | sed -ne "/^$1 / p" $INDEX | cut -d' ' -f${#INDEX_FORMAT[@]}- | sed 's/\\e\[[0-9;]\+[A-Za-z]//g' | ||
686 | } | ||
687 | |||
688 | # Update description of a gist | 688 | # Update description of a gist |
689 | _edit_gist() { | 689 | _edit_gist() { |
690 | _gist_id "$1" || return 1 | 690 | _gist_id "$1" || return 1 |
691 | checkInternet || exit 1 | 691 | checkInternet || exit 1 |
692 | 692 | ||
693 | local prefill="$(_get_desc $1)" | ||
694 | if [[ -z $2 ]]; then | 693 | if [[ -z $2 ]]; then |
695 | read -e -p 'Edit description: ' -i "$prefill" -r DESC < /dev/tty | 694 | read -r "${INDEX_FORMAT[@]}" <<<"$(sed -ne "/^$1 / p" $INDEX)" |
695 | read -e -p 'Edit description: ' -i "$description" -r DESC < /dev/tty | ||
696 | tags=( ${tags_string//,/ } ) | ||
697 | DESC="$DESC ${tags[*]}" | ||
696 | else | 698 | else |
697 | DESC="$2" | 699 | DESC="$2" |
698 | fi | 700 | fi |
@@ -733,13 +735,12 @@ _tag_gist() { | |||
733 | display=tag mark=${INPUT:+.} _show_list | 735 | display=tag mark=${INPUT:+.} _show_list |
734 | # if user want to change tags of a gist | 736 | # if user want to change tags of a gist |
735 | elif _gist_id $1 &>/dev/null; then | 737 | elif _gist_id $1 &>/dev/null; then |
736 | detail="$(_show_detail $1 | sed 3,6d | tee /dev/tty)"; echo | 738 | _show_detail $1 | sed 3,6d && echo |
737 | local desc="$(echo "$detail" | sed -ne 1p | cut -d' ' -f2-)" | 739 | read -r "${INDEX_FORMAT[@]}" <<<"$(sed -ne "/^$1 / p" $INDEX)" |
738 | local tags="$(echo "$detail" | sed -ne '2 s/#//gp' | cut -d' ' -f2-)" | 740 | local tags="$(sed -e 's/,//g; s/#/ /g; s/^ //g' <<<"$tags_string")" |
739 | read -e -p 'Edit tags: ' -i "$tags" -r -a new_tags < /dev/tty | 741 | read -e -p 'Edit tags: ' -i "$tags" -r -a new_tags < /dev/tty |
740 | local new_hashtags="${new_tags[@]/#/#}" | 742 | local hashtags="${new_tags[@]/#/#}" |
741 | local new_desc=$(sed "s/$hashtags$//; s/ *$/ /" <<<"$desc")${new_hashtags} | 743 | (_edit_gist $1 "${description}${hashtags:+ }${hashtags}" &>/dev/null &) |
742 | (_edit_gist $1 "$new_desc" &>/dev/null &) | ||
743 | # if user want to filter gists with given tags | 744 | # if user want to filter gists with given tags |
744 | else | 745 | else |
745 | local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" | 746 | local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" |