yaynay.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. import os, sys, re, argparse
  3. import curses
  4. FILELEN = None
  5. def quit(win):
  6. curses.nocbreak()
  7. win.keypad(False)
  8. curses.echo()
  9. curses.endwin()
  10. sys.exit(os.EX_OK)
  11. def wrapped_addstr(sy, sx, width, win, s):
  12. words = re.split(r"\s", s)
  13. win.addstr(sy, sx, '')
  14. for word in words:
  15. (y, x) = win.getyx()
  16. if len(word) + 1 + x <= width:
  17. win.addstr(word+' ')
  18. else:
  19. win.addstr(y+1, sx, word+' ')
  20. return y
  21. def remove_first_line(path):
  22. with open(path, "r") as f:
  23. lines = f.read().splitlines(True)
  24. with open(path, 'w') as f:
  25. f.writelines(lines[1:])
  26. def append_line(path, s):
  27. with open(path, "a") as f:
  28. f.write(s)
  29. def main_loop(args, win):
  30. global FILELEN
  31. win.clear()
  32. (wy, wx) = win.getmaxyx()
  33. with open(args['input'], 'r') as f:
  34. lines = f.readlines()
  35. if not FILELEN:
  36. FILELEN = len(lines)
  37. if len(lines) > 0:
  38. win.addstr(1, 1, f'line {FILELEN - len(lines)}/{FILELEN} <- nay | yay -> \'q\' to quit')
  39. wrapped_addstr(3, 1, wx-2, win, lines[0])
  40. char = win.getch()
  41. if char == curses.KEY_LEFT:
  42. remove_first_line(args['input'])
  43. elif char == curses.KEY_RIGHT:
  44. print('right')
  45. remove_first_line(args['input'])
  46. append_line(args['output'], lines[0])
  47. elif char == ord('q'):
  48. quit(win)
  49. else:
  50. os.remove(args['input'])
  51. quit(win)
  52. main_loop(args, win)
  53. if __name__ == '__main__':
  54. parser = argparse.ArgumentParser(description="tool for reviewing lines of a file")
  55. parser.add_argument("input", help="file to review lines from")
  56. parser.add_argument("output", help="file to write approved lines to")
  57. args = vars(parser.parse_args())
  58. assert(os.path.isfile(args['input']))
  59. win = curses.initscr()
  60. win.keypad(True)
  61. try:
  62. main_loop(args, win)
  63. except Exception as e:
  64. print(e)
  65. quit(win)