1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #!/usr/bin/env python3
- import os, sys, re, argparse
- import curses
- FILELEN = None
- def quit(win):
- curses.nocbreak()
- win.keypad(False)
- curses.echo()
- curses.endwin()
- sys.exit(os.EX_OK)
- def wrapped_addstr(sy, sx, width, win, s):
- words = re.split(r"\s", s)
- win.addstr(sy, sx, '')
- for word in words:
- (y, x) = win.getyx()
- if len(word) + 1 + x <= width:
- win.addstr(word+' ')
- else:
- win.addstr(y+1, sx, word+' ')
- return y
- def remove_first_line(path):
- with open(path, "r") as f:
- lines = f.read().splitlines(True)
- with open(path, 'w') as f:
- f.writelines(lines[1:])
- def append_line(path, s):
- with open(path, "a") as f:
- f.write(s)
- def main_loop(args, win):
- global FILELEN
- win.clear()
- (wy, wx) = win.getmaxyx()
- with open(args['input'], 'r') as f:
- lines = f.readlines()
- if not FILELEN:
- FILELEN = len(lines)
- if len(lines) > 0:
- win.addstr(1, 1, f'line {FILELEN - len(lines)}/{FILELEN} <- nay | yay -> \'q\' to quit')
- wrapped_addstr(3, 1, wx-2, win, lines[0])
- char = win.getch()
- if char == curses.KEY_LEFT:
- remove_first_line(args['input'])
- elif char == curses.KEY_RIGHT:
- print('right')
- remove_first_line(args['input'])
- append_line(args['output'], lines[0])
- elif char == ord('q'):
- quit(win)
- else:
- os.remove(args['input'])
- quit(win)
- main_loop(args, win)
- if __name__ == '__main__':
- parser = argparse.ArgumentParser(description="tool for reviewing lines of a file")
- parser.add_argument("input", help="file to review lines from")
- parser.add_argument("output", help="file to write approved lines to")
- args = vars(parser.parse_args())
- assert(os.path.isfile(args['input']))
- win = curses.initscr()
- win.keypad(True)
- try:
- main_loop(args, win)
- except Exception as e:
- print(e)
- quit(win)
|