6 from plomrogue.io_tcp import PlomSocket
8 def recv_loop(plom_socket, q):
9 for msg in plom_socket.recv():
14 def __init__(self, socket, q):
20 self.do_refresh = True
21 curses.wrapper(self.loop)
23 def log_msg(self, msg):
25 if len(self.log) > 100:
26 self.log = self.log[-100:]
28 def loop(self, stdscr):
30 def msg_into_lines_of_width(msg, width):
34 for i in range(len(msg)):
36 if x >= width or msg[i] == "\n":
45 def reset_screen_size():
46 self.rows, self.cols = stdscr.getmaxyx()
47 self.cols -= 1 # ugly TODO ncurses bug workaround, FIXME
48 self.window_width = self.cols // 2
50 def recalc_input_lines():
51 self.input_lines = msg_into_lines_of_width(input_prompt +input_,
57 lines += msg_into_lines_of_width(line, self.window_width)
59 max_y = self.rows - len(self.input_lines)
60 for i in range(len(lines)):
63 stdscr.addstr(max_y - i - 1, self.window_width, lines[i])
66 y = self.rows - len(self.input_lines)
67 for i in range(len(self.input_lines)):
68 stdscr.addstr(y, self.window_width, self.input_lines[i])
71 curses.curs_set(False) # hide cursor
79 self.do_refresh = False
88 msg = self.queue.get(block=False)
90 self.do_refresh = True
98 self.do_refresh = True
100 if key == 'KEY_RESIZE':
102 elif key == 'KEY_BACKSPACE':
104 elif key == '\n': # Return key
105 self.socket.send(input_)
109 # TODO find out why - 1 is necessary here
110 max_length = self.window_width * self.rows - len(input_prompt) - 1
111 if len(input_) > max_length:
112 input_ = input_[:max_length]
114 s = socket.create_connection(('127.0.0.1', 5000))
115 plom_socket = PlomSocket(s)
117 t = threading.Thread(target=recv_loop, args=(plom_socket, q))