Skip to content

Commit c1af802

Browse files
committed
No longer indenting help headers and topic lists.
1 parent ed88780 commit c1af802

File tree

4 files changed

+30
-47
lines changed

4 files changed

+30
-47
lines changed

cmd2/argparse_completer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: list[str]
578578
hint_table = Table(
579579
*headers,
580580
box=SIMPLE_HEAD,
581+
show_edge=False,
581582
border_style=Cmd2Style.TABLE_BORDER,
582583
)
583584
for item in completion_items:

cmd2/cmd2.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,7 +2076,7 @@ def _display_matches_gnu_readline(
20762076
if self.formatted_completions:
20772077
if not hint_printed:
20782078
sys.stdout.write('\n')
2079-
sys.stdout.write(self.formatted_completions)
2079+
sys.stdout.write('\n' + self.formatted_completions + '\n')
20802080

20812081
# Otherwise use readline's formatter
20822082
else:
@@ -2133,7 +2133,7 @@ def _display_matches_pyreadline(self, matches: list[str]) -> None: # pragma: no
21332133
if self.formatted_completions:
21342134
if not hint_printed:
21352135
sys.stdout.write('\n')
2136-
sys.stdout.write(self.formatted_completions)
2136+
sys.stdout.write('\n' + self.formatted_completions + '\n')
21372137

21382138
# Redraw the prompt and input lines
21392139
rl_force_redisplay()
@@ -4072,12 +4072,8 @@ def do_help(self, args: argparse.Namespace) -> None:
40724072
cmds_cats, cmds_doc, cmds_undoc, help_topics = self._build_command_info()
40734073

40744074
if self.doc_leader:
4075-
# Indent doc_leader to align with the help tables.
40764075
self.poutput()
4077-
self.poutput(
4078-
ru.indent(self.doc_leader, 1),
4079-
style=Cmd2Style.HELP_LEADER,
4080-
)
4076+
self.poutput(self.doc_leader, style=Cmd2Style.HELP_LEADER)
40814077
self.poutput()
40824078

40834079
if not cmds_cats:
@@ -4122,9 +4118,6 @@ def print_topics(self, header: str, cmds: list[str] | None, cmdlen: int, maxcol:
41224118
41234119
Override of cmd's print_topics() to use Rich.
41244120
4125-
The output for both the header and the commands is indented by one space to align
4126-
with the tables printed by the `help -v` command.
4127-
41284121
:param header: string to print above commands being printed
41294122
:param cmds: list of topics to print
41304123
:param cmdlen: unused, even by cmd's version
@@ -4137,16 +4130,11 @@ def print_topics(self, header: str, cmds: list[str] | None, cmdlen: int, maxcol:
41374130
if header:
41384131
header_grid = Table.grid()
41394132
header_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4140-
header_grid.add_row(Rule(characters=self.ruler, style=Cmd2Style.TABLE_BORDER))
4141-
self.poutput(ru.indent(header_grid, 1))
4142-
4143-
# Subtract 2 from the max column width to account for the
4144-
# one-space indentation and a one-space right margin.
4145-
maxcol = min(maxcol, ru.console_width()) - 2
4133+
header_grid.add_row(Rule(characters=self.ruler))
4134+
self.poutput(header_grid)
41464135

4147-
# Print the topics in columns.
4148-
columnized_cmds = self.render_columns(cmds, maxcol)
4149-
self.poutput(ru.indent(columnized_cmds, 1))
4136+
# Subtract 1 from maxcol to account for a one-space right margin.
4137+
self.columnize(cmds, maxcol - 1)
41504138
self.poutput()
41514139

41524140
def _print_documented_command_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
@@ -4160,13 +4148,17 @@ def _print_documented_command_topics(self, header: str, cmds: list[str], verbose
41604148
self.print_topics(header, cmds, 15, 80)
41614149
return
41624150

4163-
# Indent header to align with the help tables.
4164-
self.poutput(ru.indent(header, 1), style=Cmd2Style.HELP_HEADER)
4151+
# Create a grid to hold the header and the topics table
4152+
category_grid = Table.grid()
4153+
category_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4154+
category_grid.add_row(Rule(characters=self.ruler))
4155+
41654156
topics_table = Table(
41664157
Column("Name", no_wrap=True),
41674158
Column("Description", overflow="fold"),
4168-
box=ru.TOP_AND_HEAD,
4159+
box=rich.box.SIMPLE_HEAD,
41694160
border_style=Cmd2Style.TABLE_BORDER,
4161+
show_edge=False,
41704162
)
41714163

41724164
# Try to get the documentation string for each command
@@ -4205,7 +4197,8 @@ def _print_documented_command_topics(self, header: str, cmds: list[str], verbose
42054197
# Add this command to the table
42064198
topics_table.add_row(command, cmd_desc)
42074199

4208-
self.poutput(topics_table)
4200+
category_grid.add_row(topics_table)
4201+
self.poutput(category_grid)
42094202
self.poutput()
42104203

42114204
def render_columns(self, str_list: list[str] | None, display_width: int = 80) -> str:
@@ -4486,6 +4479,7 @@ def do_set(self, args: argparse.Namespace) -> None:
44864479
Column("Description", overflow="fold"),
44874480
box=rich.box.SIMPLE_HEAD,
44884481
border_style=Cmd2Style.TABLE_BORDER,
4482+
show_edge=False,
44894483
)
44904484

44914485
# Build the table and populate self.last_result
@@ -4496,7 +4490,9 @@ def do_set(self, args: argparse.Namespace) -> None:
44964490
settable_table.add_row(param, str(settable.get_value()), settable.description)
44974491
self.last_result[param] = settable.get_value()
44984492

4493+
self.poutput()
44994494
self.poutput(settable_table)
4495+
self.poutput()
45004496

45014497
@classmethod
45024498
def _build_shell_parser(cls) -> Cmd2ArgumentParser:

cmd2/rich_utils.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
TypedDict,
99
)
1010

11-
from rich.box import Box
1211
from rich.console import (
1312
Console,
1413
ConsoleRenderable,
@@ -111,19 +110,6 @@ class RichPrintKwargs(TypedDict, total=False):
111110
new_line_start: bool
112111

113112

114-
# Custom Rich Box for tables which has a top border and a head row separator.
115-
TOP_AND_HEAD: Box = Box(
116-
" ── \n" # top
117-
" \n"
118-
" ── \n" # head_row
119-
" \n"
120-
" \n"
121-
" \n"
122-
" \n"
123-
" \n"
124-
)
125-
126-
127113
class Cmd2BaseConsole(Console):
128114
"""Base class for all cmd2 Rich consoles.
129115

tests/test_argparse_completer.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,8 @@ def test_completion_items(ac_app) -> None:
720720
line_found = False
721721
for line in ac_app.formatted_completions.splitlines():
722722
# Since the CompletionItems were created from strings, the left-most column is left-aligned.
723-
# Therefore choice_1 will begin the line (with 2 spaces for padding).
724-
if line.startswith(' choice_1') and 'A description' in line:
723+
# Therefore choice_1 will begin the line (with 1 space for padding).
724+
if line.startswith(' choice_1') and 'A description' in line:
725725
line_found = True
726726
break
727727

@@ -743,7 +743,7 @@ def test_completion_items(ac_app) -> None:
743743
for line in ac_app.formatted_completions.splitlines():
744744
# Since the CompletionItems were created from numbers, the left-most column is right-aligned.
745745
# Therefore 1.5 will be right-aligned.
746-
if line.startswith(" 1.5") and "One.Five" in line:
746+
if line.startswith(" 1.5") and "One.Five" in line:
747747
line_found = True
748748
break
749749

@@ -908,7 +908,7 @@ def test_completion_items_arg_header(ac_app) -> None:
908908
begidx = endidx - len(text)
909909

910910
complete_tester(text, line, begidx, endidx, ac_app)
911-
assert "DESC_HEADER" in normalize(ac_app.formatted_completions)[1]
911+
assert "DESC_HEADER" in normalize(ac_app.formatted_completions)[0]
912912

913913
# Test when metavar is a string
914914
text = ''
@@ -917,7 +917,7 @@ def test_completion_items_arg_header(ac_app) -> None:
917917
begidx = endidx - len(text)
918918

919919
complete_tester(text, line, begidx, endidx, ac_app)
920-
assert ac_app.STR_METAVAR in normalize(ac_app.formatted_completions)[1]
920+
assert ac_app.STR_METAVAR in normalize(ac_app.formatted_completions)[0]
921921

922922
# Test when metavar is a tuple
923923
text = ''
@@ -927,7 +927,7 @@ def test_completion_items_arg_header(ac_app) -> None:
927927

928928
# We are completing the first argument of this flag. The first element in the tuple should be the column header.
929929
complete_tester(text, line, begidx, endidx, ac_app)
930-
assert ac_app.TUPLE_METAVAR[0].upper() in normalize(ac_app.formatted_completions)[1]
930+
assert ac_app.TUPLE_METAVAR[0].upper() in normalize(ac_app.formatted_completions)[0]
931931

932932
text = ''
933933
line = f'choices --tuple_metavar token_1 {text}'
@@ -936,7 +936,7 @@ def test_completion_items_arg_header(ac_app) -> None:
936936

937937
# We are completing the second argument of this flag. The second element in the tuple should be the column header.
938938
complete_tester(text, line, begidx, endidx, ac_app)
939-
assert ac_app.TUPLE_METAVAR[1].upper() in normalize(ac_app.formatted_completions)[1]
939+
assert ac_app.TUPLE_METAVAR[1].upper() in normalize(ac_app.formatted_completions)[0]
940940

941941
text = ''
942942
line = f'choices --tuple_metavar token_1 token_2 {text}'
@@ -946,7 +946,7 @@ def test_completion_items_arg_header(ac_app) -> None:
946946
# We are completing the third argument of this flag. It should still be the second tuple element
947947
# in the column header since the tuple only has two strings in it.
948948
complete_tester(text, line, begidx, endidx, ac_app)
949-
assert ac_app.TUPLE_METAVAR[1].upper() in normalize(ac_app.formatted_completions)[1]
949+
assert ac_app.TUPLE_METAVAR[1].upper() in normalize(ac_app.formatted_completions)[0]
950950

951951

952952
def test_completion_items_descriptive_headers(ac_app) -> None:
@@ -961,7 +961,7 @@ def test_completion_items_descriptive_headers(ac_app) -> None:
961961
begidx = endidx - len(text)
962962

963963
complete_tester(text, line, begidx, endidx, ac_app)
964-
assert ac_app.CUSTOM_DESC_HEADERS[0] in normalize(ac_app.formatted_completions)[1]
964+
assert ac_app.CUSTOM_DESC_HEADERS[0] in normalize(ac_app.formatted_completions)[0]
965965

966966
# This argument did not provide a descriptive header, so it should be DEFAULT_DESCRIPTIVE_HEADERS
967967
text = ''
@@ -970,7 +970,7 @@ def test_completion_items_descriptive_headers(ac_app) -> None:
970970
begidx = endidx - len(text)
971971

972972
complete_tester(text, line, begidx, endidx, ac_app)
973-
assert DEFAULT_DESCRIPTIVE_HEADERS[0] in normalize(ac_app.formatted_completions)[1]
973+
assert DEFAULT_DESCRIPTIVE_HEADERS[0] in normalize(ac_app.formatted_completions)[0]
974974

975975

976976
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)