Skip to content

Commit 42da841

Browse files
committed
Moved the style and color string constants into Cmd2Style and ColorName StrEnums.
1 parent 83a1946 commit 42da841

14 files changed

+356
-312
lines changed

cmd2/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
__version__ = importlib_metadata.version(__name__)
88

99
from . import (
10-
colors,
1110
plugin,
1211
rich_utils,
1312
string_utils,
14-
styles,
1513
)
1614
from .argparse_completer import set_default_ap_completer_type
1715
from .argparse_custom import (
@@ -22,6 +20,7 @@
2220
set_default_argument_parser_type,
2321
)
2422
from .cmd2 import Cmd
23+
from .colors import ColorName
2524
from .command_definition import (
2625
CommandSet,
2726
with_default_category,
@@ -46,6 +45,7 @@
4645
from .parsing import Statement
4746
from .py_bridge import CommandResult
4847
from .string_utils import stylize
48+
from .styles import Cmd2Style
4949
from .utils import (
5050
CompletionMode,
5151
CustomCompletionSettings,
@@ -68,6 +68,8 @@
6868
'CommandResult',
6969
'CommandSet',
7070
'Statement',
71+
# Color
72+
"ColorName",
7173
# Decorators
7274
'with_argument_list',
7375
'with_argparser',
@@ -81,13 +83,13 @@
8183
'PassThroughException',
8284
'SkipPostcommandHooks',
8385
# modules
84-
'colors',
8586
'plugin',
8687
'rich_utils',
8788
'string_utils',
88-
'styles',
8989
# String Utils
9090
'stylize',
91+
# Styles,
92+
"Cmd2Style",
9193
# Utilities
9294
'categorize',
9395
'CompletionMode',

cmd2/argparse_completer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from rich.box import SIMPLE_HEAD
3030
from rich.table import Column, Table
3131

32-
from . import styles
3332
from .argparse_custom import (
3433
ChoicesCallable,
3534
ChoicesProviderFuncWithTokens,
@@ -42,6 +41,7 @@
4241
from .exceptions import (
4342
CompletionError,
4443
)
44+
from .styles import Cmd2Style
4545

4646
# If no descriptive headers are supplied, then this will be used instead
4747
DEFAULT_DESCRIPTIVE_HEADERS: Sequence[str | Column] = ('Description',)
@@ -584,7 +584,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: list[str]
584584
*headers,
585585
box=SIMPLE_HEAD,
586586
show_edge=False,
587-
border_style=styles.RULE_LINE,
587+
border_style=Cmd2Style.RULE_LINE,
588588
)
589589
for item in completion_items:
590590
hint_table.add_row(item, *item.descriptive_data)

cmd2/argparse_custom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ def get_items(self) -> list[CompletionItems]:
297297
from . import (
298298
constants,
299299
rich_utils,
300-
styles,
301300
)
301+
from .styles import Cmd2Style
302302

303303
if TYPE_CHECKING: # pragma: no cover
304304
from .argparse_completer import (
@@ -1483,7 +1483,7 @@ def error(self, message: str) -> NoReturn:
14831483
# Add error style to message
14841484
console = self._get_formatter().console
14851485
with console.capture() as capture:
1486-
console.print(formatted_message, style=styles.ERROR)
1486+
console.print(formatted_message, style=Cmd2Style.ERROR)
14871487
formatted_message = f"{capture.get()}"
14881488

14891489
self.exit(2, f'{formatted_message}\n')

cmd2/cmd2.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080
plugin,
8181
rich_utils,
8282
string_utils,
83-
styles,
8483
utils,
8584
)
8685
from .argparse_custom import (
@@ -142,6 +141,7 @@
142141
strip_quotes,
143142
strip_style,
144143
)
144+
from .styles import Cmd2Style
145145

146146
# NOTE: When using gnureadline with Python 3.13, start_ipython needs to be imported before any readline-related stuff
147147
with contextlib.suppress(ImportError):
@@ -169,7 +169,7 @@
169169

170170
# Set up readline
171171
if rl_type == RlType.NONE: # pragma: no cover
172-
Cmd2Console(sys.stderr).print(rl_warning, style=styles.WARNING)
172+
Cmd2Console(sys.stderr).print(rl_warning, style=Cmd2Style.WARNING)
173173
else:
174174
from .rl_utils import ( # type: ignore[attr-defined]
175175
readline,
@@ -1292,7 +1292,7 @@ def perror(
12921292
*objects: Any,
12931293
sep: str = " ",
12941294
end: str = "\n",
1295-
style: StyleType | None = styles.ERROR,
1295+
style: StyleType | None = Cmd2Style.ERROR,
12961296
soft_wrap: bool | None = None,
12971297
rich_print_kwargs: RichPrintKwargs | None = None,
12981298
**kwargs: Any, # noqa: ARG002
@@ -1302,7 +1302,7 @@ def perror(
13021302
:param objects: objects to print
13031303
:param sep: string to write between print data. Defaults to " ".
13041304
:param end: string to write at end of print data. Defaults to a newline.
1305-
:param style: optional style to apply to output. Defaults to styles.ERROR.
1305+
:param style: optional style to apply to output. Defaults to Cmd2Style.ERROR.
13061306
:param soft_wrap: Enable soft wrap mode. If True, text lines will not be automatically word-wrapped to fit the
13071307
terminal width; instead, any text that doesn't fit will run onto the following line(s),
13081308
similar to the built-in print() function. Set to False to enable automatic word-wrapping.
@@ -1332,7 +1332,7 @@ def psuccess(
13321332
rich_print_kwargs: RichPrintKwargs | None = None,
13331333
**kwargs: Any, # noqa: ARG002
13341334
) -> None:
1335-
"""Wrap poutput, but apply styles.SUCCESS.
1335+
"""Wrap poutput, but apply Cmd2Style.SUCCESS.
13361336
13371337
:param objects: objects to print
13381338
:param sep: string to write between print data. Defaults to " ".
@@ -1351,7 +1351,7 @@ def psuccess(
13511351
*objects,
13521352
sep=sep,
13531353
end=end,
1354-
style=styles.SUCCESS,
1354+
style=Cmd2Style.SUCCESS,
13551355
soft_wrap=soft_wrap,
13561356
rich_print_kwargs=rich_print_kwargs,
13571357
)
@@ -1365,7 +1365,7 @@ def pwarning(
13651365
rich_print_kwargs: RichPrintKwargs | None = None,
13661366
**kwargs: Any, # noqa: ARG002
13671367
) -> None:
1368-
"""Wrap perror, but apply styles.WARNING.
1368+
"""Wrap perror, but apply Cmd2Style.WARNING.
13691369
13701370
:param objects: objects to print
13711371
:param sep: string to write between print data. Defaults to " ".
@@ -1384,7 +1384,7 @@ def pwarning(
13841384
*objects,
13851385
sep=sep,
13861386
end=end,
1387-
style=styles.WARNING,
1387+
style=Cmd2Style.WARNING,
13881388
soft_wrap=soft_wrap,
13891389
rich_print_kwargs=rich_print_kwargs,
13901390
)
@@ -1415,7 +1415,7 @@ def pexcept(
14151415

14161416
if not self.debug and 'debug' in self.settables:
14171417
warning = "\nTo enable full traceback, run the following command: 'set debug true'"
1418-
final_msg.append(warning, style=styles.WARNING)
1418+
final_msg.append(warning, style=Cmd2Style.WARNING)
14191419

14201420
if final_msg:
14211421
self.perror(
@@ -2491,7 +2491,7 @@ def complete( # type: ignore[override]
24912491
sys.stdout,
24922492
Text.assemble(
24932493
"\n",
2494-
(err_str, styles.ERROR if ex.apply_style else ""),
2494+
(err_str, Cmd2Style.ERROR if ex.apply_style else ""),
24952495
),
24962496
)
24972497
rl_force_redisplay()
@@ -3591,7 +3591,7 @@ def _build_alias_create_parser(cls) -> Cmd2ArgumentParser:
35913591
alias_create_notes = Group(
35923592
"If you want to use redirection, pipes, or terminators in the value of the alias, then quote them.",
35933593
"\n",
3594-
Text(" alias create save_results print_results \">\" out.txt\n", style=styles.EXAMPLE),
3594+
Text(" alias create save_results print_results \">\" out.txt\n", style=Cmd2Style.EXAMPLE),
35953595
(
35963596
"Since aliases are resolved during parsing, tab completion will function as it would "
35973597
"for the actual command the alias resolves to."
@@ -3804,14 +3804,14 @@ def _build_macro_create_parser(cls) -> Cmd2ArgumentParser:
38043804
"\n",
38053805
"The following creates a macro called my_macro that expects two arguments:",
38063806
"\n",
3807-
Text(" macro create my_macro make_dinner --meat {1} --veggie {2}", style=styles.EXAMPLE),
3807+
Text(" macro create my_macro make_dinner --meat {1} --veggie {2}", style=Cmd2Style.EXAMPLE),
38083808
"\n",
38093809
"When the macro is called, the provided arguments are resolved and the assembled command is run. For example:",
38103810
"\n",
38113811
Text.assemble(
3812-
(" my_macro beef broccoli", styles.EXAMPLE),
3812+
(" my_macro beef broccoli", Cmd2Style.EXAMPLE),
38133813
(" ───> ", Style(bold=True)),
3814-
("make_dinner --meat beef --veggie broccoli", styles.EXAMPLE),
3814+
("make_dinner --meat beef --veggie broccoli", Cmd2Style.EXAMPLE),
38153815
),
38163816
)
38173817
macro_create_parser = argparse_custom.DEFAULT_ARGUMENT_PARSER(description=macro_create_description)
@@ -3827,15 +3827,15 @@ def _build_macro_create_parser(cls) -> Cmd2ArgumentParser:
38273827
"first argument will populate both {1} instances."
38283828
),
38293829
"\n",
3830-
Text(" macro create ft file_taxes -p {1} -q {2} -r {1}", style=styles.EXAMPLE),
3830+
Text(" macro create ft file_taxes -p {1} -q {2} -r {1}", style=Cmd2Style.EXAMPLE),
38313831
"\n",
38323832
"To quote an argument in the resolved command, quote it during creation.",
38333833
"\n",
3834-
Text(" macro create backup !cp \"{1}\" \"{1}.orig\"", style=styles.EXAMPLE),
3834+
Text(" macro create backup !cp \"{1}\" \"{1}.orig\"", style=Cmd2Style.EXAMPLE),
38353835
"\n",
38363836
"If you want to use redirection, pipes, or terminators in the value of the macro, then quote them.",
38373837
"\n",
3838-
Text(" macro create show_results print_results -type {1} \"|\" less", style=styles.EXAMPLE),
3838+
Text(" macro create show_results print_results -type {1} \"|\" less", style=Cmd2Style.EXAMPLE),
38393839
"\n",
38403840
(
38413841
"Since macros don't resolve until after you press Enter, their arguments tab complete as paths. "
@@ -4125,7 +4125,7 @@ def print_topics(self, header: str, cmds: list[str] | None, cmdlen: int, maxcol:
41254125
"""
41264126
if cmds:
41274127
header_grid = Table.grid()
4128-
header_grid.add_row(header, style=styles.HELP_TITLE)
4128+
header_grid.add_row(header, style=Cmd2Style.HELP_TITLE)
41294129
if self.ruler:
41304130
header_grid.add_row(Rule(characters=self.ruler))
41314131
self.poutput(header_grid)
@@ -4197,8 +4197,8 @@ def _help_menu(self, verbose: bool = False) -> None:
41974197
self._print_topics(self.doc_header, cmds_doc, verbose)
41984198
else:
41994199
# Categories found, Organize all commands by category
4200-
self.poutput(self.doc_leader, style=styles.HELP_HEADER, soft_wrap=False)
4201-
self.poutput(self.doc_header, style=styles.HELP_HEADER, end="\n\n", soft_wrap=False)
4200+
self.poutput(self.doc_leader, style=Cmd2Style.HELP_HEADER, soft_wrap=False)
4201+
self.poutput(self.doc_header, style=Cmd2Style.HELP_HEADER, end="\n\n", soft_wrap=False)
42024202
for category in sorted(cmds_cats.keys(), key=self.default_sort_key):
42034203
self._print_topics(category, cmds_cats[category], verbose)
42044204
self._print_topics(self.default_category, cmds_doc, verbose)
@@ -4246,13 +4246,13 @@ def _print_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
42464246
self.print_topics(header, cmds, 15, 80)
42474247
else:
42484248
category_grid = Table.grid()
4249-
category_grid.add_row(header, style=styles.HELP_TITLE)
4249+
category_grid.add_row(header, style=Cmd2Style.HELP_TITLE)
42504250
category_grid.add_row(Rule(characters=self.ruler))
42514251
topics_table = Table(
42524252
Column("Name", no_wrap=True),
42534253
Column("Description", overflow="fold"),
42544254
box=SIMPLE_HEAD,
4255-
border_style=styles.RULE_LINE,
4255+
border_style=Cmd2Style.RULE_LINE,
42564256
show_edge=False,
42574257
)
42584258

@@ -4500,7 +4500,7 @@ def do_set(self, args: argparse.Namespace) -> None:
45004500
Column("Value", overflow="fold"),
45014501
Column("Description", overflow="fold"),
45024502
box=SIMPLE_HEAD,
4503-
border_style=styles.RULE_LINE,
4503+
border_style=Cmd2Style.RULE_LINE,
45044504
show_edge=False,
45054505
)
45064506

@@ -5344,7 +5344,7 @@ def _build_edit_parser(cls) -> Cmd2ArgumentParser:
53445344
"Note",
53455345
Text.assemble(
53465346
"To set a new editor, run: ",
5347-
("set editor <program>", styles.EXAMPLE),
5347+
("set editor <program>", Cmd2Style.EXAMPLE),
53485348
),
53495349
)
53505350

0 commit comments

Comments
 (0)