From bb4bcf9dd92bfba5a016f0cc0ca6329598dbdd01 Mon Sep 17 00:00:00 2001 From: typebrook Date: Mon, 18 May 2020 21:50:29 +0800 Subject: Refactor color functions --- gist | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/gist b/gist index 0e28f20..6825d9b 100755 --- a/gist +++ b/gist @@ -237,14 +237,12 @@ _trailing_hashtags() { grep -Eo " #[$TAG_CHAR #]+$" <<<"$1" | sed -Ee "s/.* [$TAG_CHAR]+//g" } -_color_pinned_tags() { - local pinned_tags=( $pin ) - pattern='('$(sed -E 's/ /[[:space:]]|/g; s/\./[^ ]/g; s/$/[[:space:]]/' <<<"${pinned_tags[@]/#/#}")')' - sed -E -e "s/$pattern/\\\e[33m\1\\\e[0m/g" <<<"$1 " +_color_pattern() { + sed -Ee "s/$1/\\\e[33m\1\\\e[0m/g" } -_color_description_title() { - sed -E -e 's/^\[(.+)\]/\\e[33m[\1]\\e[0m/' <<<"$1" +_pattern_pinned_tags() { + echo '('$(sed -E 's/ /[[:space:]]|/g; s/\./[^ ]/g; s/$/[[:space:]]/' <<<"$@")')' } # Return git status of a given repo @@ -320,8 +318,11 @@ _show_list() { 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:+${status} }$(_color_description_title "$description")" - [[ -n $pin ]] && raw_output="$(_color_pinned_tags "$raw_output")" + raw_output="$(printf "%-3s" "$index") $(printf "%${align:--}${width:-56}s" "$message") $extra ${status:+${status} }$(_color_pattern '^(\[.+\])' <<<"$description")" + if [[ -n $pin && $display == 'tag' ]]; then + local pinned_tags=($pin); local pattern="$(_pattern_pinned_tags ${pinned_tags[@]/#/#})" + raw_output="$(_color_pattern "$pattern" <<<"$raw_output")" + fi decorator=$(( $(grep -o '\\e\[0m' <<<"$raw_output" | wc -l) *9 )) echo -e "$raw_output" | cut -c -$(( $(tput cols) +decorator )) done @@ -601,7 +602,7 @@ _show_detail() { sed -En -e "/[^ ]+ [^ ]+$GIST_ID / {p; q}" $INDEX \ | while read -r "${INDEX_FORMAT[@]}"; do - echo -e desc: $(_color_description_title "$description") + echo -e desc: $(_color_pattern '^(\[.+\])' <<<"$description") echo -e tags: ${tags_string//,/ } echo -e site: https://gist.github.com/$GIST_ID echo -e API : https://api.github.com/gists/$GIST_ID @@ -780,6 +781,7 @@ _tag_gist() { # show all tags and pinned tags _show_tags() { local pinned_tags=( $pin ) + local pattern=$(_pattern_pinned_tags "${pinned_tags[@]/#/#}") local tags=$(while read -r "${INDEX_FORMAT[@]}"; do echo ${tags_string//,/ } done < $INDEX | tr ' ' '\n' | sed -e '/^$/d' | sort -u) @@ -789,7 +791,7 @@ _show_tags() { [[ -z $line ]] && continue # add color to pinned tags - echo -e "$(_color_pinned_tags "$line")" + echo -e $(_color_pattern "$pattern" <<<"$line") done echo -- cgit v1.2.3-70-g09d2