From d3a1794c6cb2b8385171ef12deac1018db28f0d5 Mon Sep 17 00:00:00 2001 From: typebrook Date: Thu, 14 May 2020 17:20:39 +0800 Subject: Refactor _show_list() --- gist | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/gist b/gist index 8471f00..e5808d2 100755 --- a/gist +++ b/gist @@ -326,6 +326,19 @@ _index_pattern() { fi } +_show_hint() { + if [[ $display == 'tag' && -n $pin ]]; then + local pinned_tags=( $pin ) + echo > /dev/tty + echo Pinned tags: "${pinned_tags[*]/#/#} " > /dev/tty + elif [[ $hint != 'false' ]]; then + local mtime="$(mtime $INDEX | cut -d'.' -f1)" + echo > /dev/tty + echo "Last updated at $mtime" > /dev/tty + echo "Run \"$NAME fetch\" to keep gists up to date, or \"$NAME help\" for more details" > /dev/tty + fi +} + # Display the list of gist, show username for starred gist # If hint=false, do not print hint to tty. If mark=, filter index with regex # If display=tag/language, print tags/languages instead or url @@ -347,29 +360,17 @@ _show_list() { local width=45; local align=' '; fi - local status='' - status=$(_check_repo_status "$folder/${url##*/}" "$blob_code") - status="${status:+${status} }" local extra="$(printf "%-4s" "$file_num $comment_num")" - + local status=''; status=$(_check_repo_status "$folder/${url##*/}" "$blob_code") [[ $index =~ ^s ]] && description="$(printf "%-12s" [${author}]) ${description}" - raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra $status$(_color_description_title "$description")" + raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra ${status:+${status} }$(_color_description_title "$description")" [[ -n $pin ]] && raw_output="$(_color_pinned_tags "$raw_output")" decorator=$(( $(grep -o '\\e\[0m' <<<"$raw_output" | wc -l) *9 )) echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) done - if [[ $display == 'tag' && -n $pin ]]; then - local pinned_tags=( $pin ) - echo > /dev/tty - echo Pinned tags: "${pinned_tags[*]/#/#} " > /dev/tty - elif [[ -z $INPUT && $hint != 'false' ]]; then - local mtime="$(mtime $INDEX | cut -d'.' -f1)" - echo > /dev/tty - echo "Last updated at $mtime" > /dev/tty - echo "Run \"$NAME fetch\" to keep gists up to date, or \"$NAME help\" for more details" > /dev/tty - fi + [[ -z $INPUT ]] && _show_hint || true } # Grep description, filename or file content with a given pattern @@ -834,7 +835,7 @@ _pin_tags() { # show languages of files in gists _gists_with_languages() { local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /|/g' <<<"$@"))" - hint=false display=language _show_list | grep --color=always -Ei "$pattern" + display=language _show_list | grep --color=always -Ei "$pattern" } _gists_with_range() { -- cgit v1.2.3-70-g09d2