Browse Source

partial

master
Raphael Roberts 7 years ago
parent
commit
7f01bfc739
  1. 14
      ext_open/ext_open.py
  2. 7
      ext_open/opener.py
  3. 62
      ext_open/util.py

14
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

7
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))

62
ext_open/util.py

@ -33,6 +33,24 @@ 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):
@ -50,11 +68,11 @@ 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:
@ -64,14 +82,40 @@ if r_end:
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())
Loading…
Cancel
Save