clogs.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. import os
  3. import json
  4. import curses
  5. import curses.textpad
  6. import traceback
  7. import shutil
  8. from window_tree_node import WindowTreeNode, CommandWindow
  9. DEFAULT_CLOGS_LOCATION = '.local/share/strlst/default.clogs'
  10. def grab_input(window):
  11. while True:
  12. return window.getch()
  13. def read_logs(filepath):
  14. shutil.copyfile(filepath, f'{filepath}.bak')
  15. with open(filepath, 'r') as f:
  16. return json.load(f)
  17. def write_logs(filepath, logs):
  18. with open(filepath, 'w') as f:
  19. json.dump(logs, f)
  20. def main(stdscr):
  21. filepath = f'{os.getenv("HOME")}/{DEFAULT_CLOGS_LOCATION}'
  22. logs = read_logs(filepath)
  23. state = {
  24. 'stdscr': stdscr,
  25. 'logs': logs,
  26. 'filepath': filepath,
  27. 'write_callback': write_logs,
  28. 'quit': False,
  29. 'write': False
  30. }
  31. curses.use_default_colors()
  32. curses.curs_set(0)
  33. max_nl, max_nc = stdscr.getmaxyx()
  34. cmd = CommandWindow(1, max_nc, max_nl - 1, 0)
  35. root = WindowTreeNode(cmd, max_nl - 1, max_nc - 1, max_nl - 1, max_nc - 1, 0, 1, logs, active=True, display_metadata=True)
  36. while not state['quit']:
  37. # render current state
  38. cmd.render()
  39. root.render()
  40. # grab new input
  41. keypress = grab_input(root.window)
  42. # update based on input
  43. root.update(state, keypress)
  44. cmd.update(state, keypress)
  45. if __name__ == '__main__':
  46. try:
  47. curses.wrapper(main)
  48. except Exception:
  49. traceback.print_exc()