diff options
author | typebrook <typebrook@gmail.com> | 2020-03-20 20:06:13 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2020-03-20 20:06:13 +0800 |
commit | 13fe88ceb0cfa973a7f886bc5987c306d335a9e8 (patch) | |
tree | 4ef7b4fca182bfe87aedd263ad3a57c4622d9694 | |
parent | 35d37f75325a62ae9c1a907d1529f81dd174ada0 (diff) |
Improve 'gist tags' and 'gist pin'
'gist tag'
- Replace hint with pinned tags
'gist tags'
- Display each line by the first charactor
- highlight pinned tags
- Show pinned tags at the bottom
'gist pin'
- With given tags, update pinned tags and do 'gist tags' again
- With no aguments, print gists with pinned tags
-rwxr-xr-x | gist | 61 |
1 files changed, 42 insertions, 19 deletions
@@ -292,8 +292,14 @@ _show_list() { | |||
292 | echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) | 292 | echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) |
293 | done | 293 | done |
294 | 294 | ||
295 | [[ $hint == 'true' ]] && echo -e '\nrun "gist fetch" to update gists or "gist help" for more details' > /dev/tty \ | 295 | if [[ $tag == 'true' && -n $pin ]]; then |
296 | || return 0 | 296 | local pinned_tags=( $pin ) |
297 | echo | ||
298 | echo Pinned tags: ${pinned_tags[@]/#/#} | ||
299 | elif [[ $hint == 'true' ]]; then | ||
300 | echo | ||
301 | echo 'run "gist fetch" to update gists or "gist help" for more details' > /dev/tty | ||
302 | fi | ||
297 | } | 303 | } |
298 | 304 | ||
299 | # Grep description, filename or file content with a given pattern | 305 | # Grep description, filename or file content with a given pattern |
@@ -674,31 +680,48 @@ _tag_gist() { | |||
674 | fi | 680 | fi |
675 | } | 681 | } |
676 | 682 | ||
683 | # show all tags and pinned tags | ||
677 | _show_tags() { | 684 | _show_tags() { |
678 | while read -r "${INDEX_FORMAT[@]}"; do | 685 | local pinned_tags=( $pin ) |
679 | _hashtags "$description" | tr ' ' '\n' | sed -e '/^$/d' | 686 | local tags=$(while read -r "${INDEX_FORMAT[@]}"; do |
680 | done < $INDEX \ | 687 | _hashtags "$description" | tr ' ' '\n' | sed -e '/^$/d' |
681 | | sort -u | 688 | done < $INDEX | sort -u) |
689 | |||
690 | for prefix in {0..9} {a..z} {A-Z} [^0-9a-zA-Z]; do | ||
691 | local line=$(echo $tags | grep -Eo "#$prefix[^ ]+" | tr '\n' ' ') | ||
692 | [[ -z $line ]] && continue | ||
693 | |||
694 | # add color to pinned tags | ||
695 | pattern='('$(sed 's/ /|/g' <<<"${pinned_tags[@]/#/#}")')' | ||
696 | echo -e "$(sed -E -e "s/$pattern/\\\e[33m\1\\\e[0m/g" <<<"$line")" | ||
697 | done | ||
682 | 698 | ||
683 | tags=( $pin ) | ||
684 | echo | 699 | echo |
685 | echo Pinned tags: ${tags[@]/#/#} | 700 | if [[ ${#pinned_tags} == 0 ]]; then |
701 | echo 'Run "gist pin <tag1> <tag2>..." to pin tags' | ||
702 | else | ||
703 | echo Pinned tags: ${pinned_tags[@]/#/#} | ||
704 | fi | ||
686 | } | 705 | } |
687 | 706 | ||
688 | # pin/unpin tags | 707 | # pin/unpin tags |
689 | _pin_tags() { | 708 | _pin_tags() { |
690 | local new_pinned=( $(echo $pin $* | tr ' ' '\n' | sort | uniq -u | xargs) ) | 709 | # if no arguments, print gists with pinned tags |
691 | for tag in ${new_pinned[@]}; do | 710 | if [[ -z $* && -n $pin ]]; then |
692 | if _gist_id $tag &> /dev/null; then | 711 | hint=false _tag_gist $pin |
693 | echo Invalid tag: $tag | 712 | else |
694 | return 1 | 713 | local new_pinned=( $(echo $pin $* | tr ' ' '\n' | sort | uniq -u | xargs) ) |
695 | fi | 714 | for tag in ${new_pinned[@]}; do |
696 | done || exit 1 | 715 | if [[ $tag =~ [p]*[0-9]+ ]]; then |
716 | echo Invalid tag: $tag | ||
717 | return 1 | ||
718 | fi | ||
719 | done || exit 1 | ||
697 | 720 | ||
698 | hint=false _tag_gist ${new_pinned[@]} | 721 | pin="${new_pinned[@]}" |
699 | echo > /dev/tty | 722 | _show_tags |
700 | echo Pinned tags: ${new_pinned[@]/#/#} > /dev/tty | 723 | sed -i'' -e "/^pin=/ d" "$CONFIG" && echo pin=\'"${new_pinned[@]}"\' >> "$CONFIG" |
701 | sed -i'' -e "/^pin=/ d" "$CONFIG" && echo pin=\'"${new_pinned[@]}"\' >> "$CONFIG" | 724 | fi |
702 | } | 725 | } |
703 | 726 | ||
704 | _apply_config "$@" || exit 1 | 727 | _apply_config "$@" || exit 1 |