From 781505df9325fc4902cc3f399f4844a48e02d5bd Mon Sep 17 00:00:00 2001 From: typebrook Date: Thu, 6 Feb 2020 16:11:10 +0800 Subject: update --- gist | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/gist b/gist index f7933e1..2ac7fd7 100755 --- a/gist +++ b/gist @@ -18,20 +18,20 @@ # edit, e edit a gist description # delete, D ... delete a gist # clean, C clean removed gists in local -# config, c [token | user | folder | auto-sync | EDITOR | action [value] ] do configuration +# config, c [token | user | folder | auto_sync | EDITOR | action [value] ] do configuration # user, U get gists from a given Github user +# grep, g grep gists by a given pattern +# push, p push changes by git # help, h show this help message -# +# # Example: # gist (Show your gists) # gist 3 (show the repo path of your 3rd gist, and do custom actions) -# +# # Since now a gist is a local cloned repo # It is your business to do git commit and git push -# TODO grep mode for description, file content -# TODO push github.com (may need new token) -# TODO unit test +# TODO import to github repo (may need a new token) # TODO test on bats, mac and remote machine # TODO completion @@ -55,7 +55,7 @@ _configure() { [[ ${#2} -ne 40 ]] && echo 'Invalid token format, it is not 40 chars' >&2 \ && return 1 elif [[ $1 == 'auto_sync' ]]; then - [[ ! $2 =~ ^(true|false)$ ]] && return 1 + [[ ! $2 =~ ^(true|false)$ ]] && return 1 fi target=$1=$2 @@ -84,7 +84,6 @@ _ask_token() { while [[ ! $token =~ ^[[:alnum:]]{40}$ ]]; do [[ -n $token ]] && echo "Not a valid token" - trap 'echo; return 1' INT read -p "Paste your token here (Ctrl-C to skip): " token < /dev/tty done _configure token $token @@ -118,7 +117,7 @@ _apply_config() { AUTH_HEADER="Authorization: token $token" [[ -z "$action" ]] && action="${EDITOR:-vi} ." [[ -z "$folder" ]] && folder=~/gist && mkdir -p $folder - INDEX=$folder/index + INDEX=$folder/index; [[ -e $INDEX ]] || touch $INDEX } _apply_config "$@" || exit 1 @@ -157,6 +156,7 @@ http_method() { esac } +# TODO Split into 2 funcs for filter and display # Show the list of gist, but not updated time _show_list() { if [[ ! -e $INDEX ]]; then @@ -164,8 +164,8 @@ _show_list() { echo ' gist update' return 0 fi - local filter='/^ *s/ d; /^$/ d' - [[ $1 == "s" ]] && filter='/^ *[^ s]/ d; /^$/ d' + [[ -z $1 ]] && local filter='/^ *s/ d; /^$/ d' + [[ $1 == "s" ]] && local filter='/^ *[^ s]/ d; /^$/ d' while read index link blob_code file_num extra author description; do [[ $1 == "s" ]] && local name=$author @@ -182,7 +182,18 @@ _show_list() { echo -e "$(printf "% 3s" $index)" $link $name $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) done < $INDEX \ | sed "$filter" - echo -e '\nrun "gist help" for more details' > /dev/tty + echo -e '\nrun "gist help" or "gist h" for more details' > /dev/tty +} + +# TODO support filenames, file contents +_grep_content() { + _show_list other | grep -i $1 +} + +_push_to_remote() { + _gist_id $1 + cd $folder/$GIST_ID && git add . \ + && git commit --allow-empty-message -m '' && git push origin master } # parse JSON from STDIN with string of commands @@ -192,7 +203,6 @@ AccessJsonElement() { return "$?" } -# equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' _handle_gists() { echo ' raw = json.load(sys.stdin) @@ -207,7 +217,7 @@ for gist in raw: ' } -# TODO check if a user create a very first gist +# TODO check if a user has no gist # parse response from gists require _parse_response() { AccessJsonElement "$(_handle_gists)" \ @@ -240,7 +250,7 @@ _update() { sed -i "$filter" $INDEX && echo "$result" >> $INDEX _show_list $mark - if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi + if [[ $auto_sync != 'false' ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi } _query_user() { @@ -306,6 +316,10 @@ _goto_gist() { } _delete_gist() { + read -r -p "Delete gists above? [y/N] " response + response=${response,,} + [[ ! "$response" =~ ^(yes|y)$ ]] && return 0 + for i in "$@"; do _gist_id "$i" http_method DELETE $GITHUB_API/gists/$GIST_ID \ @@ -352,7 +366,6 @@ for comment in raw: ' } -# TODO format with simple text _show_detail() { _gist_id $1 http_method GET $GITHUB_API/gists/$GIST_ID \ @@ -441,7 +454,7 @@ _edit_gist() { read DESC < /dev/tty http_data=$(mktemp) - echo "{ \"description\": \"$DESC\" }" > $http_data + echo { \"description\": \"$(echo $DESC | sed 's/"/\\"/g')\" } > $http_data http_method PATCH $http_data $GITHUB_API/gists/$GIST_ID > /dev/null \ && _update } @@ -480,6 +493,12 @@ case "$1" in user | U) shift _query_user "$@" ;; + grep | g) + shift + _grep_content "$@" ;; + push | p) + shift + _push_to_remote "$1" ;; help | h) usage ;; *) -- cgit v1.2.3-70-g09d2