8000 gh-133346: Make theming support in _colorize extensible by ambv · Pull Request #133347 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-133346: Make theming support in _colorize extensible #133347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update test_argparse to use theme not color and fix bug
  • Loading branch information
hugovk authored and ambv committed May 4, 2025
commit fd9c85cd48dfe8996938cbe28bbd1361dddf7152
1 change: 1 addition & 0 deletions Lib/_colorize.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Argparse(ThemeSection):
summary_long_option: str = ANSIColors.CYAN
summary_short_option: str = ANSIColors.GREEN
summary_label: str = ANSIColors.YELLOW
summary_action: str = ANSIColors.GREEN
long_option: str = ANSIColors.BOLD_CYAN
short_option: str = ANSIColors.BOLD_GREEN
label: str = ANSIColors.BOLD_YELLOW
Expand Down
2 changes: 1 addition & 1 deletion Lib/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def _get_actions_usage_parts(self, actions, groups):
elif not action.option_strings:
default = self._get_default_metavar_for_positional(action)
part = (
t.summary_short_option
t.summary_action
+ self._format_args(action, default)
+ t.reset
)
Expand Down
34 changes: 19 additions & 15 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7058,7 +7058,7 @@ def setUp(self):
super().setUp()
# Ensure color even if ran with NO_COLOR=1
_colorize.can_colorize = lambda *args, **kwargs: True
self.ansi = _colorize.ANSIColors()
self.theme = _colorize.get_theme(force_color=True).argparse

def test_argparse_color(self):
# Arrange: create a parser with a bit of everything
Expand Down Expand Up @@ -7120,13 +7120,17 @@ def test_argparse_color(self):
sub2 = subparsers.add_parser("sub2", deprecated=True, help="sub2 help")
sub2.add_argument("--baz", choices=("X", "Y", "Z"), help="baz help")

heading = self.ansi.BOLD_BLUE
label, label_b = self.ansi.YELLOW, self.ansi.BOLD_YELLOW
long, long_b = self.ansi.CYAN, self.ansi.BOLD_CYAN
pos, pos_b = short, short_b = self.ansi.GREEN, self.ansi.BOLD_GREEN
sub = self.ansi.BOLD_GREEN
prog = self.ansi.BOLD_MAGENTA
reset = self.ansi.RESET
prog = self.theme.prog
heading = self.theme.heading
long = self.theme.summary_long_option
short = self.theme.summary_short_option
label = self.theme.summary_label
pos = self.theme.summary_action
long_b = self.theme.long_option
short_b = self.theme.short_option
label_b = self.theme.label
pos_b = self.theme.action
reset = self.theme.reset

# Act
help_text = parser.format_help()
Expand Down Expand Up @@ -7171,9 +7175,9 @@ def test_argparse_color(self):
{heading}subcommands:{reset}
valid subcommands
{sub}{{sub1,sub2}}{reset} additional help
{sub}sub1{reset} sub1 help
{sub}sub2{reset} sub2 help
{pos_b}{{sub1,sub2}}{reset} additional help
{pos_b}sub1{reset} sub1 help
{pos_b}sub2{reset} sub2 help
"""
),
)
Expand All @@ -7187,10 +7191,10 @@ def test_argparse_color_usage(self):
prog="PROG",
usage="[prefix] %(prog)s [suffix]",
)
heading = self.ansi.BOLD_BLUE
prog = self.ansi.BOLD_MAGENTA
reset = self.ansi.RESET
usage = self.ansi.MAGENTA
heading = self.theme.heading
prog = self.theme.prog
reset = self.theme.reset
usage = self< 533A /span>.theme.prog_extra

# Act
help_text = parser.format_help()
Expand Down
Loading
0