Build a rich.Table for completion results if applicable.
(self, arg_state: _ArgumentState, completions: Completions)
| 601 | ) |
| 602 | |
| 603 | def _build_completion_table(self, arg_state: _ArgumentState, completions: Completions) -> Completions: |
| 604 | """Build a rich.Table for completion results if applicable.""" |
| 605 | # Verify integrity of completion data |
| 606 | self._validate_table_data(arg_state, completions) |
| 607 | |
| 608 | table_columns = cast( |
| 609 | Sequence[str | Column] | None, |
| 610 | arg_state.action.get_table_columns(), # type: ignore[attr-defined] |
| 611 | ) |
| 612 | |
| 613 | # Skip table generation if results are outside thresholds or no columns are defined |
| 614 | if ( |
| 615 | len(completions) < 2 |
| 616 | or len(completions) > self._cmd2_app.max_completion_table_items |
| 617 | or table_columns is None |
| 618 | ): # fmt: skip |
| 619 | return completions |
| 620 | |
| 621 | # If a metavar was defined, use that instead of the dest field |
| 622 | destination = arg_state.action.metavar or arg_state.action.dest |
| 623 | |
| 624 | # Handle case where metavar was a tuple |
| 625 | if isinstance(destination, tuple): |
| 626 | # Figure out what string in the tuple to use based on how many of the arguments have been completed. |
| 627 | # Use min() to avoid going passed the end of the tuple to support nargs being ZERO_OR_MORE and |
| 628 | # ONE_OR_MORE. In those cases, argparse limits metavar tuple to 2 elements but we may be completing |
| 629 | # the 3rd or more argument here. |
| 630 | destination = destination[min(len(destination) - 1, arg_state.count)] |
| 631 | |
| 632 | # Build header row |
| 633 | rich_columns: list[Column] = [] |
| 634 | rich_columns.append( |
| 635 | Column( |
| 636 | destination.upper(), |
| 637 | justify="right" if completions.numeric_display else "left", |
| 638 | no_wrap=True, |
| 639 | ) |
| 640 | ) |
| 641 | rich_columns.extend( |
| 642 | column if isinstance(column, Column) else Column(column, overflow="fold") for column in table_columns |
| 643 | ) |
| 644 | |
| 645 | # Build the table |
| 646 | table = Cmd2SimpleTable(*rich_columns) |
| 647 | for item in completions: |
| 648 | table.add_row(Text.from_ansi(item.display), *item.table_data) |
| 649 | |
| 650 | return dataclasses.replace( |
| 651 | completions, |
| 652 | table=table, |
| 653 | ) |
| 654 | |
| 655 | def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: int, tokens: Sequence[str]) -> Completions: |
| 656 | """Supports cmd2's help command in the completion of subcommand names. |
no test coverage detected