diff options
author | Carlo Zancanaro <carlo@zancanaro.id.au> | 2022-09-04 14:18:02 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@zancanaro.id.au> | 2022-09-24 21:19:23 +1000 |
commit | e031af6e5e8324fe4cda66d9597904040b17ca80 (patch) | |
tree | 0917439655016b088ffe10ae76f85c7e97f1f621 /network/browser.py | |
parent | 833f87ebba90e6c2d3cb386b2d51c3d113d98a54 (diff) |
Vendor the "simple-network" library
Diffstat (limited to 'network/browser.py')
-rw-r--r-- | network/browser.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/network/browser.py b/network/browser.py new file mode 100644 index 0000000..faed1da --- /dev/null +++ b/network/browser.py @@ -0,0 +1,52 @@ +import curses +import os +from six.moves import input +import sys +from threading import Thread + +curses.setupterm(os.environ['TERM']) +def clear_terminal(): + f = sys.stdout + try: + f = f.buffer + except AttributeError: + pass + f.write(curses.tigetstr('clear')) + f.flush() + +class ConsoleServerBrowser(object): + def __init__(self, client): + self.client = client + self.result = None + self.services = [] + + def read_input(self): + while self.result is None: + line = input() + try: + self.result = int(line) + except ValueError: + pass + self.client.mainloop.quit() + + def update(self, services): + self.services = services + + # print the new output + output = '\n'.join( + '{0}: {name} @ {host} ({address}:{port})'.format(i, **service) + for i, service in enumerate(services, 1) + ) + clear_terminal() + print(output) + + def run(self, client_run): + t = Thread(target=self.read_input) + t.setDaemon(True) + t.start() + + client_run() + + service = self.services[self.result - 1] + return service['target'] + |