diff options
author | typebrook <typebrook@gmail.com> | 2020-01-18 22:08:00 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-18 22:08:00 +0800 |
commit | 4b39d3ad54f355536260d4c990c3061a923baf29 (patch) | |
tree | ffcb85ec958d5b227679b9b1bc89a3e8267a7dd2 /scripts | |
parent | 7064bf2f83f8377f0df49f43e6d6344686c34c7d (diff) |
update
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/gist | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/scripts/gist b/scripts/gist index 6038282..144a033 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -93,21 +93,27 @@ _show_list() { | |||
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.gist.com" > /dev/tty | 96 | echo "fetching from api.github.com..." > /dev/tty |
97 | list_file=$index | 97 | list_file=$index |
98 | route="users/$user/gists" | 98 | route="users/$user/gists" |
99 | mark="" | 99 | mark="" |
100 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" | 100 | [[ "$1" =~ ^(star|s)$ ]] && list_file=$starred && route="gists/starred" && mark="s" |
101 | 101 | ||
102 | curl -s -H "$auth_header" $github_api/$route |\ | 102 | curl -s -H "$auth_header" $github_api/$route |\ |
103 | _parse_response | nl -s' ' | sed -E "s/^ */$mark/" > $list_file && \ | ||
104 | _show_list $list_file | ||
105 | #(_sync_repos $1 > /dev/null 2>&1 &) | ||
106 | _sync_repos $1 | ||
107 | } | ||
108 | |||
109 | # TODO check if a user create a very first gist | ||
110 | _parse_response() { | ||
103 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ | 111 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ |
104 | tac | nl |\ | 112 | tac |\ |
105 | while read line_num link file_url_array file_num comment_num description; do | 113 | while read link file_url_array file_num comment_num description; do |
106 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 114 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
107 | echo $mark$line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -60) | tr -d '"' | 115 | echo $link $blob_code $file_num $comment_num $(echo $description | cut -c -60) | tr -d '"' |
108 | done > $list_file && \ | 116 | done |
109 | _show_list $list_file | ||
110 | (_sync_repos $1 > /dev/null 2>&1 &) | ||
111 | } | 117 | } |
112 | 118 | ||
113 | _sync_repos() { | 119 | _sync_repos() { |
@@ -181,18 +187,19 @@ _show_detail() { | |||
181 | 187 | ||
182 | _new_file() { | 188 | _new_file() { |
183 | tmp_file=$(mktemp) | 189 | tmp_file=$(mktemp) |
184 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" | 190 | [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty |
185 | cat > $tmp_file | 191 | cat > $tmp_file |
186 | echo $tmp_file | 192 | echo -n 'Type file name: ' > /dev/tty |
193 | read filename && mv $tmp_file $filename | ||
194 | echo $filename | ||
187 | } | 195 | } |
188 | 196 | ||
189 | # create a new gist with files | 197 | # create a new gist with files |
190 | # FIXME consider file not exist -> let user type? return 1? | 198 | # FIXME consider file not exist -> let user type? return 1? |
191 | _create_gist() { | 199 | _create_gist() { |
192 | files="$@" | 200 | files="$@" |
193 | [[ -z "$files" ]] && _new_file | read files | 201 | [[ -z "$files" ]] && files=$(_new_file) |
194 | echo files: $files | 202 | echo -n 'Description: ' |
195 | echo -n 'description: ' | ||
196 | read DESC | 203 | read DESC |
197 | for file in $files; do | 204 | for file in $files; do |
198 | FILE=$(basename $file) | 205 | FILE=$(basename $file) |
@@ -203,8 +210,10 @@ _create_gist() { | |||
203 | files: add, | 210 | files: add, |
204 | description: ($DESC) | 211 | description: ($DESC) |
205 | }' |\ | 212 | }' |\ |
206 | curl -s -H "$auth_header" --data @- $github_api/gists > /dev/null && \ | 213 | curl -s -H "$auth_header" --data @- $github_api/gists |\ |
207 | _update | 214 | sed '1 s/^/[/; $ s/$/]/' |\ |
215 | _parse_response | > $index | ||
216 | echo "Gist created" | ||
208 | } | 217 | } |
209 | 218 | ||
210 | # update description of a gist | 219 | # update description of a gist |