forked from bpython/curtsies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
70 lines (63 loc) · 2.5 KB
/
chat.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
from __future__ import unicode_literals
"""A more realtime netcat"""
import sys
import select
import socket
from curtsies import FullscreenWindow, Input, FSArray
from curtsies.formatstring import linesplit
from curtsies.fmtfuncs import blue, red, green
class Connection(object):
def __init__(self, sock):
self.sock = sock
self.received = []
def fileno(self):
return self.sock.fileno()
def on_read(self):
self.received.append(self.sock.recv(50))
def render(self):
return linesplit(green(''.join(s.decode('latin-1') for s in self.received)), 80) if self.received else ['']
def main(host, port):
client = socket.socket()
client.connect((host, port))
client.setblocking(False)
conn = Connection(client)
keypresses = []
with FullscreenWindow() as window:
with Input() as input_generator:
while True:
a = FSArray(10, 80)
in_text = ''.join(keypresses)[:80]
a[9:10, 0:len(in_text)] = [red(in_text)]
for i, line in zip(reversed(range(2,7)), reversed(conn.render())):
a[i:i+1, 0:len(line)] = [line]
text = 'connected to %s:%d' % (host if len(host) < 50 else host[:50]+'...', port)
a[0:1, 0:len(text)] = [blue(text)]
window.render_to_terminal(a)
ready_to_read, _, _ = select.select([conn, input_generator], [], [])
for r in ready_to_read:
if r is conn:
r.on_read()
else:
e = input_generator.send(0)
if e == '<ESC>':
return
elif e == '<Ctrl-j>':
keypresses.append('\n')
client.send((''.join(keypresses)).encode('latin-1'))
keypresses = []
elif e == '<SPACE>':
keypresses.append(' ')
elif e in ('<DELETE>', '<BACKSPACE>'):
keypresses = keypresses[:-1]
elif e is not None:
keypresses.append(e)
if __name__ == '__main__':
try:
host, port = sys.argv[1:3]
except ValueError:
print('usage: python chat.py google.com 80')
print('(if you use this example, try typing')
print('GET /')
print('and then hitting enter)')
else:
main(host, int(port))