diff options
-rw-r--r-- | cmd2/cmd2.py | 62 | ||||
-rw-r--r-- | cmd2/table_creator.py | 24 |
2 files changed, 46 insertions, 40 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 392084bb..d479e484 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -130,6 +130,10 @@ from .rl_utils import ( rl_warning, vt100_support, ) +from .table_creator import ( + Column, + SimpleTable, +) from .utils import ( Settable, get_defining_class, @@ -3558,8 +3562,8 @@ class Cmd(cmd.Cmd): if cmds: self.poutput(header) if self.ruler: - header_width = ansi.widest_line(header) - self.poutput(self.ruler * header_width) + divider = utils.align_left('', fill_char=self.ruler, width=ansi.widest_line(header)) + self.poutput(divider) self.columnize(cmds, maxcol - 1) self.poutput() @@ -3676,25 +3680,27 @@ class Cmd(cmd.Cmd): if not verbose: self.print_topics(header, cmds, 15, 80) else: - self.poutput(f'{header}') - widest = 0 - # measure the commands - for command in cmds: - width = ansi.style_aware_wcswidth(command) - if width > widest: - widest = width - # add a 4-space pad - widest += 4 - if widest < 20: - widest = 20 - - if self.ruler: - ruler_line = utils.align_left('', width=80, fill_char=self.ruler) - self.poutput(f'{ruler_line}') + # Find the widest command + widest = max([ansi.style_aware_wcswidth(command) for command in cmds]) + + # Define the topic table + name_column = Column('', width=max(widest, 20)) + desc_column = Column('', width=80) + + divider_char = self.ruler if self.ruler else None + topic_table = SimpleTable([name_column, desc_column], divider_char=divider_char) + + # Build the topic table + table_str_buf = io.StringIO() + if header: + table_str_buf.write(header + "\n") + + divider = topic_table.generate_divider() + if divider: + table_str_buf.write(divider + "\n") # Try to get the documentation string for each command topics = self.get_help_topics() - for command in cmds: cmd_func = self.cmd_func(command) doc: Optional[str] @@ -3721,10 +3727,8 @@ class Cmd(cmd.Cmd): doc = cmd_func.__doc__ # Attempt to locate the first documentation block - if not doc: - doc_block = [''] - else: - doc_block = [] + cmd_desc = '' + if doc: found_first = False for doc_line in doc.splitlines(): stripped_line = doc_line.strip() @@ -3734,15 +3738,18 @@ class Cmd(cmd.Cmd): if found_first: break elif stripped_line: - doc_block.append(stripped_line) + if found_first: + cmd_desc += "\n" + cmd_desc += stripped_line found_first = True elif found_first: break - for doc_line in doc_block: - self.poutput(f'{command: <{widest}}{doc_line}') - command = '' - self.poutput() + # Add this command to the table + table_row = topic_table.generate_data_row([command, cmd_desc]) + table_str_buf.write(table_row + '\n') + + self.poutput(table_str_buf.getvalue()) shortcuts_parser = DEFAULT_ARGUMENT_PARSER(description="List available shortcuts") @@ -4131,7 +4138,6 @@ class Cmd(cmd.Cmd): If pyscript is None, then this function runs an interactive Python shell. Otherwise, it runs the pyscript file. - :param args: Namespace of args on the command line :param pyscript: optional path to a pyscript file to run. This is intended only to be used by do_run_pyscript() after it sets up sys.argv for the script. (Defaults to None) :return: True if running of commands should stop diff --git a/cmd2/table_creator.py b/cmd2/table_creator.py index 3df6ce37..38102a07 100644 --- a/cmd2/table_creator.py +++ b/cmd2/table_creator.py @@ -604,20 +604,20 @@ class SimpleTable(TableCreator): header = self.generate_row(inter_cell=self.inter_cell) header_buf.write(header) - # Create the divider if necessary - if self.divider_char is not None: - total_width = self.total_width() - divider_char_width = ansi.style_aware_wcswidth(self.divider_char) - - # Make divider as wide as table and use padding if width of - # divider_char does not divide evenly into table width. - divider = self.divider_char * (total_width // divider_char_width) - divider += SPACE * (total_width % divider_char_width) - - header_buf.write('\n') - header_buf.write(divider) + # Add the divider if necessary + divider = self.generate_divider() + if divider: + header_buf.write('\n' + divider) + return header_buf.getvalue() + def generate_divider(self) -> str: + """Generate divider row""" + if self.divider_char is None: + return '' + + return utils.align_left('', fill_char=self.divider_char, width=self.total_width()) + def generate_data_row(self, row_data: Sequence[Any]) -> str: """ Generate a data row |