From ff7a1fbd558e38e6495145f6a2e9e1479ce8ece0 Mon Sep 17 00:00:00 2001 From: typebrook Date: Wed, 1 Apr 2020 09:32:35 +0800 Subject: Improve 'gist edit' Make it works without index if working dir is a gist repo --- gist | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/gist b/gist index d4071da..2064815 100755 --- a/gist +++ b/gist @@ -458,10 +458,10 @@ _repo_url() { _gist_id() { GIST_ID=$(sed -En -e "/^$1 / {s#$1 [^ ]+/([[:alnum:]]+) .+#\1#; p; q}" $INDEX) if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then - echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" - echo - echo -e "Invalid index: \e[33m$1\e[0m" - echo 'Use the indices blinking instead (like 1 or s1)' + echo -e "$(hint=false _show_list | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" >&2 + echo >&2 + echo -e "Invalid index: \e[33m$1\e[0m" >&2 + echo 'Use the indices blinking instead (like 1 or s1)' >&2 return 1 fi } @@ -469,7 +469,8 @@ _gist_id() { # set gist id either by given index or current directory _set_gist_id() { if [[ -z $1 ]]; then - [[ $(dirname $(pwd)) == $folder ]] && GIST_ID=$(basename $(pwd)) || return 1 + [[ $(dirname $(pwd)) == $folder ]] && GIST_ID=$(basename $(pwd)) && return 0 + echo "Need to specify index" >&2; return 1 else _gist_id "$1" || return 1 fi @@ -672,22 +673,28 @@ _get_desc() { # Update description of a gist _edit_gist() { - _gist_id "$1" || return 1 + _set_gist_id || _set_gist_id $1 || return 1 + local index=$(sed -En -e "s/^([^ ]+) [^ ]+$GIST_ID .+/\1/p" $INDEX) + if [[ -z $index ]]; then + echo "Invalid gist" >&2 + return 1 + fi - local prefill="$(_get_desc $1)" - if [[ -z $2 ]]; then + local desc="$(sed -Ee "s/^$index //" <<<"$@")" + local prefill="$(_get_desc $index)" + if [[ -z $desc ]]; then read -e -p 'Edit description: ' -i "$prefill" -r DESC < /dev/tty else - DESC="$2" + DESC="$desc" fi http_data=$(mktemp) echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" new_record=$(http_method PATCH "$GITHUB_API/gists/$GIST_ID" \ | sed -e '1 s/^/[/; $ s/$/]/' \ - | _parse_response "${1#[[:alpha:]]}" ) - [[ -n $new_record ]] && sed -i'' -E -e "/^$1 / s^.+^$new_record^" $INDEX \ - && hint=false mark="$1 " _show_list \ + | _parse_response "${index#[[:alpha:]]}" ) + [[ -n $new_record ]] && sed -i'' -E -e "/^$index / s^.+^$new_record^" $INDEX \ + && hint=false mark="$index " _show_list \ || echo 'Fail to modify gist description' } -- cgit v1.2.3-70-g09d2