aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xscripts/gist28
1 files changed, 15 insertions, 13 deletions
diff --git a/scripts/gist b/scripts/gist
index f33f83f..58c949f 100755
--- a/scripts/gist
+++ b/scripts/gist
@@ -63,8 +63,7 @@ set -eo pipefail
63[ "$TRACE" ] && set -x 63[ "$TRACE" ] && set -x
64 64
65GITHUB_API=https://api.github.com 65GITHUB_API=https://api.github.com
66CONFIG=~/.config/gistrc 66CONFIG=~/.config/gist.conf; mkdir -p ~/.config
67mkdir -p ~/.config && umask 0077
68configuredClient="" 67configuredClient=""
69 68
70_config_cases() { 69_config_cases() {
@@ -86,7 +85,7 @@ _configure() {
86 [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0 85 [[ -z "$@" ]] && (${EDITOR:-vi} $CONFIG) && return 0
87 target=$(_config_cases "$@") 86 target=$(_config_cases "$@")
88 87
89 touch $CONFIG 88 umask 0077 && touch $CONFIG
90 [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $CONFIG && echo $target >> $CONFIG 89 [[ "$target" =~ [^=]$ ]] && sed -i "/^$1=/ d" $CONFIG && echo $target >> $CONFIG
91 cat $CONFIG 90 cat $CONFIG
92} 91}
@@ -114,10 +113,11 @@ _ask_token() {
114} 113}
115 114
116_apply_config() { 115_apply_config() {
117 source $CONFIG 116 source $CONFIG 2> /dev/null || true
118 if [[ ! -e $CONFIG ]] || [[ -z $user ]]; then 117 if [[ ! -e $CONFIG ]] || [[ -z $user ]]; then
119 echo -n 'Hi fellow! To access your gists, I need your Github username, ' 118 echo 'Hi fellow! To access your gists, I need your Github username'
120 echo -n "also a personal token with scope which allows "gist"!'" 119 echo "Also a personal token with scope which allows "gist"!'"
120 echo
121 _ask_username 121 _ask_username
122 _ask_token 122 _ask_token
123 elif [[ -z $token ]] && [[ $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then 123 elif [[ -z $token ]] && [[ $1 =~ ^(n|new|e|edit|D|delete)$ ]]; then
@@ -174,7 +174,7 @@ _show_list() {
174 return 0 174 return 0
175 fi 175 fi
176 cat $1 \ 176 cat $1 \
177 | while read line_num link file_url_array file_num extra description; do 177 | while read index link blob_code file_num extra description; do
178 local repo=$folder/$(echo $link | sed 's#.*/##') 178 local repo=$folder/$(echo $link | sed 's#.*/##')
179 local occupy=0 179 local occupy=0
180 180
@@ -185,7 +185,7 @@ _show_list() {
185 # if there is a commit not yet push, show red message "ahead" 185 # if there is a commit not yet push, show red message "ahead"
186 [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7 186 [[ -n $(cd $repo && git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" && occupy=7
187 187
188 echo -e $line_num $link $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) ) 188 echo -e $index $link $file_num $extra $(echo $description | cut -c -$(( 60 -$occupy -1 )) )
189 done 189 done
190} 190}
191 191
@@ -202,6 +202,7 @@ _handle_gists() {
202for gist in raw: 202for gist in raw:
203 print(gist["html_url"], end=" ") 203 print(gist["html_url"], end=" ")
204 print([file["raw_url"] for file in gist["files"].values()], end=" ") 204 print([file["raw_url"] for file in gist["files"].values()], end=" ")
205 print(gist["public"], end=" ")
205 print(len(gist["files"]), end=" ") 206 print(len(gist["files"]), end=" ")
206 print(gist["comments"], end=" ") 207 print(gist["comments"], end=" ")
207 print(gist["description"]) 208 print(gist["description"])
@@ -211,10 +212,11 @@ for gist in raw:
211# TODO check if a user create a very first gist 212# TODO check if a user create a very first gist
212_parse_response() { 213_parse_response() {
213 AccessJsonElement "$(_handle_gists)" \ 214 AccessJsonElement "$(_handle_gists)" \
214 | tac | sed 's/, /,/g'\ 215 | tac | sed 's/, /,/g' | nl -s' ' \
215 | while read link file_url_array file_num comment_num description; do 216 | while read index link file_url_array public file_num comment_num description; do
216 local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') 217 local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-')
217 echo $link $blob_code $file_num $comment_num $description | tr -d '"' 218 [[ $public == 'False' ]] && mark=p
219 echo $mark$index $link $blob_code $file_num $comment_num $description | tr -d '"'
218 done 220 done
219} 221}
220 222
@@ -227,8 +229,8 @@ _update() {
227 local mark="" 229 local mark=""
228 [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" && mark="s" 230 [[ "$1" =~ ^(star|s)$ ]] && list_file=$STARRED && route="gists/starred" && mark="s"
229 231
230 httpGet $GITHUB_API/$route \ 232 curl -H "$auth_header" $GITHUB_API/$route \
231 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file \ 233 | _parse_response | sed -E "s/^ */$mark/" > $list_file \
232 && _show_list $list_file \ 234 && _show_list $list_file \
233 || echo Fail to update gists 235 || echo Fail to update gists
234 236