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