-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtzp.py
118 lines (86 loc) · 3.05 KB
/
tzp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import zmq
import curses
import argparse
import configparser
import threading
import time
from curses import wrapper
from client import Client
from ui import UI
def parse_args():
parser = argparse.ArgumentParser(description='Client for teezeepee')
# Please specify your username
parser.add_argument('username',
type=str,
help='Specified username')
parser.add_argument('--config-file',
type=str,
help='Default path for configuration file.')
return parser.parse_args()
def display_section(window, display):
window_lines, window_cols = window.getmaxyx()
bottom_line = window_lines - 1
window.bkgd(curses.A_NORMAL)
window.scrollok(1)
while True:
window.addstr(bottom_line, 1, display.recv_string())
window.move(bottom_line, 1)
window.scroll(1)
window.refresh()
def input_section(window, chat_sender):
window.bkgd(curses.A_NORMAL)
window.clear()
window.box()
window.refresh()
while True:
window.clear()
window.box()
window.refresh()
s = window.getstr(1, 1).decode('utf-8')
if s is not None and s != "":
chat_sender.send_string(s)
# Short pause
time.sleep(0.01)
def main(stdscr):
config_file = args.config_file if args.config_file is not None else 'tzp.cfg'
config = configparser.ConfigParser()
config.read(config_file)
config = config['default']
receiver = zmq.Context().instance().socket(zmq.PAIR)
receiver.bind("inproc://clientchat")
sender = zmq.Context().instance().socket(zmq.PAIR)
sender.connect("inproc://clientchat")
client = Client(args.username, config['server_host'],
config['chat_port'], receiver)
client.run()
show_receiver = zmq.Context().instance().socket(zmq.PAIR)
show_receiver.bind("inproc://clientdisplay")
show_sender = zmq.Context().instance().socket(zmq.PAIR)
show_sender.connect("inproc://clientdisplay")
ui = UI(config['server_host'], config['display_port'], show_sender)
ui.run()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.echo()
curses.curs_set(0)
window_height = curses.LINES
window_width = curses.COLS
divider = int(window_height * 0.5)
history_screen = stdscr.subpad(divider, window_width, 0, 0)
input_screen = stdscr.subpad(window_height - divider, window_width, divider, 0)
history_thread = threading.Thread(target=display_section, args=(history_screen, show_receiver))
history_thread.daemon = True
history_thread.start()
input_thread = threading.Thread(target=input_section, args=(input_screen, sender))
input_thread.daemon = True
input_thread.start()
history_thread.join()
input_thread.join()
if '__main__' == __name__:
try:
args = parse_args()
wrapper(main)
except KeyboardInterrupt as e:
pass
except:
raise