From d382fca5247d9c1e2b381fc84bd08e0214a45dbf Mon Sep 17 00:00:00 2001 From: Arinerron Date: Tue, 12 Jun 2018 23:09:55 -0700 Subject: [PATCH] Dynamically adjust stats menu size --- onionr/onionr.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/onionr/onionr.py b/onionr/onionr.py index d3d7ee4f..688193ed 100755 --- a/onionr/onionr.py +++ b/onionr/onionr.py @@ -253,7 +253,7 @@ class Onionr: def getCommands(self): return self.cmds - + def getWebPassword(self): return config.get('client')['client_hmac'] @@ -637,16 +637,24 @@ class Onionr: # pre-processing maxlength = 0 + width = self.getConsoleWidth() for key, val in messages.items(): if not (type(val) is bool and val is True): maxlength = max(len(key), maxlength) + prewidth = maxlength + len(' | ') + groupsize = width - prewidth - len('[+] ') # generate stats table logger.info(colors['title'] + 'Onionr v%s Statistics' % ONIONR_VERSION + colors['reset']) logger.info(colors['border'] + '-' * (maxlength + 1) + '+' + colors['reset']) for key, val in messages.items(): if not (type(val) is bool and val is True): - logger.info(colors['key'] + str(key).rjust(maxlength) + colors['reset'] + colors['border'] + ' | ' + colors['reset'] + colors['val'] + str(val) + colors['reset']) + val = [str(val)[i:i + groupsize] for i in range(0, len(str(val)), groupsize)] + + logger.info(colors['key'] + str(key).rjust(maxlength) + colors['reset'] + colors['border'] + ' | ' + colors['reset'] + colors['val'] + str(val.pop(0)) + colors['reset']) + + for value in val: + logger.info(' ' * maxlength + colors['border'] + ' | ' + colors['reset'] + colors['val'] + str(value) + colors['reset']) else: logger.info(colors['border'] + '-' * (maxlength + 1) + '+' + colors['reset']) logger.info(colors['border'] + '-' * (maxlength + 1) + '+' + colors['reset']) @@ -683,6 +691,21 @@ class Onionr: except Exception: return None + def getConsoleWidth(self): + ''' + Returns an integer, the width of the terminal/cmd window + ''' + + columns = 80 + + try: + columns = int(os.popen('stty size', 'r').read().split()[1]) + except: + # if it errors, it's probably windows, so default to 80. + pass + + return columns + def addFile(self): ''' Adds a file to the onionr network