diff options
author | typebrook <typebrook@gmail.com> | 2020-01-17 13:29:17 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-01-17 13:29:17 +0800 |
commit | fa4547ef64a495ba145584fa96ed2889c66b6f16 (patch) | |
tree | 16ba8a36bab3fdcb3d8d915e5b7af5fa8253373e /scripts/gist | |
parent | 68d648553cd7411c2c4bcb92081bfb5d95d3b260 (diff) |
update
Diffstat (limited to 'scripts/gist')
-rwxr-xr-x | scripts/gist | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/scripts/gist b/scripts/gist index 69ec619..3262a76 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -62,12 +62,9 @@ starred=$folder/starred | |||
62 | 62 | ||
63 | # Show the list of gist, but not updated time | 63 | # Show the list of gist, but not updated time |
64 | # TODO show git status outdated | 64 | # TODO show git status outdated |
65 | # TODO unit test | ||
65 | _show_list() { | 66 | _show_list() { |
66 | list_files=$index | 67 | cat $1 |\ |
67 | [[ "$1" == "--star" ]] && list_files=$starred | ||
68 | [[ "$1" == "--all" ]] && list_files="$index $starred" | ||
69 | |||
70 | cat $list_files |\ | ||
71 | while read line_num link file_url_array file_num extra description; do | 68 | while read line_num link file_url_array file_num extra description; do |
72 | repo=$folder/$(echo $link | sed 's#.*/##') | 69 | repo=$folder/$(echo $link | sed 's#.*/##') |
73 | 70 | ||
@@ -97,7 +94,7 @@ _update() { | |||
97 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 94 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
98 | echo $mark$line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -65) | tr -d '"' | 95 | echo $mark$line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -65) | tr -d '"' |
99 | done > $list_file && \ | 96 | done > $list_file && \ |
100 | _show_list $1 | 97 | _show_list $list_file |
101 | (_sync_repos $1 > /dev/null 2>&1 &) | 98 | (_sync_repos $1 > /dev/null 2>&1 &) |
102 | } | 99 | } |
103 | 100 | ||
@@ -128,21 +125,21 @@ _sync_repos() { | |||
128 | echo Everything is fine! | 125 | echo Everything is fine! |
129 | } | 126 | } |
130 | 127 | ||
131 | # TODO simplify | ||
132 | _gist_id() { | 128 | _gist_id() { |
133 | cat $index $starred | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##' | 129 | GIST_ID=$(cat $index $starred | sed -n "/^$1 / p" | cut -d' ' -f2 | sed -E 's#.*/##') |
134 | } | ||
135 | |||
136 | _goto_gist() { | ||
137 | GIST_ID=$(_gist_id $1) | ||
138 | if [[ -z $GIST_ID ]]; then | 130 | if [[ -z $GIST_ID ]]; then |
139 | echo Not a valid index: $1 | 131 | echo Not a valid index: $1 |
140 | echo Use the index number in the first column instead: | 132 | echo Use the index number in the first column instead: |
141 | echo | 133 | echo |
142 | _show_list --all | 134 | _show_list "$index $starred" |
143 | return 0 | 135 | return 1 |
136 | else | ||
137 | echo $GIST_ID | ||
144 | fi | 138 | fi |
139 | } | ||
145 | 140 | ||
141 | _goto_gist() { | ||
142 | GIST_ID=$(_gist_id $1 || return 0) | ||
146 | echo This gist is at $folder/$GIST_ID | 143 | echo This gist is at $folder/$GIST_ID |
147 | echo -e "You can use the following command to jump to this directory: \n" | 144 | echo -e "You can use the following command to jump to this directory: \n" |
148 | echo -e " \e[31m. gist $1\e[0m" | 145 | echo -e " \e[31m. gist $1\e[0m" |
@@ -151,7 +148,7 @@ _goto_gist() { | |||
151 | } | 148 | } |
152 | 149 | ||
153 | _delete_gist() { | 150 | _delete_gist() { |
154 | GIST_ID=$(_gist_id $1) | 151 | GIST_ID=$(_gist_id $1 || return 0) |
155 | curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ | 152 | curl -X DELETE -s -H "$auth_header" $github_api/gists/$GIST_ID && \ |
156 | _update | 153 | _update |
157 | } | 154 | } |
@@ -167,7 +164,7 @@ _clean_repos() { | |||
167 | 164 | ||
168 | # TODO format with simple text | 165 | # TODO format with simple text |
169 | _show_detail() { | 166 | _show_detail() { |
170 | GIST_ID=$(_gist_id $1) | 167 | GIST_ID=$(_gist_id $1 || return 0) |
171 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ | 168 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ |
172 | jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' | 169 | jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' |
173 | 170 | ||
@@ -197,8 +194,7 @@ _create_gist() { | |||
197 | 194 | ||
198 | # update description of a gist | 195 | # update description of a gist |
199 | _edit_gist() { | 196 | _edit_gist() { |
200 | [[ ! $1 =~ ^[0-9]+$ ]] && echo not a valid index number && return 1 | 197 | GIST_ID=$(_gist_id $1 || return 0) |
201 | GIST_ID=$(_gist_id $1) | ||
202 | 198 | ||
203 | echo -n 'Type new description: ' | 199 | echo -n 'Type new description: ' |
204 | read DESC | 200 | read DESC |
@@ -213,7 +209,7 @@ _help_message() { | |||
213 | 209 | ||
214 | case "$1" in | 210 | case "$1" in |
215 | "") | 211 | "") |
216 | _show_list | 212 | _show_list $index |
217 | ;; | 213 | ;; |
218 | new | n) | 214 | new | n) |
219 | shift; | 215 | shift; |
@@ -226,7 +222,7 @@ case "$1" in | |||
226 | _update "$2" | 222 | _update "$2" |
227 | ;; | 223 | ;; |
228 | star | s) | 224 | star | s) |
229 | _show_list --star | 225 | _show_list $starred |
230 | ;; | 226 | ;; |
231 | sync | S) | 227 | sync | S) |
232 | _sync_repos | 228 | _sync_repos |