R9_Dev

To-Do TUI

Nov 8th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | Source Code | 0 0
  1. import curses, json
  2.  
  3. def main():
  4.     boards = {}
  5.     cur_board = ""
  6.     stdscr = curses.initscr()
  7.     seq = 0
  8.     rows, cols = stdscr.getmaxyx()
  9.     init_text = "To-Do TUI App(Made for GHW 2024: Career)"
  10.     stdscr.addstr(0, int(cols/2)-len(init_text), init_text)
  11.     stdscr.getch(rows-1, 0)
  12.     stdscr.refresh()
  13.     while 1:
  14.         stdscr.clear()
  15.         stdscr.addstr(0, 0, "Select a board:")
  16.         i = 0
  17.         for x in boards.keys():
  18.             stdscr.addstr(i+1, 0, x)
  19.             i += 1
  20.         text = stdscr.getstr(rows-1, 0).decode()
  21.         txt = text.split(" ")
  22.         prompt = txt.pop(0)
  23.         if prompt in boards.keys():
  24.             cur_board = text
  25.             stdscr.clear()
  26.             stdscr.refresh()
  27.             while True:
  28.                 stdscr.clear()
  29.                 for i in range(len(boards[cur_board])):
  30.                     stdscr.addstr(i, 0, f"{i+1}. {boards[cur_board][i]}")
  31.                 text = stdscr.getstr(rows-1, 0).decode()
  32.                 if text != "":
  33.                     text = text.split(" ")
  34.                     prompt = text.pop(0)
  35.                     if prompt.lower() == "add":
  36.                         txt = " ".join(text)
  37.                         boards[cur_board].append(txt)
  38.                     if prompt.lower() == "rem":
  39.                         boards[cur_board].pop(int(text[0])-1)
  40.                     if prompt.lower() == "save":
  41.                         save_data(text[0], boards)
  42.                     if prompt.lower() == "exit" or prompt.lower() == "quit":
  43.                         cur_board = text
  44.                         stdscr.clear()
  45.                         stdscr.refresh()
  46.                         break
  47.                 stdscr.refresh()
  48.         elif prompt.lower() == "create":
  49.             boards[txt[0]] = []
  50.         elif prompt.lower() == "rem":
  51.             boards.pop(txt[0])
  52.         elif prompt.lower() == "load":
  53.             boards = load_data(txt[0])
  54.         elif prompt.lower() == "exit" or prompt.lower() == "quit":
  55.             break
  56.         elif prompt.lower() == "save":
  57.             save_data(text[0], boards)
  58.         stdscr.refresh()
  59.     curses.endwin()
  60.     return 0
  61.  
  62. def save_data(filename, data):
  63.     with open(filename, "w+") as f:
  64.         f.write(json.dumps(data))
  65.  
  66. def load_data(filename):
  67.     with open(filename, "r+") as f:
  68.         return json.loads(f.read())
  69.  
  70. if __name__ == '__main__':
  71.     main()
Tags: python ncurses
Advertisement
Add Comment
Please, Sign In to add comment