Skip to content

Commit a94cc75

Browse files
committed
Made a simple table class to eliminate code duplication.
Removed Cmd.ruler since cmd2 no longer uses it.
1 parent c8c5ca8 commit a94cc75

4 files changed

Lines changed: 43 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ prompt is displayed.
6363
`add_alert()`. This new function is thread-safe and does not require you to acquire a mutex
6464
before calling it like the previous functions did.
6565
- Removed `Cmd.default_to_shell`.
66+
- Removed `Cmd.ruler` since `cmd2` no longer uses it.
6667
- Enhancements
6768
- New `cmd2.Cmd` parameters
6869
- **auto_suggest**: (boolean) if `True`, provide fish shell style auto-suggestions. These

cmd2/argparse_completer.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@
2525
from rich.text import Text
2626

2727
from .constants import INFINITY
28+
from .rich_utils import Cmd2SimpleTable
2829

2930
if TYPE_CHECKING: # pragma: no cover
3031
from .cmd2 import Cmd
3132

32-
from rich.box import SIMPLE_HEAD
33-
from rich.table import (
34-
Column,
35-
Table,
36-
)
33+
from rich.table import Column
3734

3835
from .argparse_custom import (
3936
ChoicesCallable,
@@ -46,7 +43,6 @@
4643
all_display_numeric,
4744
)
4845
from .exceptions import CompletionError
49-
from .styles import Cmd2Style
5046

5147
# Name of the choice/completer function argument that, if present, will be passed a dictionary of
5248
# command line tokens up through the token being completed mapped to their argparse destination name.
@@ -658,7 +654,7 @@ def _build_completion_table(self, arg_state: _ArgumentState, completions: Comple
658654
)
659655

660656
# Build the table
661-
table = Table(*rich_columns, box=SIMPLE_HEAD, show_edge=False, border_style=Cmd2Style.TABLE_BORDER)
657+
table = Cmd2SimpleTable(*rich_columns)
662658
for item in completions:
663659
table.add_row(Text.from_ansi(item.display), *item.table_data)
664660

cmd2/cmd2.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
cast,
6969
)
7070

