From f92a03e8e1c9f2f4d0ed171e4923ae7edec005ac Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Thu, 17 Sep 2020 14:42:17 +0800 Subject: update --- gist | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/gist b/gist index d95561f..b88f0b9 100755 --- a/gist +++ b/gist @@ -9,8 +9,8 @@ # Description: Manage your gists with git and Github API v3 # Usage: gist [command] [] # -# [star|s|all|a] List your gists, use 'star' or 's' for your starred gists, -# 'all' or 'a' for both your and starred gists. Format for each line is: +# [star|s] [all|a] List your latest accessed gists. Use 'star' for your starred gists, +# 'all' for all your or starred gists. Format for each line is: # # fetch, f [star|s] Update the local list of your gists, 'star' for your starred gists # [-n|--no-action] Show the path of local gist repo and do custom actions(enter sub-shell by default) @@ -49,9 +49,9 @@ # Since now a gist is a local cloned repo # It is your business to do git commit and git push +# FIXME For latest 10 or N gists, show related hint # TODO scripts for shell completion # FIXME Chinese charactor break tag alignment -# TODO only shows 10 gists by default, 'gist all' to all gists # FIXME Only print files in git with default action # TODO change gist from public to private or reverse versa # TODO feature to exclude tag-value or grep-string @@ -66,7 +66,7 @@ declare -r CONFIG=~/.config/gist.conf; mkdir -p ~/.config declare -r per_page=100 declare -ar INDEX_FORMAT=('index' 'public' 'gist_id' 'tags_string' 'blob_code' 'file_array' 'file_num' 'comment_num' 'author' 'created_at' 'updated_at' 'description') -declare -ar VALID_CONFIGS=('user' 'token' 'folder' 'auto_sync' 'action' 'EDITOR' 'protocol' 'show_untagged' 'pin') +declare -ar VALID_CONFIGS=('user' 'token' 'folder' 'auto_sync' 'action' 'EDITOR' 'protocol' 'show_untagged' 'pin' 'entry_num') declare -A CONFIG_VALUES CONFIG_VALUES[auto_sync]='true|false' CONFIG_VALUES[protocol]='https|ssh' @@ -310,17 +310,28 @@ _show_hint() { fi } +_matched_entries() { + awk 'NR==FNR {a[$1]=$0; a[$3]=$0; next} {print a[$1]}' $INDEX - +} + # 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 -_print_records() { +_print_entries() { if [[ ! -s $INDEX ]]; then echo "Index file is empty, please run commands "$NAME fetch" or "$NAME create"" return 0 fi local PWD=$(pwd) - sed -Ee "/^$mark/ !d; /^$(_index_pattern) / !d" $INDEX \ + if [[ -n $INPUT ]]; then # Print entries from STDIN + _matched_entries <<<"$INPUT" + elif [[ ! $1 =~ (a|all) ]]; then # Print the latest accessed 10(or if configured) entries + cd $folder && cut -d' ' -f3 <$INDEX | sort -u | xargs ls --time=atime --directory 2>/dev/null \ + | head -${entry_num-:10} | _matched_entries + else # Print all entries if the first augument is a or all + sed -Ee "/^$mark/ !d" $INDEX + fi \ | while read -r "${INDEX_FORMAT[@]}"; do local message="$(printf '%-56s' ${GIST_DOMAIN}/${gist_id})" if [[ $display == 'tag' ]]; then @@ -359,7 +370,7 @@ _grep_content() { | while read -r "${INDEX_FORMAT[@]}"; do # grep from description if grep --color=always -iq "$1" <<<"$description"; then - hint=false mark="$index " _print_records + hint=false mark="$index " _print_entries else local repo=${folder}/${gist_id} [[ -d $repo ]] && cd $repo || continue @@ -371,7 +382,7 @@ _grep_content() { grep --color=always -EHi -m1 "$1" * 2>/dev/null | head -1 }) - [[ -n $result ]] && cd - >/dev/null && hint=false mark="$index " _print_records && sed -e 's/^/ /' <<<"$result" + [[ -n $result ]] && cd - >/dev/null && hint=false mark="$index " _print_entries && sed -e 's/^/ /' <<<"$result" fi done } @@ -464,7 +475,7 @@ _update_gists() { echo $prefix$index $public $extra done >> $INDEX - _print_records + _print_entries [[ $auto_sync != false ]] && (_sync_repos &> /dev/null &) true } @@ -531,7 +542,7 @@ _gist_id() { GIST_ID=${gist_id} if [[ -z $GIST_ID || ! $1 =~ [0-9a-z]+ ]]; then - echo -e "$(hint=false _print_records | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" + echo -e "$(hint=false _print_entries | sed -Ee 's/^( *[0-9a-z]+)/\\e[5m\1\\e[0m/')" echo echo -e "Invalid index: \e[33m$1\e[0m" echo 'Use the indices blinking instead (like 1 or s1)' @@ -741,7 +752,7 @@ _create_gist() { # shellcheck disable=2181 if [[ $? -eq 0 ]]; then echo 'Gist is created' - INPUT=$(tail -1 $INDEX | cut -d' ' -f1) hint=false _print_records + INPUT=$(tail -1 $INDEX | cut -d' ' -f1) hint=false _print_entries else echo 'Failed to create gist' fi @@ -765,7 +776,7 @@ _edit_gist() { echo '{' \"description\": \""${DESC//\"/\\\"}"\" '}' > "$http_data" http_method PATCH "${GITHUB_API}/gists/${GIST_ID}" | _update_gist $index \ - && hint=false mark="$index " _print_records \ + && hint=false mark="$index " _print_entries \ || echo 'Fail to modify gist description' } @@ -787,7 +798,7 @@ _check_protocol() { _tag_gist() { # if no tag is given, show gist list with tags if [[ -z $* ]]; then - display=tag _print_records + display=tag _print_entries # if user want to change tags of a gist elif _gist_id $1 &>/dev/null; then _show_detail $1 | sed 3,6d && echo @@ -799,7 +810,7 @@ _tag_gist() { # if user want to filter gists with given tags else local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /[[:space:]]|/g; s/\./[^ ]/g' <<<"$@") )" - hint=false mark=${INPUT:+.} display=tag _print_records | grep --color=always -E "$pattern" + hint=false mark=${INPUT:+.} display=tag _print_entries | grep --color=always -E "$pattern" fi } @@ -850,7 +861,7 @@ _pin_tags() { # show languages of files in gists _gists_with_languages() { local pattern="($(sed -E 's/([^ ]+)/#\1/g; s/ /|/g' <<<"$@"))" - display=language _print_records | grep --color=always -Ei "$pattern" + display=language _print_entries | grep --color=always -Ei "$pattern" } _gists_with_range() { @@ -860,7 +871,7 @@ _gists_with_range() { local maximum=$(sed -Ene "/^${prefix:-[^s]}/ p" $INDEX | wc -l) local range=$(sed -Ee "s/s//g; s/^-/1-/; s/-$/-$maximum/; s/-/ /" <<< "$*") INPUT=$(seq $range | sed -e "s/^/p?$prefix/") - hint=false _print_records + hint=false _print_entries } _access_last_index() { @@ -873,11 +884,11 @@ if [[ $init ]]; then _update_gists; exit 0; fi shopt -s extglob case "$1" in "") - _print_records ;; + mark=[^s]; _print_entries ;; star | s) - mark=s; _print_records ;; + mark=s; _print_entries $2 ;; all | a) - mark=.; _print_records ;; + mark=[^s]; _print_entries $1 ;; fetch | f) [[ $2 =~ ^(s|star)$ ]] && mark=s || mark=[^s] _update_gists ;; -- cgit v1.2.3-70-g09d2