def menu_summary(self, args): """Prints Summary of commands and sub-menu items""" menu_color = [1, 4, 34] # bold, underlined, blue prevname = None names = self.get_names() menu_items = [] maxlen = 0 main_pt = pt = PrettyTable(['MAIN']) main_pt.header = False main_pt.border = False main_pt.align = 'l' sub_pt = PrettyTable(['COMMAND', 'DESCRIPTION']) base_pt = PrettyTable(['COMMAND', 'DESCRIPTION']) current_pt = PrettyTable(['COMMAND', 'DESCRIPTION']) for table in [sub_pt, base_pt, current_pt]: table.header = False table.border = False table.align = 'l' #Sort out methods that begin with 'do_' as menu items... for name in names: if name[:3] == 'do_': if name == prevname: continue prevname = name if len(name[3:]) > maxlen: maxlen = len(name[3:]) menu_items.append(name) #Sort out menu items into: submenus, local commands, and globals for name in menu_items: cmd = str(name[3:]) cmd_method = getattr(self, name) doc = str(cmd_method.__doc__) or '' doc = doc.lstrip().splitlines()[0].strip() if hasattr(cmd_method,'__submenu__'): if hasattr(BaseMenu, name): base_pt.add_row(["\t{0}".format(markup(cmd, markups=menu_color)), markup(doc, markups=menu_color)]) else: current_pt.add_row(["\t{0}".format(markup(cmd, markups=menu_color)), markup(doc, markups=menu_color)]) else: if hasattr(BaseMenu, name): base_pt.add_row(["\t{0}".format(yellow(cmd, bold=True)), doc]) else: current_pt.add_row(["\t{0}".format(yellow(cmd, bold=True)), doc]) buf = "{0}\n".format(cyan('*** ' + self.name.upper() + ' OPTIONS ***', bold=True)) if current_pt._rows: # Add blank line for formatting, separate menu items from commands #current_pt.add_row(["\t{0}".format(yellow(" ", bold=True)), ""]) buf += "{0}\n".format(current_pt.get_string(sortby='COMMAND')) if sub_pt._rows: buf += "{0}\n".format(sub_pt) if base_pt._rows: buf += "{0}\n{1}\n".format(cyan('*** BASE COMMANDS ***', bold=True), base_pt) main_pt.add_row([buf]) self.oprint("{0}".format(main_pt))
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))
def menu_summary(self, args): """Prints Summary of commands and sub-menu items""" menu_color = [1, 4, 34] # bold, underlined, blue prevname = None names = self.get_names() menu_items = [] maxlen = 0 main_pt = pt = PrettyTable(['MAIN']) main_pt.header = False main_pt.border = False main_pt.align = 'l' sub_pt = PrettyTable(['COMMAND', 'DESCRIPTION']) base_pt = PrettyTable(['COMMAND', 'DESCRIPTION']) current_pt = PrettyTable(['COMMAND', 'DESCRIPTION']) for table in [sub_pt, base_pt, current_pt]: table.header = False table.border = False table.align = 'l' #Sort out methods that begin with 'do_' as menu items... for name in names: if name[:3] == 'do_': if name == prevname: continue prevname = name if len(name[3:]) > maxlen: maxlen = len(name[3:]) menu_items.append(name) #Sort out menu items into: submenus, local commands, and globals for name in menu_items: cmd = str(name[3:]) cmd_method = getattr(self, name) doc = str(cmd_method.__doc__) or '' doc = doc.lstrip().splitlines()[0].strip() if hasattr(cmd_method, '__submenu__'): if hasattr(BaseMenu, name): base_pt.add_row([ "\t{0}".format(markup(cmd, markups=menu_color)), markup(doc, markups=menu_color) ]) else: current_pt.add_row([ "\t{0}".format(markup(cmd, markups=menu_color)), markup(doc, markups=menu_color) ]) else: if hasattr(BaseMenu, name): base_pt.add_row( ["\t{0}".format(yellow(cmd, bold=True)), doc]) else: current_pt.add_row( ["\t{0}".format(yellow(cmd, bold=True)), doc]) buf = "{0}\n".format( cyan('*** ' + self.name.upper() + ' OPTIONS ***', bold=True)) if current_pt._rows: # Add blank line for formatting, separate menu items from commands #current_pt.add_row(["\t{0}".format(yellow(" ", bold=True)), ""]) buf += "{0}\n".format(current_pt.get_string(sortby='COMMAND')) if sub_pt._rows: buf += "{0}\n".format(sub_pt) if base_pt._rows: buf += "{0}\n{1}\n".format( cyan('*** BASE COMMANDS ***', bold=True), base_pt) main_pt.add_row([buf]) self.oprint("{0}".format(main_pt))
def _completer_display(self, substitution, matches, longest_match_length): try: completer = None if self.env: completer = self.env.completer_context completer = completer or self linebuffer = readline.get_line_buffer() height, width = self._get_terminal_size() columns = int((width - 12) / longest_match_length) or 1 column_width = longest_match_length def create_table(): header = [] for x in xrange(0, columns): header.append(x) pt = PrettyTable(header) pt.header = False pt.border = False pt.align = 'l' pt.max_width = column_width return pt menu_pt = None cmd_pt = None base_pt = None menus = [""] * columns cmds = [""] * columns basecmds = [""] * columns cmd_count = 0 menu_count = 0 basecmd_count = 0 total = [] base_dir = dir(BaseMenu) for match in matches: if True: #not substitution or str(match).startswith(substitution): if not match.strip(): continue total.append(match) if match in completer.submenu_names: if menu_pt is None: menu_pt = create_table() if menu_count > columns: menu_pt.add_row(menus) menu_count = 0 menus = [""] * columns menus[menu_count] = match menu_count += 1 elif 'do_{0}'.format(match) in base_dir: if base_pt is None: base_pt = create_table() if basecmd_count >= columns: base_pt.add_row(basecmds) basecmd_count = 0 basecmds = [""] * columns basecmds[basecmd_count] = match basecmd_count += 1 else: if cmd_pt is None: cmd_pt = create_table() if cmd_count >= columns: cmd_pt.add_row(cmds) cmd_count = 0 cmds = [""] * columns cmds[cmd_count] = match cmd_count += 1 if menu_count: menu_pt.add_row(menus) if cmd_count: cmd_pt.add_row(cmds) if basecmd_count: base_pt.add_row(basecmds) cli_text = "" if substitution == completer.name: self.dprint('sub matches self.name, Inserting white space') readline.insert_text(" ") linebuffer = "{0} ".format(linebuffer) else: self.dprint('sub:"{0}" doesnt match self.name:"{1}"'.format( substitution, self.name)) cli_text = "{0}{1}".format(self.prompt, linebuffer) newline = readline.get_line_buffer() self.dprint( "\nCompleter_display():\n\tsubstitution:{0}\n\tmatches:{1}" "\n\tlongest_match_length:{2}\n\tlen total:{3}\n\torig line buffer:\"{4}\"" "\n\tsubtype:{5}\n\ttotal:{6}\n\tsubstitution:\"{7}\"\n\tcolumns:{8}" "\n\tcolumn width:{9}\n\tcurrent menu:{10}\n\tcurrent menu path from home:'{11}'" "\n\tnew line buffer:'{12}'\n\tcli text:'{13}'" "\n\tcompleter_context:{14}\n\tcompleter path from home:{15}" "\nmenu_pt:\n{16}\ncmds_pt:\n{17}\n".format( substitution, matches, longest_match_length, len(total), linebuffer, type(substitution), total, substitution, columns, column_width, self, self.path_from_home, newline, cli_text, completer, completer.path_from_home, menu_pt, cmd_pt)) self.stdout.seek(0) self.stdout.write("") self.stdout.flush() readline.redisplay() line_sep = "---------------------------------" buf = "\n" if cmd_pt: buf += yellow("\n{0}".format(cmd_pt), bold=True) if menu_pt: buf += blue("\n{0}\n{1}".format(line_sep, menu_pt), bold=True) if base_pt: buf += "\n{0}".format( cyan("{0}\n{1}".format(line_sep, base_pt))) self.oprint(buf) self.stdout.write(cli_text) self.stdout.flush() readline.redisplay() except Exception as E: self.stderr.write( red("{0}\nError in completer_display:\nerror:{1}".format( get_traceback(), E))) self.stderr.flush() finally: if self.env: self.env.completer_context = None
def _completer_display(self, substitution, matches, longest_match_length): try: completer = None if self.env: completer = self.env.completer_context completer = completer or self linebuffer = readline.get_line_buffer() height, width = self._get_terminal_size() columns = int((width - 12) / longest_match_length) or 1 column_width = longest_match_length def create_table(): header = [] for x in xrange(0, columns): header.append(x) pt = PrettyTable(header) pt.header = False pt.border = False pt.align = 'l' pt.max_width = column_width return pt menu_pt = None cmd_pt = None base_pt = None menus = [""]*columns cmds = [""]*columns basecmds = [""]*columns cmd_count = 0 menu_count = 0 basecmd_count = 0 total = [] base_dir = dir(BaseMenu) for match in matches: if True: #not substitution or str(match).startswith(substitution): if not match.strip(): continue total.append(match) if match in completer.submenu_names: if menu_pt is None: menu_pt = create_table() if menu_count > columns: menu_pt.add_row(menus) menu_count = 0 menus = [""]*columns menus[menu_count] = match menu_count += 1 elif 'do_{0}'.format(match) in base_dir: if base_pt is None: base_pt = create_table() if basecmd_count >= columns: base_pt.add_row(basecmds) basecmd_count = 0 basecmds = [""]*columns basecmds[basecmd_count] = match basecmd_count +=1 else: if cmd_pt is None: cmd_pt = create_table() if cmd_count >= columns: cmd_pt.add_row(cmds) cmd_count = 0 cmds = [""]*columns cmds[cmd_count] = match cmd_count +=1 if menu_count: menu_pt.add_row(menus) if cmd_count: cmd_pt.add_row(cmds) if basecmd_count: base_pt.add_row(basecmds) cli_text = "" if substitution == completer.name: self.dprint('sub matches self.name, Inserting white space') readline.insert_text(" ") linebuffer = "{0} ".format(linebuffer) else: self.dprint('sub:"{0}" doesnt match self.name:"{1}"' .format(substitution, self.name)) cli_text = "{0}{1}".format(self.prompt, linebuffer) newline = readline.get_line_buffer() self.dprint("\nCompleter_display():\n\tsubstitution:{0}\n\tmatches:{1}" "\n\tlongest_match_length:{2}\n\tlen total:{3}\n\torig line buffer:\"{4}\"" "\n\tsubtype:{5}\n\ttotal:{6}\n\tsubstitution:\"{7}\"\n\tcolumns:{8}" "\n\tcolumn width:{9}\n\tcurrent menu:{10}\n\tcurrent menu path from home:'{11}'" "\n\tnew line buffer:'{12}'\n\tcli text:'{13}'" "\n\tcompleter_context:{14}\n\tcompleter path from home:{15}" "\nmenu_pt:\n{16}\ncmds_pt:\n{17}\n" .format(substitution, matches, longest_match_length, len(total), linebuffer, type(substitution), total, substitution, columns, column_width, self, self.path_from_home, newline, cli_text, completer, completer.path_from_home, menu_pt, cmd_pt)) self.stdout.seek(0) self.stdout.write("") self.stdout.flush() readline.redisplay() line_sep = "---------------------------------" buf = "\n" if cmd_pt: buf += yellow("\n{0}".format(cmd_pt), bold=True) if menu_pt: buf += blue("\n{0}\n{1}".format(line_sep, menu_pt), bold=True) if base_pt: buf += "\n{0}".format(cyan("{0}\n{1}".format(line_sep, base_pt))) self.oprint(buf) self.stdout.write(cli_text) self.stdout.flush() readline.redisplay() except Exception as E: self.stderr.write(red("{0}\nError in completer_display:\nerror:{1}" .format(get_traceback(), E))) self.stderr.flush() finally: if self.env: self.env.completer_context = None