diff --git a/ext_open/ext_open.py b/ext_open/ext_open.py index 0175826..af76635 100644 --- a/ext_open/ext_open.py +++ b/ext_open/ext_open.py @@ -2,6 +2,7 @@ from opts import * import searcher import opener import argparse +from util import print2d parser = argparse.ArgumentParser(prog='ext-open',prefix_chars=prefix_char) for opt in prefix: parser.add_argument(*opt.pop('ostring'),**opt) @@ -16,4 +17,20 @@ for ext in exts: sub_parser.add_argument(*opt.pop('ostring'),**opt) if __name__ == '__main__': args = parser.parse_args() - print(args) \ No newline at end of file + ext = arsg.ext + s = searcher.Searcher(ext,args.case_insensitive) + if args.update: + s.update() + if args.exact: + matches = s.exeact(args.query) + else: + matches = s.search(args.query,args.regex) + print2d(matches) + which = None +while not which: + try: + which = input("Which?: ") +except KeyBoardInterrupt: + quit() + + diff --git a/ext_open/opts.py b/ext_open/opts.py index 6b7fc28..437f22c 100644 --- a/ext_open/opts.py +++ b/ext_open/opts.py @@ -12,6 +12,7 @@ prefix = [ }, { 'ostring':['+m','++update-modified-times'], + 'action':'store_true', 'help': '''updates the modified times for paths in config useful for ensuring all new files are found''', }, @@ -49,4 +50,4 @@ exe = [ {'help': 'Directory to start in', 'ostring': ['+d', '++start-in'],'metavar':'dir'}, {'ostring':['query'],'help':'Query to search for'}, {'action': 'store', 'help': 'Command-line args', 'nargs': '*', 'ostring': ['arg']}, -] \ No newline at end of file +] diff --git a/ext_open/searcher.py b/ext_open/searcher.py index 59e5bf7..1e24c14 100644 --- a/ext_open/searcher.py +++ b/ext_open/searcher.py @@ -76,6 +76,10 @@ class Searcher: else: self.cur.execute('SELECT name,fullpath FROM {} WHERE INSIDE(?,name,?)=1;'.format(self.ext),[query,self.case]) return self.cur.fetchall() + + def exact(self,query): + self.cur.execute('SELECT name,fullpath FROM {} WHERE name=?'.format(self.ext),[query]) + return self.cur.fetchall() def clean(self,backhistory): self.cur.execute('SELECT update_time FROM updates WHERE ext=? ORDER BY update_time DESC LIMIT ?;',[self.ext,backhistory+1]) @@ -92,4 +96,4 @@ if __name__ == "__main__": init_db() s = Searcher('py') s.update() - # s.update(True) \ No newline at end of file + # s.update(True) diff --git a/ext_open/util.py b/ext_open/util.py index ef93555..f732b95 100644 --- a/ext_open/util.py +++ b/ext_open/util.py @@ -1,7 +1,9 @@ from ctypes import windll - +import datetime +from pydoc import pager import os import re + # https://stackoverflow.com/a/827397 def is_admin(): return windll.shell32.IsUserAnAdmin() != 0 @@ -29,4 +31,47 @@ def no_parents(paths): if not path.startswith(el1): paths.append(path) - return ret \ No newline at end of file + return ret + + +def str_coerce(s,**kwargs): + if isinstance(s,datetime.datetime): + return s.strftime(kwargs['datetime_format']) + else: + return str(s) +def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer = ' ',bottom = '=',l_end = '|',r_end = '|',interactive = False): + l = [[str_coerce(s,datetime_format = datetime_format) for s in row] for row in l] + + max_col = [] + for row in l: + for i,col in enumerate(row): + try: + max_col[i] = max(max_col[i],len(col)) + except IndexError: + max_col.append(len(col)) + +fmt_row = '{content}' + if l_end: + fmt_row = f'{l_end} ' + fmt_row +if r_end: + fmt_row = fmt_row + f' {r_end}' + + done = [] + for row in l: + content = seperator.join(col.ljust(max_col[i],spacer if i < len(row)-1 or r_end else ' ') for i,col in enumerate(row)) + done.append(fmt_row.format(content = content)) + + if bottom: + bottom = bottom*len(done[0]) + row_sep = ('\n'+bottom+'\n') +else: + row_sep = '\n' + final = row_sep.join(done) + if bottom: + final = '\n'.join((bottom,final,bottom)) +if interactive: + if not bottom: + final += '\n' + pager(final) + else: + return final