Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python3
from argparse import ArgumentParser
from pathlib import Path
from textwrap import indent
import curses
import unicodedata
FOLDER_OPEN = unicodedata.lookup('Open File Folder')
FOLDER_CLOSED = unicodedata.lookup('File Folder')
DOCUMENT = unicodedata.lookup('Page Facing Up')
PAD = ' '
PREPAD = ' '
def exit_on_ctrl_c(f):
'Decorator to nicely exit on Control-C'
def f_inner(*args, **kargs):
f(*args, **kargs)
except KeyboardInterrupt:
return f_inner
def python_tree(directory, level, current=0):
'Make a unicode tree'
results = ''
for f in sorted(directory.iterdir()):
results += PAD
results += DOCUMENT if not f.is_dir() else FOLDER_OPEN if current < level else FOLDER_CLOSED
results += f' {}\n'
if f.is_dir() and current < level:
results += indent(python_tree(f, level, current+1), PAD)
return results
parser = ArgumentParser(description='Watch for files')
parser.add_argument('-L', type=int, default=1,
help='Depth to print')
args = parser.parse_args()
def main(stdscr):
curses.halfdelay(5) # 5/10 of a second refresh
stdscr.addstr(1, 0, PREPAD + FOLDER_OPEN + ' ' + Path('.').resolve().name)
stdscr.addstr(2, 0, indent(python_tree(Path('.'), args.L), PREPAD))
stdscr.addstr(curses.LINES - 2, 0, "Press a number or arrow keys to change levels shown.")
stdscr.addstr(curses.LINES - 1, 0, "Press any other key or Control-C to exit.")
new_l = stdscr.getkey()
if new_l in ('KEY_UP', 'KEY_LEFT'):
new_l = max(args.L - 1, 0)
elif new_l in ('KEY_DOWN', 'KEY_RIGHT'):
new_l = args.L + 1
args.L = int(new_l)
except curses.error:
except ValueError:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.