diff options
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'] + |