0

I am trying to write a simple sockets-based chat application in python, and am making a client with curses.

The issue I have is that you may receive messages longer than the window width, and in this case the text wraps like expected, but the next message is written on the next line overwriting the previous one wrapped to that line.

Here is the related code:

def receive():
  global recv_queue
  raw_data = sock.recv(1024)
  raw_data = json.loads(raw_data)
  recv_queue.append(raw_data["TMSG"] + "\n")


def mainloop(stdscr):
  global recv_queue
  i = 1

  stdscr.clear()
  curses.cbreak()
  stdscr.keypad(1)
  stdscr.scrollok(1)
  curses.noecho()
  menu_bar = stdscr.derwin(3, curses.COLS, 0, 0)
  log_win = stdscr.derwin(curses.LINES - 6, curses.COLS, 2, 0)
  ed_win = stdscr.derwin(0, curses.COLS, curses.LINES - 4, 0)
  sub_ed_win = ed_win.derwin(2, curses.COLS - 2, 1, 1)

  box = curses.textpad.Textbox(sub_ed_win, insert_mode=True)
  while True:

    log_win.border()
    ed_win.border()
    menu_bar.box()

    menu_bar.refresh()
    log_win.refresh()
    ed_win.refresh()
    sub_ed_win.refresh()

    t = threading.Thread()
    t.run = box.edit
    t.start()

    receive()

    try:
      log_win.addstr(i, 1, recv_queue.pop().encode("utf_8"))
      i += 1
    except:
      pass


wrapper(mainloop)

The part where I start a thread for editing box can be ignored, it isn't finished yet.

SigmaOne
  • 9
  • 7

0 Answers0