diff options
Diffstat (limited to 'scripts')
| -rwxr-xr-x | scripts/gist | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/scripts/gist b/scripts/gist index b82fa0a..444b9a6 100755 --- a/scripts/gist +++ b/scripts/gist | |||
| @@ -62,6 +62,12 @@ config=~/.config/gistrc | |||
| 62 | set -eo pipefail | 62 | set -eo pipefail |
| 63 | [ "$TRACE" ] && set -x | 63 | [ "$TRACE" ] && set -x |
| 64 | 64 | ||
| 65 | ## Grabs an element from a a json string and then echoes it to stdout | ||
| 66 | AccessJsonElement() { | ||
| 67 | PYTHONIOENCODING=utf-8 python -c "from __future__ import print_function; import sys, json; raw = json.load(sys.stdin); $1" | ||
| 68 | return "$?" | ||
| 69 | } | ||
| 70 | |||
| 65 | _auth() { | 71 | _auth() { |
| 66 | echo 'Hi fellow! To access your gists, I need your Github username and a personal token with scope which allows "gist"!' | 72 | echo 'Hi fellow! To access your gists, I need your Github username and a personal token with scope which allows "gist"!' |
| 67 | read -p "Github username: " user < /dev/tty | 73 | read -p "Github username: " user < /dev/tty |
| @@ -146,12 +152,24 @@ _update() { | |||
| 146 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi | 152 | if [[ $auto_sync != "false" ]]; then (_sync_repos $1 > /dev/null 2>&1 &); fi |
| 147 | } | 153 | } |
| 148 | 154 | ||
| 155 | # equal to: jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' | ||
| 156 | handle_gists() { | ||
| 157 | echo ' | ||
| 158 | for gist in raw: | ||
| 159 | print(gist["html_url"], end=" ") | ||
| 160 | print([file["raw_url"] for file in gist["files"].values()], end=" ") | ||
| 161 | print(len(gist["files"]), end=" ") | ||
| 162 | print(gist["comments"], end=" ") | ||
| 163 | print(gist["description"]) | ||
| 164 | ' | ||
| 165 | } | ||
| 166 | |||
| 149 | # TODO check if a user create a very first gist | 167 | # TODO check if a user create a very first gist |
| 150 | _parse_response() { | 168 | _parse_response() { |
| 151 | jq '.[] | "\(.html_url) \([.files[] | .raw_url]) \(.files | keys | length) \(.comments) \(.description)"' \ | 169 | AccessJsonElement "$(handle_gists)" \ |
| 152 | | tac \ | 170 | | tac | sed 's/, /,/g'\ |
| 153 | | while read link file_url_array file_num comment_num description; do | 171 | | while read link file_url_array file_num comment_num description; do |
| 154 | local blob_code=$(echo $file_url_array | jq -r '.[]' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') | 172 | local blob_code=$(echo $file_url_array | tr ',' '\n' | sed -E 's#.*raw/(.*)/.*#\1#' | sort | cut -c -7 | paste -sd '-') |
| 155 | echo $link $blob_code $file_num $comment_num $description | tr -d '"' | 173 | echo $link $blob_code $file_num $comment_num $description | tr -d '"' |
| 156 | done | 174 | done |
| 157 | } | 175 | } |