71-
import rich.box
7271
from prompt_toolkit import (
7372
filters,
7473
print_formatted_text,
@@ -160,6 +159,7 @@
160159
Cmd2BaseConsole,
161160
Cmd2ExceptionConsole,
162161
Cmd2GeneralConsole,
162+
Cmd2SimpleTable,
163163
RichPrintKwargs,
164164
)
165165
from .styles import Cmd2Style
@@ -517,9 +517,6 @@ def __init__(
517517
# Used to keep track of whether we are redirecting or piping output
518518
self._redirecting = False
519519

520-
# Characters used to draw a horizontal rule. Should not be blank.
521-
self.ruler = "─"
522-
523520
# Set text which prints right before all of the help tables are listed.
524521
self.doc_leader = ""
525522

@@ -4185,6 +4182,15 @@ def do_help(self, args: argparse.Namespace) -> None:
41854182
self.perror(err_msg, style=None)
41864183
self.last_result = False
41874184

4185+
def _create_help_grid(self, title: str, *content: RenderableType) -> Table:
4186+
"""Create a titled grid for help headers with a ruler and optional content."""
4187+
grid = Table.grid()
4188+
grid.add_row(Text(title, style=Cmd2Style.HELP_HEADER))
4189+
grid.add_row(Rule(style=Cmd2Style.TABLE_BORDER))
4190+
for item in content:
4191+
grid.add_row(item)
4192+
return grid
4193+
41884194
def print_topics(self, header: str, cmds: Sequence[str] | None, cmdlen: int, maxcol: int) -> None: # noqa: ARG002
41894195
"""Print groups of commands and topics in columns and an optional header.
41904196
@@ -4198,12 +4204,11 @@ def print_topics(self, header: str, cmds: Sequence[str] | None, cmdlen: int, max
41984204
if not cmds:
41994205
return
42004206

4201-
# Print a row that looks like a table header.
42024207
if header:
4203-
header_grid = Table.grid()
4204-
header_grid.add_row(Text(header, style=Cmd2Style.HELP_HEADER))
4205-
header_grid.add_row(Rule(characters=self.ruler, style=Cmd2Style.TABLE_BORDER))
4206-
self.poutput(header_grid, soft_wrap=False)
4208+
self.poutput(
4209+
self._create_help_grid(header),
4210+
soft_wrap=False,
4211+
)
42074212

42084213
# Subtract 1 from maxcol to account for a one-space right margin.
42094214
maxcol = min(maxcol, ru.console_width()) - 1
@@ -4221,17 +4226,9 @@ def _print_documented_command_topics(self, header: str, cmds: Sequence[str], ver
42214226
self.print_topics(header, cmds, 15, 80)
42224227
return
42234228

4224-
# Create a grid to hold the header and the topics table
4225-
category_grid = Table.grid()
4226-
category_grid.add_row(Text(header, style=Cmd2Style.HELP_HEADER))
4227-
category_grid.add_row(Rule(characters=self.ruler, style=Cmd2Style.TABLE_BORDER))
4228-
4229-
topics_table = Table(
4229+
topic_table = Cmd2SimpleTable(
42304230
Column("Name", no_wrap=True),
42314231
Column("Description", overflow="fold"),
4232-
box=rich.box.SIMPLE_HEAD,
4233-
show_edge=False,
4234-
border_style=Cmd2Style.TABLE_BORDER,
42354232
)
42364233

42374234
# Try to get the documentation string for each command
@@ -4268,10 +4265,12 @@ def _print_documented_command_topics(self, header: str, cmds: Sequence[str], ver
42684265
cmd_desc = strip_doc_annotations(doc) if doc else ''
42694266

42704267
# Add this command to the table
4271-
topics_table.add_row(command, cmd_desc)
4268+
topic_table.add_row(command, cmd_desc)
42724269

4273-
category_grid.add_row(topics_table)
4274-
self.poutput(category_grid, soft_wrap=False)
4270+
self.poutput(
4271+
self._create_help_grid(header, topic_table),
4272+
soft_wrap=False,
4273+
)
42754274
self.poutput()
42764275

42774276
def render_columns(self, str_list: Sequence[str] | None, display_width: int = 80) -> str:
@@ -4560,14 +4559,10 @@ def do_set(self, args: argparse.Namespace) -> None:
45604559
# Show all settables
45614560
to_show = list(self.settables.keys())
45624561

4563-
# Define the table structure
4564-
settable_table = Table(
4562+
settable_table = Cmd2SimpleTable(
45654563
Column("Name", no_wrap=True),
45664564
Column("Value", overflow="fold"),
45674565
Column("Description", overflow="fold"),
4568-
box=rich.box.SIMPLE_HEAD,
4569-
show_edge=False,
4570-
border_style=Cmd2Style.TABLE_BORDER,
45714566
)
45724567

45734568
# Build the table and populate self.last_result

cmd2/rich_utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
TypedDict,
1111
)
1212

13+
from rich.box import SIMPLE_HEAD
1314
from rich.console import (
1415
Console,
1516
ConsoleRenderable,
@@ -29,7 +30,10 @@
2930
from rich.theme import Theme
3031
from rich_argparse import RichHelpFormatter
3132

32-
from .styles import DEFAULT_CMD2_STYLES
33+
from .styles import (
34+
DEFAULT_CMD2_STYLES,
35+
Cmd2Style,
36+
)
3337

3438
# Matches ANSI SGR (Select Graphic Rendition) sequences for text styling.
3539
# \x1b[ - the CSI (Control Sequence Introducer)
@@ -395,6 +399,19 @@ def __init__(self, *, file: IO[str] | None = None) -> None:
395399
)
396400

397401

402+
class Cmd2SimpleTable(Table):
403+
"""A clean, lightweight Rich Table tailored for cmd2's internal use."""
404+
405+
def __init__(self, *headers: Column | str) -> None:
406+
"""Cmd2SimpleTable initializer."""
407+
super().__init__(
408+
*headers,
409+
box=SIMPLE_HEAD,
410+
show_edge=False,
411+
border_style=Cmd2Style.TABLE_BORDER,
412+
)
413+
414+
398415
def console_width() -> int:
399416
"""Return the width of the console."""
400417
return Console().width

0 commit comments

Comments
 (0)