diff options
author | typebrook <typebrook@gmail.com> | 2020-04-01 09:32:35 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-04-01 10:45:33 +0800 |
commit | ff7a1fbd558e38e6495145f6a2e9e1479ce8ece0 (patch) | |
tree | 5a0d3e7b837c0f2220b34e0cd2c6dbef0c9f3d11 | |
parent | 1c7b1a1b47dd04d97dc4fad87c4866a0ba2e99aa (diff) |
Improve 'gist edit'need-fix
Make it works without index if working dir is a gist repo
-rwxr-xr-x | gist | 31 |
1 files changed, 19 insertions, 12 deletions
@@ -458,10 +458,10 @@ _repo_url() { | |||
458 | _gist_id() { | 458 | _gist_id() { |
459 | GIST_ID=$(sed -En -e "/^$1 / {s#$1 [^ ]+/([[:alnum:]]+) .+#\1#; p; q}" $INDEX) | 459 | GIST_ID=$(sed -En -e "/^$1 / {s#$1 [^ ]+/([[:alnum:]]+) .+#\1#; p; q}" $INDEX) |
460 | if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then | 460 | if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then |
461 | echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" | 461 | echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" >&2 |
462 | echo | 462 | echo >&2 |
463 | echo -e "Invalid index: \e[33m$1\e[0m" | 463 | echo -e "Invalid index: \e[33m$1\e[0m" >&2 |
464 | echo 'Use the indices blinking instead (like 1 or s1)' | 464 | echo 'Use the indices blinking instead (like 1 or s1)' >&2 |
465 | return 1 | 465 | return 1 |
466 | fi | 466 | fi |
467 | } | 467 | } |
@@ -469,7 +469,8 @@ _gist_id() { | |||
469 | # set gist id either by given index or current directory | 469 | # set gist id either by given index or current directory |
470 | _set_gist_id() { | 470 | _set_gist_id() { |
471 | if [[ -z $1 ]]; then | 471 | if [[ -z $1 ]]; then |
472 | [[ $(dirname $(pwd)) == $folder ]] && GIST_ID=$(basename $(pwd)) || return 1 | 472 | [[ $(dirname $(pwd)) == $folder ]] && GIST_ID=$(basename $(pwd)) && return 0 |
473 | echo "Need to specify index" >&2; return 1 | ||
473 | else | 474 | else |
474 | _gist_id "$1" || return 1 | 475 | _gist_id "$1" || return 1 |
475 | fi | 476 | fi |
@@ -672,22 +673,28 @@ _get_desc() { | |||
672 | 673 | ||
673 | # Update description of a gist | 674 | # Update description of a gist |
674 | _edit_gist() { | 675 | _edit_gist() { |
675 | _gist_id "$1" || return 1 | 676 | _set_gist_id || _set_gist_id $1 || return 1 |
677 | local index=$(sed -En -e "s/^([^ ]+) [^ ]+$GIST_ID .+/\1/p" $INDEX) | ||
678 | if [[ -z $index ]]; then | ||
679 | echo "Invalid gist" >&2 | ||
680 | return 1 | ||
681 | fi | ||
676 | 682 | ||
677 | local prefill="$(_get_desc $1)" | 683 | local desc="$(sed -Ee "s/^$index //" <<<"$@")" |
678 | if [[ -z $2 ]]; then | 684 | local prefill="$(_get_desc $index)" |
685 | if [[ -z $desc ]]; then | ||
679 | read -e -p 'Edit description: ' -i "$prefill" -r DESC < /dev/tty | 686 | read -e -p 'Edit description: ' -i "$prefill" -r DESC < /dev/tty |
680 | else | 687 | else |
681 | DESC="$2" | 688 | DESC="$desc" |
682 | fi | 689 | fi |
683 | 690 | ||
684 | http_data=$(mktemp) | 691 | http_data=$(mktemp) |
685 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" | 692 | echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" |
686 | new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ | 693 | new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ |
687 | | sed -e '1 s/^/[/; $ s/$/]/' \ | 694 | | sed -e '1 s/^/[/; $ s/$/]/' \ |
688 | | _parse_response "${1#[[:alpha:]]}" ) | 695 | | _parse_response "${index#[[:alpha:]]}" ) |
689 | [[ -n $new_record ]] && sed -i'' -E -e "/^$1 / s^.+^$new_record^" $INDEX \ | 696 | [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \ |
690 | && hint=false mark="$1 " _show_list \ | 697 | && hint=false mark="$index " _show_list \ |
691 | || echo 'Fail to modify gist description' | 698 | || echo 'Fail to modify gist description' |
692 | } | 699 | } |
693 | 700 | ||