aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rwxr-xr-xgist49
1 files changed, 30 insertions, 19 deletions
diff --git a/gist b/gist
index d95561f..b88f0b9 100755
--- a/gist
+++ b/gist
@@ -9,8 +9,8 @@
9# Description: Manage your gists with git and Github API v3 9# Description: Manage your gists with git and Github API v3
10# Usage: gist [command] [<args>] 10# Usage: gist [command] [<args>]
11# 11#
12# [star|s|all|a] List your gists, use 'star' or 's' for your starred gists, 12# [star|s] [all|a] List your latest accessed gists. Use 'star' for your starred gists,
13# 'all' or 'a' for both your and starred gists. Format for each line is: 13# 'all' for all your or starred gists. Format for each line is:
14# <INDEX> <URL> <FILE_NUM> <COMMENT_NUM> <DESCRIPTION> 14# <INDEX> <URL> <FILE_NUM> <COMMENT_NUM> <DESCRIPTION>
15# fetch, f [star|s] Update the local list of your gists, 'star' for your starred gists 15# fetch, f [star|s] Update the local list of your gists, 'star' for your starred gists
16# <INDEX> [-n|--no-action] Show the path of local gist repo and do custom actions(enter sub-shell by default) 16# <INDEX> [-n|--no-action] Show the path of local gist repo and do custom actions(enter sub-shell by default)
@@ -49,9 +49,9 @@
49# Since now a gist is a local cloned repo 49# Since now a gist is a local cloned repo
50# It is your business to do git commit and git push 50# It is your business to do git commit and git push
51 51
52# FIXME For latest 10 or N gists, show related hint
52# TODO scripts for shell completion 53# TODO scripts for shell completion
53# FIXME Chinese charactor break tag alignment 54# FIXME Chinese charactor break tag alignment
54# TODO only shows 10 gists by default, 'gist all' to all gists
55# FIXME Only print files in git with default action 55# FIXME Only print files in git with default action
56# TODO change gist from public to private or reverse versa 56# TODO change gist from public to private or reverse versa
57# TODO feature to exclude tag-value or grep-string 57# TODO feature to exclude tag-value or grep-string
@@ -66,7 +66,7 @@ declare -r CONFIG=~/.config/gist.conf; mkdir -p ~/.config
66declare -r per_page=100 66declare -r per_page=100
67 67
68declare -ar INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') 68declare -ar INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description')
69declare -ar VALID_CONFIGS=('user' 'token' 'folder' 'auto_sync' 'action' 'EDITOR' 'protocol' 'show_untagged' 'pin') 69declare -ar VALID_CONFIGS=('user' 'token' 'folder' 'auto_sync' 'action' 'EDITOR' 'protocol' 'show_untagged' 'pin' 'entry_num')
70declare -A CONFIG_VALUES 70declare -A CONFIG_VALUES
71CONFIG_VALUES[auto_sync]='true|false' 71CONFIG_VALUES[auto_sync]='true|false'
72CONFIG_VALUES[protocol]='https|ssh' 72CONFIG_VALUES[protocol]='https|ssh'
@@ -310,17 +310,28 @@ _show_hint() {
310 fi 310 fi
311} 311}
312 312
313_matched_entries() {
314 awk 'NR==FNR {a[$1]=$0; a[$3]=$0; next} {print a[$1]}' $INDEX -
315}
316
313# Display the list of gist, show username for starred gist 317# Display the list of gist, show username for starred gist
314# If hint=false, do not print hint to tty. If mark=<pattern>, filter index with regex 318# If hint=false, do not print hint to tty. If mark=<pattern>, filter index with regex
315# If display=tag/language, print tags/languages instead or url 319# If display=tag/language, print tags/languages instead or url
316_print_records() { 320_print_entries() {
317 if [[ ! -s $INDEX ]]; then 321 if [[ ! -s $INDEX ]]; then
318 echo "Index file is empty, please run commands "$NAME fetch" or "$NAME create"" 322 echo "Index file is empty, please run commands "$NAME fetch" or "$NAME create""
319 return 0 323 return 0
320 fi 324 fi
321 325
322 local PWD=$(pwd) 326 local PWD=$(pwd)
323 sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ 327 if [[ -n $INPUT ]]; then # Print entries from STDIN
328 _matched_entries <<<"$INPUT"
329 elif [[ ! $1 =~ (a|all) ]]; then # Print the latest accessed 10(or if configured) entries
330 cd $folder && cut -d' ' -f3 <$INDEX | sort -u | xargs ls --time=atime --directory 2>/dev/null \
331 | head -${entry_num-:10} | _matched_entries
332 else # Print all entries if the first augument is a or all
333 sed -Ee "/^$mark/ !d" $INDEX
334 fi \
324 | while read -r "${INDEX_FORMAT[@]}"; do 335 | while read -r "${INDEX_FORMAT[@]}"; do
325 local message="$(printf '%-56s' ${GIST_DOMAIN}/${gist_id})" 336 local message="$(printf '%-56s' ${GIST_DOMAIN}/${gist_id})"
326 if [[ $display == 'tag' ]]; then 337 if [[ $display == 'tag' ]]; then
@@ -359,7 +370,7 @@ _grep_content() {
359 | while read -r "${INDEX_FORMAT[@]}"; do 370 | while read -r "${INDEX_FORMAT[@]}"; do
360 # grep from description 371 # grep from description
361 if grep --color=always -iq "$1" <<<"$description"; then 372 if grep --color=always -iq "$1" <<<"$description"; then
362 hint=false mark="$index " _print_records 373 hint=false mark="$index " _print_entries
363 else 374 else
364 local repo=${folder}/${gist_id} 375 local repo=${folder}/${gist_id}
365 [[ -d $repo ]] && cd $repo || continue 376 [[ -d $repo ]] && cd $repo || continue
@@ -371,7 +382,7 @@ _grep_content() {
371 grep --color=always -EHi -m1 "$1" * 2>/dev/null | head -1 382 grep --color=always -EHi -m1 "$1" * 2>/dev/null | head -1
372 }) 383 })
373 384
374 [[ -n $result ]] && cd - >/dev/null && hint=false mark="$index " _print_records && sed -e 's/^/ /' <<<"$result" 385 [[ -n $result ]] && cd - >/dev/null && hint=false mark="$index " _print_entries && sed -e 's/^/ /' <<<"$result"
375 fi 386 fi
376 done 387 done
377} 388}
@@ -464,7 +475,7 @@ _update_gists() {
464 echo $prefix$index $public $extra 475 echo $prefix$index $public $extra
465 done >> $INDEX 476 done >> $INDEX
466 477
467 _print_records 478 _print_entries
468 [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) 479 [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &)
469 true 480 true
470} 481}
@@ -531,7 +542,7 @@ _gist_id() {
531 GIST_ID=${gist_id} 542 GIST_ID=${gist_id}
532 543
533 if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then 544 if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then
534 echo -e "$(hint=false _print_records | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" 545 echo -e "$(hint=false _print_entries | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')"
535 echo 546 echo
536 echo -e "Invalid index: \e[33m$1\e[0m" 547 echo -e "Invalid index: \e[33m$1\e[0m"
537 echo 'Use the indices blinking instead (like 1 or s1)' 548 echo 'Use the indices blinking instead (like 1 or s1)'
@@ -741,7 +752,7 @@ _create_gist() {
741 # shellcheck disable=2181 752 # shellcheck disable=2181
742 if [[ $? -eq 0 ]]; then 753 if [[ $? -eq 0 ]]; then
743 echo 'Gist is created' 754 echo 'Gist is created'
744 INPUT=$(tail -1 $INDEX | cut -d' ' -f1) hint=false _print_records 755 INPUT=$(tail -1 $INDEX | cut -d' ' -f1) hint=false _print_entries
745 else 756 else
746 echo 'Failed to create gist' 757 echo 'Failed to create gist'
747 fi 758 fi
@@ -765,7 +776,7 @@ _edit_gist() {
765 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" 776 echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data"
766 777
767 http_method PATCH "${GITHUB_API}/gists/${GIST_ID}" | _update_gist $index \ 778 http_method PATCH "${GITHUB_API}/gists/${GIST_ID}" | _update_gist $index \
768 && hint=false mark="$index " _print_records \ 779 && hint=false mark="$index " _print_entries \
769 || echo 'Fail to modify gist description' 780 || echo 'Fail to modify gist description'
770} 781}
771 782
@@ -787,7 +798,7 @@ _check_protocol() {
787_tag_gist() { 798_tag_gist() {
788 # if no tag is given, show gist list with tags 799 # if no tag is given, show gist list with tags
789 if [[ -z $* ]]; then 800 if [[ -z $* ]]; then
790 display=tag _print_records 801 display=tag _print_entries
791 # if user want to change tags of a gist 802 # if user want to change tags of a gist
792 elif _gist_id $1 &>/dev/null; then 803 elif _gist_id $1 &>/dev/null; then
793 _show_detail $1 | sed 3,6d && echo 804 _show_detail $1 | sed 3,6d && echo
@@ -799,7 +810,7 @@ _tag_gist() {
799 # if user want to filter gists with given tags 810 # if user want to filter gists with given tags
800 else 811 else
801 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" 812 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )"
802 hint=false mark=${INPUT:+.} display=tag _print_records | grep --color=always -E "$pattern" 813 hint=false mark=${INPUT:+.} display=tag _print_entries | grep --color=always -E "$pattern"
803 fi 814 fi
804} 815}
805 816
@@ -850,7 +861,7 @@ _pin_tags() {
850# show languages of files in gists 861# show languages of files in gists
851_gists_with_languages() { 862_gists_with_languages() {
852 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /|/g' <<<"$@"))" 863 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /|/g' <<<"$@"))"
853 display=language _print_records | grep --color=always -Ei "$pattern" 864 display=language _print_entries | grep --color=always -Ei "$pattern"
854} 865}
855 866
856_gists_with_range() { 867_gists_with_range() {
@@ -860,7 +871,7 @@ _gists_with_range() {
860 local maximum=$(sed -Ene "/^${prefix:-[^s]}/ p" $INDEX | wc -l) 871 local maximum=$(sed -Ene "/^${prefix:-[^s]}/ p" $INDEX | wc -l)
861 local range=$(sed -Ee "s/s//g; s/^-/1-/; s/-$/-$maximum/; s/-/ /" <<< "$*") 872 local range=$(sed -Ee "s/s//g; s/^-/1-/; s/-$/-$maximum/; s/-/ /" <<< "$*")
862 INPUT=$(seq $range | sed -e "s/^/p?$prefix/") 873 INPUT=$(seq $range | sed -e "s/^/p?$prefix/")
863 hint=false _print_records 874 hint=false _print_entries
864} 875}
865 876
866_access_last_index() { 877_access_last_index() {
@@ -873,11 +884,11 @@ if [[ $init ]]; then _update_gists; exit 0; fi
873shopt -s extglob 884shopt -s extglob
874case "$1" in 885case "$1" in
875 "") 886 "")
876 _print_records ;; 887 mark=[^s]; _print_entries ;;
877 star | s) 888 star | s)
878 mark=s; _print_records ;; 889 mark=s; _print_entries $2 ;;
879 all | a) 890 all | a)
880 mark=.; _print_records ;; 891 mark=[^s]; _print_entries $1 ;;
881 fetch | f) 892 fetch | f)
882 [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s] 893 [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s]
883 _update_gists ;; 894 _update_gists ;;