diff options
author | typebrook <typebrook@gmail.com> | 2020-05-12 14:03:45 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-05-12 14:07:52 +0800 |
commit | bbd80485fef5c214288c6e591ccbc368d04b9726 (patch) | |
tree | 40eb0623efe09cfaf996e5b724ebaa6701d8f886 | |
parent | e7eac31097b0c0747e5fed233c9deda25a1cfabc (diff) |
Improve sub-command 'tag', 'edit' and 'new'
1. By default, do not shows tags for starred gist
2. Tags user typed allow any number of leading '#'
3. Fix the parameter of 'gist edit', it takes arguemnts behind $1 as
description
4. An easy to read way to wrap JSON body
-rwxr-xr-x | gist | 25 |
1 files changed, 13 insertions, 12 deletions
@@ -700,7 +700,7 @@ _create_gist() { | |||
700 | echo -e "${files[*]}\n$description" \ | 700 | echo -e "${files[*]}\n$description" \ |
701 | | _gist_body > "$http_data" \ | 701 | | _gist_body > "$http_data" \ |
702 | && http_method POST $GITHUB_API/gists \ | 702 | && http_method POST $GITHUB_API/gists \ |
703 | | sed -e '1 s/^/[/; $ s/$/]/' \ | 703 | | xargs -I{} -0 echo "[{}]" \ |
704 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ | 704 | | _parse_response $(( $(sed -e '/^s/ d' $INDEX | wc -l) +1 )) \ |
705 | | tee -a $INDEX \ | 705 | | tee -a $INDEX \ |
706 | | cut -d' ' -f2 | sed -E -e 's#.*/##' \ | 706 | | cut -d' ' -f2 | sed -E -e 's#.*/##' \ |
@@ -717,24 +717,25 @@ _create_gist() { | |||
717 | 717 | ||
718 | # Update description of a gist | 718 | # Update description of a gist |
719 | _edit_gist() { | 719 | _edit_gist() { |
720 | _gist_id "$1" || return 1 | 720 | local index=$1; shift |
721 | _gist_id "$index" || return 1 | ||
721 | 722 | ||
722 | if [[ -z $2 ]]; then | 723 | if [[ -z "$@" ]]; then |
723 | read -r "${INDEX_FORMAT[@]}" <<<"$(sed -ne "/^$1 / p" $INDEX)" | 724 | read -r "${INDEX_FORMAT[@]}" <<<"$(sed -ne "/^$index / p" $INDEX)" |
724 | read -e -p 'Edit description: ' -i "$description" -r DESC < /dev/tty | 725 | read -e -p 'Edit description: ' -i "$description" -r DESC < /dev/tty |
725 | tags=( ${tags_string//,/ } ) | 726 | tags=( ${tags_string//,/ } ) |
726 | DESC="$DESC ${tags[*]}" | 727 | DESC="$DESC ${tags[*]}" |
727 | else | 728 | else |
728 | DESC="$2" | 729 | DESC="$@" |
729 | fi | 730 | fi |
730 | 731 | ||
731 | http_data=$(tmp_file) | 732 | http_data=$(tmp_file) |
732 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" | 733 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" |
733 | new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ | 734 | new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ |
734 | | sed -e '1 s/^/[/; $ s/$/]/' \ | 735 | | xargs -I{} -0 echo "[{}]" \ |
735 | | _parse_response "${1#[[:alpha:]]}" ) | 736 | | _parse_response "${index#[[:alpha:]]}" ) |
736 | [[ -n $new_record ]] && sed -i'' -E -e "/^$1 / s^.+^$new_record^" $INDEX \ | 737 | [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \ |
737 | && hint=false mark="$1 " _show_list \ | 738 | && hint=false mark="$index " _show_list \ |
738 | || echo 'Fail to modify gist description' | 739 | || echo 'Fail to modify gist description' |
739 | } | 740 | } |
740 | 741 | ||
@@ -761,15 +762,15 @@ _check_protocol() { | |||
761 | _tag_gist() { | 762 | _tag_gist() { |
762 | # if no tag is given, show gist list with tags | 763 | # if no tag is given, show gist list with tags |
763 | if [[ -z $* ]]; then | 764 | if [[ -z $* ]]; then |
764 | display=tag mark=${INPUT:+.} _show_list | 765 | display=tag _show_list |
765 | # if user want to change tags of a gist | 766 | # if user want to change tags of a gist |
766 | elif _gist_id $1 &>/dev/null; then | 767 | elif _gist_id $1 &>/dev/null; then |
767 | _show_detail $1 | sed 3,6d && echo | 768 | _show_detail $1 | sed 3,6d && echo |
768 | read -r "${INDEX_FORMAT[@]}" <<<"$(sed -ne "/^$1 / p" $INDEX)" | 769 | read -r "${INDEX_FORMAT[@]}" <<<"$(sed -ne "/^$1 / p" $INDEX)" |
769 | local tags="$(sed -e 's/,//g; s/#/ /g; s/^ //g' <<<"$tags_string")" | 770 | local tags="$(sed -e 's/,//g; s/#/ /g; s/^ //g' <<<"$tags_string")" |
770 | read -e -p 'Edit tags: ' -i "$tags" -r -a new_tags < /dev/tty | 771 | read -e -p 'Edit tags: ' -i "$tags" -r -a new_tags < /dev/tty |
771 | local hashtags="${new_tags[@]/#/#}" | 772 | local hashtags=( $(sed -Ee 's/#+/#/g' <<<${new_tags[@]/#/#}) ) |
772 | ($0 edit $1 "${description}${hashtags:+ }${hashtags}" &>/dev/null &) | 773 | ($0 edit $1 "${description}${hashtags:+ }${hashtags[@]}" &>/dev/null &) |
773 | # if user want to filter gists with given tags | 774 | # if user want to filter gists with given tags |
774 | else | 775 | else |
775 | local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" | 776 | local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" |