From cb464605afbbad00a8381cdfc6b0facea6424538 Mon Sep 17 00:00:00 2001 From: typebrook Date: Sun, 15 Mar 2020 11:02:55 +0800 Subject: A better grep --- gist | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gist b/gist index a85e826..4ee4ecd 100755 --- a/gist +++ b/gist @@ -276,7 +276,20 @@ _show_list() { # TODO support filenames, file contents _grep_content() { - hint=false mark=. _show_list | grep -Ei "^ *[^ ]+ [^ ]+ .*$1.*" + while read -r index url _ _ _ _ _ _ description; do + if grep --color=always -iq "$1" <<<"$description"; then + hint=false mark="$index " _show_list | grep --color=always -Ei "$1" + else + repo=$folder/$(echo $url | sed -E -e 's#.*/##') + [[ -d $repo ]] && cd $repo || continue + file=$(ls $repo | grep --color=always -Ei "$1") + content=$(grep --color=always -EHi "$1" * | head -1) + + [[ -n $file && file="$file\n" || -n $content ]] \ + && hint=false mark="$index " _show_list \ + && echo -e "$file$content\n" | sed -e 's/^/ /' + fi + done < $INDEX } # Open Github repository import page -- cgit v1.2.3-70-g09d2 From 6acb5616e69d441f19193f80973db3a27d3ab4bb Mon Sep 17 00:00:00 2001 From: typebrook Date: Mon, 16 Mar 2020 12:00:40 +0800 Subject: Update helper message --- gist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gist b/gist index 4ee4ecd..a57811d 100755 --- a/gist +++ b/gist @@ -20,7 +20,7 @@ # clean, C Clean removed gists in local # config, c [token | user | folder | auto_sync | EDITOR | action | protocol [value] ] Do configuration # user, U Get gists from a given Github user -# grep, g Grep gists by a given pattern +# grep, g Grep gists by a given pattern from description, filename or file content # push, p Push changes by git (well, better to make commit by youself) # github, G Import selected gist as a new Github repo # help, h Show this help message -- cgit v1.2.3-70-g09d2 From 159dae1e649665701a4777cbff59e9bc6ccfc44b Mon Sep 17 00:00:00 2001 From: typebrook Date: Mon, 16 Mar 2020 12:09:53 +0800 Subject: Add error message --- gist | 1 + 1 file changed, 1 insertion(+) diff --git a/gist b/gist index a57811d..439a60d 100755 --- a/gist +++ b/gist @@ -276,6 +276,7 @@ _show_list() { # TODO support filenames, file contents _grep_content() { + if [[ -z $1 ]]; then echo 'Please give a pattern' && return 1; fi while read -r index url _ _ _ _ _ _ description; do if grep --color=always -iq "$1" <<<"$description"; then hint=false mark="$index " _show_list | grep --color=always -Ei "$1" -- cgit v1.2.3-70-g09d2