aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortypebrook <typebrook@gmail.com>2020-03-22 13:52:37 +0800
committertypebrook <typebrook@gmail.com>2020-03-22 13:58:37 +0800
commit36978ecf47983a5af9b0f8063a950d1af4013cd2 (patch)
treeb68c72a57d70a82b9cbc808db4ce710bf098d341
parent91da68abaaabd9d815520f4269ee922043c738ad (diff)
parenta8cfd85be5372512f308ce3caaa629d524cc667f (diff)
Merge branch 'refactor-stdin'
- If STDIN is not empty, read indices from first column in each line, and only show list with those indices - Support 'gist', 'gist star', 'gist grep', 'gist tag' - So right now, you can concate the result for each subcommand: # get gists with tag 1 or tag2, and with tag 3 gist tag tag1 tag2 | gist tag 3 # grep gists with string1, and only show starred gist gist grep string1 | gist star
-rwxr-xr-xgist50
1 files changed, 32 insertions, 18 deletions
diff --git a/gist b/gist
index e0804a9..b047368 100755
--- a/gist
+++ b/gist
@@ -39,8 +39,6 @@
39# Since now a gist is a local cloned repo 39# Since now a gist is a local cloned repo
40# It is your business to do git commit and git push 40# It is your business to do git commit and git push
41 41
42# TODO fix pattern of the last tag in description
43# TODO support pipe with tag filter, like 'gist tag <tag1> <tag2> | gist tag <tag3>'
44# TODO support file type from github API, like 'gist code' 42# TODO support file type from github API, like 'gist code'
45# TODO list dot file in 'gist detail' 43# TODO list dot file in 'gist detail'
46# TODO README/helper message about tag and default action ${SHELL:-bash} 44# TODO README/helper message about tag and default action ${SHELL:-bash}
@@ -55,6 +53,7 @@ CONFIG=~/.config/gist.conf; mkdir -p ~/.config
55 53
56INDEX_FORMAT=('index' 'url' 'blob_code' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') 54INDEX_FORMAT=('index' 'url' 'blob_code' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description')
57TAG_CHAR='-_[:alnum:]' 55TAG_CHAR='-_[:alnum:]'
56[[ ! -t 0 ]] && INPUT=$(cat)
58 57
59hint=${hint:=true} # default to show hint with list of gist 58hint=${hint:=true} # default to show hint with list of gist
60auto_sync=true # automatically clone the gist repo 59auto_sync=true # automatically clone the gist repo
@@ -268,22 +267,27 @@ _check_repo_status() {
268 fi 267 fi
269} 268}
270 269
270# check given index is necessary to handle
271_index_pattern() {
272 if [[ -z "$INPUT" ]]; then
273 echo .+
274 else
275 echo "($(echo "$INPUT" | sed -Ee '/^ {4,}/ d; s/^ *//; /^$/ q' | cut -d' ' -f1 | xargs | tr ' ' '|'))"
276 fi
277}
278
271# Display the list of gist, show username for starred gist 279# Display the list of gist, show username for starred gist
272# If hint=true, print hint to tty. If mark=<pattern>, filter index with regex 280# If hint=true, print hint to tty. If mark=<pattern>, filter index with regex
273# If tag=true, print tags instead or url 281# If tag=true, print tags instead or url
274# TODO color private/starred mark 282# TODO color private/starred mark
275_show_list() { 283_show_list() {
276 if [[ -n $1 ]]; then 284 if [[ ! -e $INDEX ]]; then
277 local input=$1
278 elif [[ ! -e $INDEX ]]; then
279 echo 'No local file found for last update, please run command:' 285 echo 'No local file found for last update, please run command:'
280 echo ' gist update' 286 echo ' gist update'
281 return 0 287 return 0
282 else
283 local input=$INDEX
284 fi 288 fi
285 289
286 sed -Ene "/^${mark:-[^s]}/ p" $input \ 290 sed -Ee "/^${mark:-[^s]}/ !d; /^$(_index_pattern) / !d" $INDEX \
287 | while read -r "${INDEX_FORMAT[@]}"; do 291 | while read -r "${INDEX_FORMAT[@]}"; do
288 local repo; repo=$folder/${url##*/} 292 local repo; repo=$folder/${url##*/}
289 local extra; extra=$(_check_repo_status "$repo" "$blob_code") 293 local extra; extra=$(_check_repo_status "$repo" "$blob_code")
@@ -302,7 +306,7 @@ _show_list() {
302 if [[ $tag == 'true' && -n $pin ]]; then 306 if [[ $tag == 'true' && -n $pin ]]; then
303 local pinned_tags=( $pin ) 307 local pinned_tags=( $pin )
304 echo 308 echo
305 echo Pinned tags: "${pinned_tags[@]/#/#}" 309 echo Pinned tags: "${pinned_tags[@]/#/#} "
306 elif [[ $hint == 'true' ]]; then 310 elif [[ $hint == 'true' ]]; then
307 echo 311 echo
308 echo 'run "gist fetch" to update gists or "gist help" for more details' > /dev/tty 312 echo 'run "gist fetch" to update gists or "gist help" for more details' > /dev/tty
@@ -313,20 +317,25 @@ _show_list() {
313# TODO add option to configure case-sensitive 317# TODO add option to configure case-sensitive
314_grep_content() { 318_grep_content() {
315 if [[ -z $1 ]]; then echo 'Please give a pattern' && return 1; fi 319 if [[ -z $1 ]]; then echo 'Please give a pattern' && return 1; fi
316 while read -r "${INDEX_FORMAT[@]}"; do 320
321 sed -Ee "/^$(_index_pattern) / !d" $INDEX \
322 | while read -r "${INDEX_FORMAT[@]}"; do
323 # grep from description
317 if grep --color=always -iq "$1" <<<"$description"; then 324 if grep --color=always -iq "$1" <<<"$description"; then
318 hint=false mark="$index " _show_list | grep --color=always -Ei "$1" 325 hint=false mark="$index " _show_list | grep --color=always -Ei "$1"
319 else 326 else
320 local repo=$folder/${url##*/} 327 local repo=$folder/${url##*/}
321 [[ -d $repo ]] && cd $repo || continue 328 [[ -d $repo ]] && cd $repo || continue
329 # grep from filenames
322 local file=$(ls $repo | grep --color=always -Ei "$1") 330 local file=$(ls $repo | grep --color=always -Ei "$1")
331 # grep from content of files
323 local content=$(grep --color=always -EHi "$1" * | head -1) 332 local content=$(grep --color=always -EHi "$1" * | head -1)
324 333
325 [[ -n $file && file="$file\n" || -n $content ]] \ 334 [[ -n $file && file="$file\n" || -n $content ]] \
326 && hint=false mark="$index " _show_list \ 335 && hint=false mark="$index " _show_list \
327 && echo -e " $file$content\n" 336 && echo -e " $file$content"
328 fi 337 fi
329 done < $INDEX 338 done
330} 339}
331 340
332# Parse JSON object of the result of gist fetch 341# Parse JSON object of the result of gist fetch
@@ -573,9 +582,14 @@ _set_gist() {
573 582
574# Let user type the content of gist before setting filename 583# Let user type the content of gist before setting filename
575_new_file() { 584_new_file() {
576 [[ -t 0 ]] && echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
577 tmp_file=$(mktemp) 585 tmp_file=$(mktemp)
578 cat > "$tmp_file" 586 if [[ -z $INPUT ]]; then
587 echo "Type a gist. <Ctrl-C> to cancel, <Ctrl-D> when done" > /dev/tty
588 cat > "$tmp_file"
589 else
590 echo "$INPUT" > "$tmp_file"
591 fi
592
579 echo > /dev/tty 593 echo > /dev/tty
580 [[ -z $1 ]] && read -r -p 'Type file name: ' filename < /dev/tty 594 [[ -z $1 ]] && read -r -p 'Type file name: ' filename < /dev/tty
581 mv "$tmp_file" /tmp/"$filename" 595 mv "$tmp_file" /tmp/"$filename"
@@ -616,7 +630,7 @@ _create_gist() {
616 # shellcheck disable=2181 630 # shellcheck disable=2181
617 if [[ $? -eq 0 ]]; then 631 if [[ $? -eq 0 ]]; then
618 echo 'Gist is created' 632 echo 'Gist is created'
619 hint=false _show_list | tail -1 633 INPUT=$(tail -1 $INDEX | cut -d' ' -f1) hint=false _show_list | tail -1
620 else 634 else
621 echo 'Failed to create gist' 635 echo 'Failed to create gist'
622 fi 636 fi
@@ -680,10 +694,10 @@ _tag_gist() {
680 (_edit_gist $1 "$new_desc" &>/dev/null &) 694 (_edit_gist $1 "$new_desc" &>/dev/null &)
681 # if user want to filter gists with given tags 695 # if user want to filter gists with given tags
682 elif [[ -n $1 ]]; then 696 elif [[ -n $1 ]]; then
683 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/$/[[:space:]]/' <<<"$@"))" 697 local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g' <<<"$@") )"
684 sed -En "/$pattern/ p" $INDEX | hint=false tag=true _show_list - 698 hint=false tag=true _show_list | grep --color=always -E "$pattern"
685 else 699 else
686 tag=true _show_list 700 tag=true tag=true _show_list
687 fi 701 fi
688} 702}
689 703