diff --git a/ext_open/ext_open.py b/ext_open/ext_open.py index af76635..0868152 100644 --- a/ext_open/ext_open.py +++ b/ext_open/ext_open.py @@ -2,7 +2,7 @@ from opts import * import searcher import opener import argparse -from util import print2d +from util import picker parser = argparse.ArgumentParser(prog='ext-open',prefix_chars=prefix_char) for opt in prefix: parser.add_argument(*opt.pop('ostring'),**opt) @@ -22,15 +22,7 @@ if __name__ == '__main__': if args.update: s.update() if args.exact: - matches = s.exeact(args.query) + matches = s.exact(args.query) else: matches = s.search(args.query,args.regex) - print2d(matches) - which = None -while not which: - try: - which = input("Which?: ") -except KeyBoardInterrupt: - quit() - - + row = picker(matches \ No newline at end of file diff --git a/ext_open/opener.py b/ext_open/opener.py index 02f488f..b152aca 100644 --- a/ext_open/opener.py +++ b/ext_open/opener.py @@ -1,16 +1,15 @@ from binaryornot.check import is_binary from config import config import os -import re import shlex import subprocess EDITOR_PATH = config.getpath('global','editor') EXT = re.compile(r'\.\w+$') def get_ext(file): - match = EXT.search(file) - if match: - return match.group(0) + ext = os.path.splitext(file)[-1] + if ext != '': + return ext def edit(path,params=[]): params = list(map(os.path.expandvars,params)) @@ -35,7 +34,7 @@ def standalone(path,params=[],windowless =False,cwd = None): cmd = cmd = subprocess.list2cmdline([path]+params).replace('"','~') subprocess.check_output(['cscript.exe',path2windowless,cmd,cwd]) else: - + subprocess.Popen([path]+params,cwd=cwd) def dependant(path,params=[],mode = 'execute',cwd = None): diff --git a/ext_open/util.py b/ext_open/util.py index f732b95..df32a48 100644 --- a/ext_open/util.py +++ b/ext_open/util.py @@ -33,7 +33,25 @@ def no_parents(paths): paths.append(path) return ret - +def abbreviate(path,n=72): + if len(path) > n: + elements = [] + b = os.path.basename(path) + while not b == '': + elements.append(b) + os.path.basename(path) + path = os.path.dirname(path) + b = os.path.basename(path) + elements.append(path) + elements = elements[::-1] + l = len(elements) + prefix = elements[:l//2] + postfix = elements[l//2:] + + while len(os.path.join(*prefix,'...',*postfix)) + else: + return path + def str_coerce(s,**kwargs): if isinstance(s,datetime.datetime): return s.strftime(kwargs['datetime_format']) @@ -41,7 +59,7 @@ def str_coerce(s,**kwargs): 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): @@ -50,28 +68,54 @@ def print2d(l,datetime_format = "%A, %B %e, %Y %H:%M:%S",seperator= ' | ',spacer except IndexError: max_col.append(len(col)) -fmt_row = '{content}' + fmt_row = '{content}' if l_end: - fmt_row = f'{l_end} ' + fmt_row -if r_end: - fmt_row = fmt_row + f' {r_end}' - + fmt_row = '{} {}'.format(l_end,fmt_row) + if r_end: + fmt_row = '{} {}'.format(fmt_row,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' + else: + row_sep = '\n' final = row_sep.join(done) if bottom: final = '\n'.join((bottom,final,bottom)) -if interactive: - if not bottom: - final += '\n' + if interactive: + if not bottom: + final += '\n' pager(final) else: return final + +def picker(rows,interactive=False): + opts = { + 'spacer':' ', + 'seperator':' ', + 'interactive': interactive, + 'bottom':'=', + 'l_end':'<', + 'r_end':'>', + } + drows = ( (i,)+row for i,row in enumerate(rows)) + print2d(drows,**opts) + which = None + while not which: + try: + which = input('Which one?: ') + except KeyboardInterrupt: + quit() + try: + which = srt_keys[int(which)] + except ValueError: + which = None + return rows[which] + +if __name__ == "__main__": + abbreviate(os.getcwd()) \ No newline at end of file