Skip to content

Commit

Permalink
monkey patch console to make no-color work
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Jan 27, 2024
1 parent d713c9a commit 5f246fd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
27 changes: 12 additions & 15 deletions django_typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]:
Expand Down
4 changes: 2 additions & 2 deletions django_typer/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ 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)

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(
Expand Down
1 change: 1 addition & 0 deletions django_typer/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
]
Expand Down

0 comments on commit 5f246fd

Please sign in to comment.