Skip to content

Commit 2ef7b9a

Browse files
Refactored cmd2 to better account for our use of Rich. (#1477)
* Added string_utils.py which contains all string utility functions. This includes quoting and alignment functions from utils.py. This also includes style-related functions from ansi.py. * Added colors.py which contains a StrEnum of all color names supported by Rich. * Added styles.py which contains a StrEnum of all cmd2-specific style names and their respective style definitions. * Moved string styling functionality from ansi.py to string_utils.py. * Removed all text style Enums from ansi.py in favor of Rich styles. * Renamed ansi.py to terminal_utils.py to reflect the functions left in it. * Removed table_creator.py in favor of Rich tables. * Renamed rich_utils.allow_style to rich_utils.ALLOW_STYLE to be consistent with our other global settings. --------- Co-authored-by: Todd Leonhardt <[email protected]>
1 parent db2654c commit 2ef7b9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1235
-4794
lines changed

.github/CONTRIBUTING.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ Nearly all project configuration, including for dependencies and quality tools i
6060

6161
See the `dependencies` list under the `[project]` heading in [pyproject.toml](../pyproject.toml).
6262

63-
| Prerequisite | Minimum Version | Purpose |
64-
| --------------------------------------------------- | --------------- | -------------------------------------- |
65-
| [python](https://www.python.org/downloads/) | `3.10` | Python programming language |
66-
| [pyperclip](https://github.com/asweigart/pyperclip) | `1.8` | Cross-platform clipboard functions |
67-
| [wcwidth](https://pypi.python.org/pypi/wcwidth) | `0.2.10` | Measure the displayed width of unicode |
63+
| Prerequisite | Minimum Version | Purpose |
64+
| ---------------------------------------------------------- | --------------- | ------------------------------------------------------ |
65+
| [python](https://www.python.org/downloads/) | `3.10` | Python programming language |
66+
| [pyperclip](https://github.com/asweigart/pyperclip) | `1.8` | Cross-platform clipboard functions |
67+
| [rich](https://github.com/Textualize/rich) | `14.1.0` | Add rich text and beautiful formatting in the terminal |
68+
| [rich-argparse](https://github.com/hamdanal/rich-argparse) | `1.7.1` | A rich-enabled help formatter for argparse |
6869

6970
> `macOS` and `Windows` each have an extra dependency to ensure they have a viable alternative to
7071
> [readline](https://tiswww.case.edu/php/chet/readline/rltop.html) available.
7172
73+
> Python 3.10 depends on [backports.strenum](https://github.com/clbarnes/backports.strenum) to use
74+
> the `enum.StrEnum` class introduced in Python 3.11.
75+
7276
#### Additional prerequisites to build and publish cmd2
7377

7478
See the `build` list under the `[dependency-groups]` heading in [pyproject.toml](../pyproject.toml)

CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
- No longer setting parser's `prog` value in `with_argparser()` since it gets set in
66
`Cmd._build_parser()`. This code had previously been restored to support backward
77
compatibility in `cmd2` 2.0 family.
8+
- Removed table_creator.py in favor of `Rich` tables.
9+
- Moved string styling functionality from ansi.py to string_utils.py.
10+
- Moved all string-related functions from utils.py to string_utils.py.
11+
- Removed all text style Enums from ansi.py in favor of `Rich` styles.
12+
- Renamed ansi.py to terminal_utils.py to reflect the functions left in it.
813

914
- Enhancements
1015
- Simplified the process to set a custom parser for `cmd2's` built-in commands. See
1116
[custom_parser.py](https://github.com/python-cmd2/cmd2/blob/main/examples/custom_parser.py)
1217
example for more details.
13-
1418
- Added `Cmd.macro_arg_complete()` which tab completes arguments to a macro. Its default
1519
behavior is to perform path completion, but it can be overridden as needed.
16-
1720
- All print methods (`poutput()`, `perror()`, `ppaged()`, etc.) have the ability to print Rich
1821
objects.
22+
- Added string_utils.py which contains all string utility functions. This includes quoting and
23+
alignment functions from utils.py. This also includes style-related functions from ansi.py.
24+
This also includes style-related functions from ansi.py.
25+
- Added colors.py which contains a StrEnum of all color names supported by Rich.
26+
- Added styles.py which contains a StrEnum of all cmd2-specific style names and their respective
27+
style definitions.
1928

2029
- Bug Fixes
2130
- No longer redirecting `sys.stdout` if it's a different stream than `self.stdout`. This fixes

cmd2/__init__.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@
99
from . import (
1010
plugin,
1111
rich_utils,
12-
)
13-
from .ansi import (
14-
Bg,
15-
Cursor,
16-
EightBitBg,
17-
EightBitFg,
18-
Fg,
19-
RgbBg,
20-
RgbFg,
21-
TextStyle,
22-
style,
12+
string_utils,
2313
)
2414
from .argparse_completer import set_default_ap_completer_type
2515
from .argparse_custom import (
@@ -30,6 +20,7 @@
3020
set_default_argument_parser_type,
3121
)
3222
from .cmd2 import Cmd
23+
from .colors import Color
3324
from .command_definition import (
3425
CommandSet,
3526
with_default_category,
@@ -53,6 +44,8 @@
5344
)
5445
from .parsing import Statement
5546
from .py_bridge import CommandResult
47+
from .string_utils import stylize
48+
from .styles import Cmd2Style
5649
from .utils import (
5750
CompletionMode,
5851
CustomCompletionSettings,
@@ -63,16 +56,6 @@
6356
__all__: list[str] = [ # noqa: RUF022
6457
'COMMAND_NAME',
6558
'DEFAULT_SHORTCUTS',
66-
# ANSI Exports
67-
'Cursor',
68-
'Bg',
69-
'Fg',
70-
'EightBitBg',
71-
'EightBitFg',
72-
'RgbBg',
73-
'RgbFg',
74-
'TextStyle',
75-
'style',
7659
# Argparse Exports
7760
'Cmd2ArgumentParser',
7861
'Cmd2AttributeWrapper',
@@ -85,6 +68,8 @@
8568
'CommandResult',
8669
'CommandSet',
8770
'Statement',
71+
# Colors
72+
"Color",
8873
# Decorators
8974
'with_argument_list',
9075
'with_argparser',
@@ -100,6 +85,11 @@
10085
# modules
10186
'plugin',
10287
'rich_utils',
88+
'string_utils',
89+
# String Utils
90+
'stylize',
91+
# Styles,
92+
"Cmd2Style",
10393
# Utilities
10494
'categorize',
10595
'CompletionMode',

0 commit comments

Comments
 (0)