def curses_get_input(stdscr: curses.window, inputwin: curses.window, msgqueue: Queue, status): to_send = "" next_char_x = 2 next_char_y = 0 inputwin.addstr(0, 0, ">>") inputwin.refresh() while status.run: curr_in = inputwin.getkey() with open("keys.log", "a") as f: f.write(f"got {curr_in}") if curr_in == "\n": msgqueue.put(to_send) to_send = "" inputwin.erase() next_char_x = next_char_y = 0 inputwin.addstr(next_char_y, next_char_x, ">>") next_char_x += 2 elif curr_in == 'KEY_BACKSPACE' or curr_in == curses.KEY_BACKSPACE or curr_in == "^?" or curr_in == "\b" or curr_in == '\x7f': with open("clientdebug.log", "a") as f: f.write(f"got key {curr_in} went into backspace case\n") if next_char_x == 2 and next_char_y == 0: continue #nothing to delete if next_char_x == 0 and next_char_y > 0: # at the start of a new line next_char_y -= 1 next_char_x = INPUT_WIDTH - 1 elif (next_char_x > 0 and next_char_y > 0) or next_char_x > 2: next_char_x -= 1 inputwin.addstr(next_char_y, next_char_x, " ") inputwin.move(next_char_y, next_char_x) to_send = to_send[:-1] else: to_send += curr_in inputwin.addstr(next_char_y, next_char_x, curr_in) next_char_x += 1 if next_char_x == INPUT_WIDTH: next_char_x = 0 next_char_y += 1 inputwin.refresh()
def main(scr: curses.window): mode = "movement" for n, pair in enumerate(colors): curses.init_pair(n + 1, *pair) y, x = 0, 0 while True: # maxy/x maxy, maxx = [n - 1 for n in scr.getmaxyx()] maxy -= 1 # output mode scr.move(maxy + 1, 1) scr.clrtobot() scr.addstr(maxy + 1, 1, mode) # move cursor to proper position scr.move(y, x) # get user input key = scr.getch() # try: y, x, scr, mode = modes[mode](key, mode, y, x, maxy, maxx, scr)
def chat_page(screen: curses.window): global running user_typing = False read_str = "" rendered_display_chats = 0 display_chats: List[str] = [] while running: key = screen.getch() if key == curses.KEY_MOUSE: _, mx, my, *_ = curses.getmouse() if my == center(screen)[0] * 2 - 2: user_typing = True else: user_typing = False elif key == 27 or key == curses.KEY_BREAK: running = False screen.erase() start_index = -1 end_index = -(center(screen)[0] * 2 - 3) - 1 box2 = screen.subwin(3, center(screen)[1] * 2, center(screen)[0] * 2 - 3, 0) box2.box() start_render_from = rendered_display_chats rendered_display_chats = len(chats) width_limit = center(screen)[1] * 2 - 2 for chat in chats[start_render_from:]: parts = textwrap.fill(chat, width_limit).split("\n") display_chats.extend(parts) for index, msg in enumerate(display_chats[start_index:end_index:-1]): y = center(screen)[0] * 2 - 4 - index x = 0 screen.addstr(y, x, msg) if user_typing: curses.curs_set(1) screen.move( center(screen)[0] * 2 - 2, len(read_str[-(center(screen)[1] - 2) * 2:])) if key: if key == curses.KEY_ENTER or key in (10, 13): if read_str: send_this.append(read_str) read_str = "" elif key == curses.KEY_BACKSPACE or key == 8: if read_str: read_str = read_str[:-1] else: if curses.ascii.isascii(key): letter = chr(key) read_str += letter screen.move( center(screen)[0] * 2 - 2, len(read_str[-(center(screen)[1] - 2) * 2:])) else: screen.move(0, center(screen)[1] * 2 - 1) if key == ord("q"): running = False elif key == ord("w") or key == curses.KEY_ENTER or key in (10, 18): user_typing = True screen.addstr( center(screen)[0] * 2 - 2, 1, read_str[-(center(screen)[1] - 2) * 2 - 1:]) screen.refresh()