diff --git a/install.sh b/install.sh index 4dd2d5b..05a548b 100644 --- a/install.sh +++ b/install.sh @@ -34,10 +34,11 @@ else sudo cp ~/.shellnotes/docs/manpages/* /usr/local/man/man1/ sudo gzip -f /usr/local/man/man1/*.1 sudo mandb >/dev/null - sudo apt install g++ >/dev/null + sudo apt install python3 sudo apt install python3-pip pip install pathlib + sudo apt update sudo chmod a+x ~/.shellnotes/util/exec/* sudo chmod +x ~/.shellnotes/.shellnotes.sh diff --git a/util/notegrep.sh b/util/notegrep.sh index e49ad9c..7db56a6 100644 --- a/util/notegrep.sh +++ b/util/notegrep.sh @@ -135,6 +135,15 @@ function notegrep() { ;; -ws | --without-string ) . ~/.shellnotes/util/notegrep/ws.sh + ;; + -m | --multiple ) + cd ~ + if [ "$(python3 ~/.shellnotes/util/notegrep/m.py $regex $notename | wc -l)" -ge 25 ]; then + python3 ~/.shellnotes/util/notegrep/m.py $regex $notename | less -R + else + python3 ~/.shellnotes/util/notegrep/m.py $regex $notename + fi + cd $DIR ;; *) echo "Invalid parameter. Proceeding in normal grep mode." @@ -151,7 +160,7 @@ function notegrep() { echo -n "Enter note name: " && read notename do_grep else - if [ $# -eq 3 ]; then + if [ $# -ge 3 ]; then export option=$1 export regex=$2 export notename=$3 @@ -174,7 +183,7 @@ function notegrep() { export option=$1 . ~/.shellnotes/util/notegrep/sf.sh ;; - -sc | --split-char | -cl | --count-lines | -sf | --show-lines | -ws | --without-string) + -sc | --split-char | -cl | --count-lines | -sf | --show-lines | -ws | --without-string | -m | --multiple) echo "Invalid use of parameter '$regex'.\nUsage:\nnotegrep [PATTERN] file...\nnotegrep [OPTION] [PATTERN] file...\nnotegrep [OPTION] file...\nnotegrep [OPTION] [PATTERN]" ;; *) diff --git a/util/notegrep/m.py b/util/notegrep/m.py new file mode 100644 index 0000000..887d34f --- /dev/null +++ b/util/notegrep/m.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +#shellnotes - m.py +#(C) Dimitris Marakomihelakis +#Released under the "All rights reserved" category. See the RIGHTS.txt file +#in /docs/github/ for its full text. + +import sys +import os.path +from os import path +import subprocess +from subprocess import check_output +import re + +class Multi(): + + def __init__(self, patterns, files): + self.patterns = patterns + self.files = files + + def check_empty(self): + if "" in self.patterns: raise IndexError #asks for direct input + + def remove_duplicates(self): + self.patterns, self.files = list(dict.fromkeys(self.patterns)), list(dict.fromkeys(self.files)) + + def remove_spaces(self): + self.patterns, self.files = [elem.strip(' ') for elem in self.patterns], [elem.strip(' ') for elem in self.files] + + def split_extras(self): #not using this for now + if len(sys.argv) == 4: + if "-i" in sys.argv[-1] or "--ignore" in sys.argv[-1]: + re.sub(r'\s(--\ignore+|-\i)', '', sys.sys.argv[-1]) #remove everything from sys.argv[-1], except "-i" or "--ignore", if it exists. + + def print_matches(self): + + for file in self.files: + try: + if file == "": return 0 + print("\n"+file if path.exists('Notes/'+file) else f"\n{file} (not found):", flush=True) + print("=" * len(file) if path.exists('Notes/'+file) else "-", flush=True) + + with open('Notes/'+file, 'r') as f: + text = f.read() + for pattern in self.patterns: + if re.search(pattern, text): + out = os.system(f"""echo -n "'\033[1;35;40m{pattern}\033[0;37;0m'": && grep -n --color=always {pattern} Notes/{file}""") + + except FileNotFoundError: pass + + +def main(): + try: + newMulti = Multi(sys.argv[1].split(','), sys.argv[2].split(',')) + newMulti.check_empty() + newMulti.remove_spaces() + newMulti.remove_duplicates() + newMulti.print_matches() + except IndexError: + patterns = input("Enter patterns, separated by a comma: ") + files = input("Enter files, separated by a comma: ") + newMulti = Multi(patterns.split(','), files.split(',')) + newMulti.remove_spaces() + newMulti.remove_duplicates() + newMulti.print_matches() + + +if __name__ == "__main__": + main()