diff --git a/django_typer/__init__.py b/django_typer/__init__.py index 51eddf9..d560298 100644 --- a/django_typer/__init__.py +++ b/django_typer/__init__.py @@ -21,8 +21,6 @@ from django.conf import settings from django.core.management import get_commands from django.core.management.base import BaseCommand -from django.core.management.color import no_style -from django.utils.functional import lazy from django.utils.translation import gettext as _ from typer import Typer from typer.core import TyperCommand as CoreTyperCommand @@ -72,19 +70,18 @@ behavior should align with native django commands """ -# try: -# from typer import rich_utils -# def get_color_system(default): -# return None -# ctx = click.get_current_context(silent=True) -# if ctx: -# return None if ctx.django_command.style == no_style() else default -# return default - -# COLOR_SYSTEM = lazy(get_color_system, str) -# rich_utils.COLOR_SYSTEM = COLOR_SYSTEM(rich_utils.COLOR_SYSTEM) -# except ImportError: -# pass +try: + from typer import rich_utils + console_getter = rich_utils._get_rich_console + def get_console(): + console = console_getter() + ctx = click.get_current_context(silent=True) + if ctx and ctx.params.get('no_color', False): + console._color_system = None + return console + rich_utils._get_rich_console = get_console +except ImportError: + pass def traceback_config() -> t.Union[bool, t.Dict[str, t.Any]]: diff --git a/django_typer/tests/tests.py b/django_typer/tests/tests.py index e232bcc..f0fa5ac 100644 --- a/django_typer/tests/tests.py +++ b/django_typer/tests/tests.py @@ -365,7 +365,7 @@ def test_helps(self, top_level_only=False): buffer = StringIO() cmd = get_command(self.cmd_name, stdout=buffer, no_color=True) - help_output_top = run_command(self.cmd_name, "--help") + help_output_top = run_command(self.cmd_name, "--help", "--no-color") cmd.print_help("./manage.py", self.cmd_name) self.assertEqual(help_output_top.strip(), buffer.getvalue().strip()) self.assertIn(f"Usage: ./manage.py {self.cmd_name} [OPTIONS]", help_output_top) @@ -373,7 +373,7 @@ def test_helps(self, top_level_only=False): if not top_level_only: buffer.truncate(0) buffer.seek(0) - callback_help = run_command(self.cmd_name, "5", self.cmd_name, "--help") + callback_help = run_command(self.cmd_name, "--no-color", "5", self.cmd_name, "--help") cmd.print_help("./manage.py", self.cmd_name, self.cmd_name) self.assertEqual(callback_help.strip(), buffer.getvalue().strip()) self.assertIn( diff --git a/django_typer/types.py b/django_typer/types.py index e579b10..fafb3dc 100644 --- a/django_typer/types.py +++ b/django_typer/types.py @@ -102,6 +102,7 @@ def print_version(context, _, value): Option( "--no-color", help=_("Don't colorize the command output."), + is_eager=True, rich_help_panel=COMMON_PANEL, ), ]