diff options
Diffstat (limited to 'scripts/gist')
| -rwxr-xr-x | scripts/gist | 86 |
1 files changed, 48 insertions, 38 deletions
diff --git a/scripts/gist b/scripts/gist index c67b6e3..7f8fad0 100755 --- a/scripts/gist +++ b/scripts/gist | |||
| @@ -86,14 +86,15 @@ _show_list() { | |||
| 86 | # if there is a commit not yet push, show red message "ahead" | 86 | # if there is a commit not yet push, show red message "ahead" |
| 87 | [[ -n $(git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" | 87 | [[ -n $(git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" |
| 88 | 88 | ||
| 89 | echo -e $line_num $link $file_num $extra $description | 89 | echo -e $line_num $link $file_num $extra $(echo $description | cut -c -60) |
| 90 | done | 90 | done |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | # get the list of gists | 93 | # get the list of gists |
| 94 | # TODO support secret gist | 94 | # TODO support secret gist |
| 95 | _update() { | 95 | _update() { |
| 96 | echo "fetching from api.github.com..." > /dev/tty | 96 | echo "fetching from api.github.com..." |
| 97 | echo | ||
| 97 | list_file=$index | 98 | list_file=$index |
| 98 | route="users/$user/gists" | 99 | route="users/$user/gists" |
| 99 | mark="" | 100 | mark="" |
| @@ -112,7 +113,7 @@ _parse_response() { | |||
| 112 | tac |\ | 113 | tac |\ |
| 113 | while read link file_url_array file_num comment_num description; do | 114 | while read link file_url_array file_num comment_num description; do |
| 114 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 115 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
| 115 | echo $link $blob_code $file_num $comment_num $(echo $description | cut -c -60) | tr -d '"' | 116 | echo $link $blob_code $file_num $comment_num $description | tr -d '"' |
| 116 | done | 117 | done |
| 117 | } | 118 | } |
| 118 | 119 | ||
| @@ -186,34 +187,54 @@ _show_detail() { | |||
| 186 | } | 187 | } |
| 187 | 188 | ||
| 188 | _new_file() { | 189 | _new_file() { |
| 189 | tmp_file=$(mktemp) | ||
| 190 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty | 190 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty |
| 191 | tmp_file=$(mktemp) | ||
| 191 | cat > $tmp_file | 192 | cat > $tmp_file |
| 192 | echo -n 'Type file name: ' > /dev/tty | 193 | echo |
| 193 | read filename && mv $tmp_file $filename | 194 | filename=$1 |
| 194 | echo $filename | 195 | [[ -z $filename ]] && echo -n 'Type file name: ' > /dev/tty && read filename |
| 196 | mv $tmp_file /tmp/$filename | ||
| 197 | echo /tmp/$filename | ||
| 195 | } | 198 | } |
| 196 | 199 | ||
| 197 | # create a new gist with files | 200 | # create a new gist with files |
| 198 | # TODO let user specify filename | ||
| 199 | _create_gist() { | 201 | _create_gist() { |
| 200 | files="$@" | 202 | for i in "$@" |
| 201 | [[ -z "$files" ]] && files=$(_new_file) | 203 | do |
| 202 | echo -n 'Description: ' | 204 | case $i in |
| 203 | read DESC | 205 | -d) |
| 206 | echo $@ | ||
| 207 | description=$2 | ||
| 208 | shift; shift | ||
| 209 | echo desc $description ;; | ||
| 210 | -f) | ||
| 211 | echo $@ | ||
| 212 | filename=$2 | ||
| 213 | shift; shift | ||
| 214 | echo filename $filename ;; | ||
| 215 | esac | ||
| 216 | done | ||
| 217 | exit 0 | ||
| 218 | |||
| 219 | files=$@ && [[ -z "$files" ]] && files=$(_new_file $filename) | ||
| 220 | [[ -z $description ]] && echo -n 'Description: ' && read description | ||
| 221 | |||
| 204 | for file in $files; do | 222 | for file in $files; do |
| 205 | FILE=$(basename $file) | 223 | FILE=$(basename $file) |
| 206 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file | 224 | jq --arg FILE "$FILE" '. as $content | { ($FILE): {content: $content} }' -Rs $file |
| 207 | done |\ | 225 | done |\ |
| 208 | jq --slurp --arg DESC "$DESC" '{ | 226 | jq --slurp --arg DESC "$description" '{ |
| 209 | public: true, | 227 | public: true, |
| 210 | files: add, | 228 | files: add, |
| 211 | description: ($DESC) | 229 | description: ($DESC) |
| 212 | }' |\ | 230 | }' |\ |
| 213 | curl -s -H "$auth_header" --data @- $github_api/gists |\ | 231 | curl -s -H "$auth_header" --data @- $github_api/gists |\ |
| 214 | sed '1 s/^/[/; $ s/$/]/' |\ | 232 | sed '1 s/^/[/; $ s/$/]/' |\ |
| 215 | _parse_response | > $index | 233 | _parse_response |\ |
| 216 | echo "Gist created" | 234 | sed -E "s/^/$(( $(wc -l $index | cut -d' ' -f1) + 1 )) /" >> $index && \ |
| 235 | echo Gist created | ||
| 236 | echo | ||
| 237 | _show_list $index | tail -1 | ||
| 217 | } | 238 | } |
| 218 | 239 | ||
| 219 | # update description of a gist | 240 | # update description of a gist |
| @@ -233,37 +254,26 @@ _help_message() { | |||
| 233 | 254 | ||
| 234 | case "$1" in | 255 | case "$1" in |
| 235 | "") | 256 | "") |
| 236 | _show_list $index | 257 | _show_list $index ;; |
| 237 | ;; | ||
| 238 | star | s) | 258 | star | s) |
| 239 | _show_list $starred | 259 | _show_list $starred ;; |
| 240 | ;; | ||
| 241 | update | u) | 260 | update | u) |
| 242 | _update "$2" | 261 | _update "$2" ;; |
| 243 | ;; | ||
| 244 | new | n) | 262 | new | n) |
| 245 | shift; | 263 | shift |
| 246 | _create_gist $@ | 264 | _create_gist $@ ;; |
| 247 | ;; | ||
| 248 | edit | e) | 265 | edit | e) |
| 249 | _edit_gist "$2" | 266 | _edit_gist "$2" ;; |
| 250 | ;; | ||
| 251 | sync | S) | 267 | sync | S) |
| 252 | _sync_repos | 268 | _sync_repos ;; |
| 253 | ;; | ||
| 254 | detail | d) | 269 | detail | d) |
| 255 | _show_detail "$2" | 270 | _show_detail "$2" ;; |
| 256 | ;; | ||
| 257 | delete | D) | 271 | delete | D) |
| 258 | _delete_gist "$2" | 272 | _delete_gist "$2" ;; |
| 259 | ;; | ||
| 260 | clean | C) | 273 | clean | C) |
| 261 | _clean_repos | 274 | _clean_repos ;; |
| 262 | ;; | ||
| 263 | help | h) | 275 | help | h) |
| 264 | _help_message | 276 | _help_message ;; |
| 265 | ;; | ||
| 266 | *) | 277 | *) |
| 267 | _goto_gist "$1" | 278 | _goto_gist "$1" ;; |
| 268 | ;; | ||
| 269 | esac | 279 | esac |