def show_results(self, results=None, expected_status=0, max_width=None, printmethod=None): results = results or self.results if not max_width: max_height, max_width = get_terminal_size() self.logger.debug( green('Got terminal width: {0}'.format(max_width))) max_width = max_width or 100 output_hdr = "OUTPUT" pt = PrettyTable(['HOST', 'RES', 'TIME', output_hdr]) host_w = 0 for host in results.keys(): if len(host) > host_w: host_w = len(host) res_w = 4 time_w = 6 pad_w = len(pt.field_names) * 2 pt.align = 'l' pt.hrules = 1 pt.padding_width = 0 max_width = max_width - (host_w + res_w + time_w + pad_w) pt.max_width[output_hdr] = max_width def sort_meth(ip): try: if re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"): return struct.unpack("!L", inet_aton(ip))[0] except: pass return ip for host in sorted(results, key=sort_meth): result = self.results.get(host) output = "" for line in result.get('output'): line.rstrip() for x in xrange(0, len(line), max_width - 1): part = str('{output: <{length}}'.format( output=line[x:(x + max_width - 1)], length=max_width)) output += part status = result.get('status') if int(status) == int(expected_status): color = green else: color = red pt.add_row([ blue(host), color(result.get('status', None)), color(result.get('elapsed', None)), color(output) ]) buf = "\n{0}\n".format(pt) if printmethod: printmethod(buf) else: print buf
def show_results(self, results=None, expected_status=0, max_width=None, printmethod=None): results = results or self.results if not max_width: max_height, max_width = get_terminal_size() self.logger.debug(green('Got terminal width: {0}'.format(max_width))) max_width = max_width or 100 output_hdr = "OUTPUT" pt = PrettyTable(['HOST', 'RES', 'TIME', output_hdr]) host_w = 0 for host in results.keys(): if len(host) > host_w: host_w = len(host) res_w = 4 time_w = 6 pad_w = len(pt.field_names) * 2 pt.align = 'l' pt.hrules = 1 pt.padding_width = 0 max_width = max_width - (host_w + res_w + time_w + pad_w) pt.max_width[output_hdr] = max_width def sort_meth(ip): try: if re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"): return struct.unpack("!L", inet_aton(ip))[0] except: pass return ip for host in sorted(results, key=sort_meth): result = self.results.get(host) output = "" for line in result.get('output'): line.rstrip() for x in xrange(0, len(line), max_width - 1): part = str('{output: <{length}}'.format(output=line[x:(x + max_width - 1)], length=max_width)) output += part status = result.get('status') if int(status) == int(expected_status): color = green else: color = red pt.add_row([blue(host), color(result.get('status', None)), color(result.get('elapsed', None)), color(output)]) buf = "\n{0}\n".format(pt) if printmethod: printmethod(buf) else: print buf
def _get_terminal_size(self): ''' Attempts to get terminal size. Currently only Linux. returns (height, width) ''' try: return get_terminal_size() except: pass try: # todo Add Windows support import fcntl import struct return struct.unpack( 'hh', fcntl.ioctl(self.stdout, termios.TIOCGWINSZ, '1234')) except: return 80
def _get_terminal_size(self): ''' Attempts to get terminal size. Currently only Linux. returns (height, width) ''' try: return get_terminal_size() except: pass try: # todo Add Windows support import fcntl import struct return struct.unpack('hh', fcntl.ioctl(self.stdout, termios.TIOCGWINSZ, '1234')) except: return 80
def show_results(self, results=None, max_width=None, printmethod=None): results = results or self.results if not max_width: max_height, max_width = get_terminal_size() host_w = 24 res_w = 4 time_w = 6 max_width = max_width - (host_w + res_w + time_w) - 5 output_hdr = "OUTPUT" pt = PrettyTable(['HOST', 'RES', 'TIME', output_hdr]) pt.align = 'l' pt.hrules = 1 pt.padding_width = 0 max_width = max_width - (host_w + res_w + time_w) pt.max_width[output_hdr] = max_width def sort_meth(ip): try: if re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"): return struct.unpack("!L", inet_aton(ip))[0] except: pass return ip for host in sorted(results, key=sort_meth): result = self.results.get(host) output = "" for line in result.get('output'): line.rstrip() for x in xrange(0, len(line), max_width - 1): part = str('{output: <{length}}'.format(output=line[x:(x + max_width - 1)], length=max_width)) output += part status = result.get('status') if status == 0: color = green else: color = red pt.add_row([blue(host), color(result.get('status')), color(result.get('elapsed')), color(output)]) buf = "\n{0}\n".format(pt) if printmethod: printmethod(buf) else: print buf
def do_environment(self, args): """show current cli environment variables""" height, width = get_terminal_size() buf = ('\nCURRENT MENU CLASS:"{0}"' '\nPROMPT:"{1}"' '\nPATH FROM HOME:"{2}"' '\nSUB MENUS:"{3}"' '\nTERM HEIGHT:{4}, WIDTH:{5}' '\nCLI CONFIG JSON:\n' .format(self, self.prompt, ",".join(x.name for x in self.path_from_home), ", ".join(str(x) for x in self.submenu_names), height, width)) if not self.env: buf += "No env found?" else: buf += json.dumps(self.env.simplecli_config.__dict__, sort_keys=True, indent=4) self.oprint(yellow(buf))
def do_environment(self, args): """show current cli environment variables""" height, width = get_terminal_size() buf = ('\nCURRENT MENU CLASS:"{0}"' '\nPROMPT:"{1}"' '\nPATH FROM HOME:"{2}"' '\nSUB MENUS:"{3}"' '\nTERM HEIGHT:{4}, WIDTH:{5}' '\nCLI CONFIG JSON:\n'.format( self, self.prompt, ",".join(x.name for x in self.path_from_home), ", ".join(str(x) for x in self.submenu_names), height, width)) if not self.env: buf += "No env found?" else: buf += json.dumps(self.env.simplecli_config.__dict__, sort_keys=True, indent=4) self.oprint(yellow(buf))