diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/gist | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/scripts/gist b/scripts/gist index a41d9b3..6f2f78d 100755 --- a/scripts/gist +++ b/scripts/gist | |||
@@ -43,7 +43,6 @@ | |||
43 | 43 | ||
44 | # define your environmemnts here | 44 | # define your environmemnts here |
45 | # TODO support auth prompt | 45 | # TODO support auth prompt |
46 | # TODO add starred repos | ||
47 | # TODO error handling | 46 | # TODO error handling |
48 | # completion | 47 | # completion |
49 | #------------------- | 48 | #------------------- |
@@ -64,7 +63,11 @@ starred=$folder/starred | |||
64 | # Show the list of gist, but not updated time | 63 | # Show the list of gist, but not updated time |
65 | # TODO show git status outdated | 64 | # TODO show git status outdated |
66 | _show_list() { | 65 | _show_list() { |
67 | cat $index |\ | 66 | list_file=$index |
67 | mark="" | ||
68 | [[ "$1" == "--star" ]] && list_file=$starred && mark="s" | ||
69 | |||
70 | cat $list_file |\ | ||
68 | while read line_num link file_url_array file_num extra description; do | 71 | while read line_num link file_url_array file_num extra description; do |
69 | repo=$folder/$(echo $link | sed 's#.*/##') | 72 | repo=$folder/$(echo $link | sed 's#.*/##') |
70 | 73 | ||
@@ -75,22 +78,26 @@ _show_list() { | |||
75 | # if there is a commit not yet push, show red message "ahead" | 78 | # if there is a commit not yet push, show red message "ahead" |
76 | [[ -n $(git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" | 79 | [[ -n $(git cherry) ]] 2>/dev/null && extra="\e[31m[ahead]\e[0m" |
77 | 80 | ||
78 | echo -e $line_num $link $file_num $extra $description | 81 | echo -e $mark$line_num $link $file_num $extra $description |
79 | done | 82 | done |
80 | } | 83 | } |
81 | 84 | ||
82 | # get the list of gists | 85 | # get the list of gists |
83 | # TODO support secret gist | 86 | # TODO support secret gist |
84 | _update() { | 87 | _update() { |
85 | curl -s -H "$auth_header" $github_api/users/$user/gists |\ | 88 | list_file=$index |
89 | route="users/$user/gists" | ||
90 | [[ "$1" == "--star" ]] && list_file=$starred && route="gists/starred" | ||
91 | |||
92 | curl -s -H "$auth_header" $github_api/$route |\ | ||
86 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ | 93 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' |\ |
87 | tac | nl |\ | 94 | tac | nl |\ |
88 | while read line_num link file_url_array file_num comment_num description; do | 95 | while read line_num link file_url_array file_num comment_num description; do |
89 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -r 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 96 | blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
90 | echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -70) | tr -d '"' | 97 | echo $line_num $link $blob_code $file_num $comment_num $(echo $description | cut -c -65) | tr -d '"' |
91 | done > $index && \ | 98 | done > $list_file && \ |
92 | _show_list | 99 | _show_list $1 |
93 | (_sync_repos > /dev/null 2>&1 &) | 100 | (_sync_repos $1 > /dev/null 2>&1 &) |
94 | } | 101 | } |
95 | 102 | ||
96 | _starred() { | 103 | _starred() { |
@@ -98,9 +105,12 @@ _starred() { | |||
98 | } | 105 | } |
99 | 106 | ||
100 | _sync_repos() { | 107 | _sync_repos() { |
108 | list_file=$index | ||
109 | [[ "$1" == "--star" ]] && list_file=$starred && route="gists/starred" | ||
110 | |||
101 | # clone repos which are not in the local | 111 | # clone repos which are not in the local |
102 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ | 112 | comm -13 <(find $folder -maxdepth 1 -type d | sed '1d; s#.*/##' | sort) \ |
103 | <(cat $index | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ | 113 | <(cat $list_file | cut -d' ' -f2 | sed 's#.*/##' | sort) |\ |
104 | xargs -I{} git clone git@github.com:{}.git $folder/{} | 114 | xargs -I{} git clone git@github.com:{}.git $folder/{} |
105 | 115 | ||
106 | # pull if remote repo has different blob objects | 116 | # pull if remote repo has different blob objects |
@@ -118,18 +128,24 @@ _sync_repos() { | |||
118 | } | 128 | } |
119 | 129 | ||
120 | _gist_id() { | 130 | _gist_id() { |
121 | cat $index | sed -n "$1"p | cut -d' ' -f2 | sed -r 's#.*/##' | 131 | list_file=$index |
132 | row_num=$1 | ||
133 | [[ "$1" =~ ^s ]] && list_file=$starred && row_num=$(echo $1 | tr -d 's') | ||
134 | cat $list_file | sed -n "$row_num"p | cut -d' ' -f2 | sed -E 's#.*/##' | ||
122 | } | 135 | } |
123 | 136 | ||
124 | _goto_gist() { | 137 | _goto_gist() { |
125 | gist_num=$(wc -l $index | cut -d' ' -f1) | 138 | gist_num=$(wc -l $index | cut -d' ' -f1) |
126 | if [[ ! "$1" =~ ^[0-9]+$ ]] || (( $1 > $gist_num )); then | 139 | if [[ ! "$1" =~ ^s?[0-9]+$ ]] || (( $1 > $gist_num )); then |
127 | _show_list | grep "$1" || echo Nothing Found | 140 | _show_list | grep "$1" || echo Nothing Found |
128 | return 0 | 141 | return 0 |
129 | fi | 142 | fi |
130 | 143 | ||
131 | GIST_ID=$(_gist_id $1) | 144 | GIST_ID=$(_gist_id $1) |
132 | echo This gist is at $folder/$GIST_ID | 145 | echo This gist is at $folder/$GIST_ID |
146 | echo You can use the following command to jump to this directory: | ||
147 | echo -e " \e[31m. gist $1\e[0m" | ||
148 | echo | ||
133 | cd $folder/$GIST_ID && ls && tig --all 2> /dev/null | 149 | cd $folder/$GIST_ID && ls && tig --all 2> /dev/null |
134 | } | 150 | } |
135 | 151 | ||
@@ -148,11 +164,11 @@ _clean_repos() { | |||
148 | done | 164 | done |
149 | } | 165 | } |
150 | 166 | ||
151 | # TODO star count | 167 | # TODO format with simple text |
152 | _show_detail() { | 168 | _show_detail() { |
153 | GIST_ID=$(_gist_id $1) | 169 | GIST_ID=$(_gist_id $1) |
154 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ | 170 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID |\ |
155 | jq '{site: .html_url, description: .description, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' | 171 | jq '{site: .html_url, description: .description, public: .public, API: .url, created_at: .created_at, updated_at: .updated_at, files: (.files | keys)}' |
156 | 172 | ||
157 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ | 173 | curl -s -H "$auth_header" $github_api/gists/$GIST_ID/comments |\ |
158 | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' | 174 | jq '.[] | {user: .user.login, created_at: .created_at, updated_at: .updated_at, body: .body}' |
@@ -187,7 +203,7 @@ _edit_gist() { | |||
187 | } | 203 | } |
188 | 204 | ||
189 | _help_message() { | 205 | _help_message() { |
190 | sed -r -n ' /^$/ q; 8,$ s/^#//p' $0 | 206 | sed -E -n ' /^$/ q; 8,$ s/^#//p' $0 |
191 | } | 207 | } |
192 | 208 | ||
193 | case "$1" in | 209 | case "$1" in |
@@ -202,10 +218,10 @@ case "$1" in | |||
202 | _edit_gist "$2" "$3" | 218 | _edit_gist "$2" "$3" |
203 | ;; | 219 | ;; |
204 | update | u) | 220 | update | u) |
205 | _update | 221 | _update "$2" |
206 | ;; | 222 | ;; |
207 | star | s) | 223 | star | s) |
208 | _starred | 224 | _show_list --star |
209 | ;; | 225 | ;; |
210 | sync | S) | 226 | sync | S) |
211 | _sync_repos | 227 | _sync_repos |