Skip to content

Commit

Permalink
try fix color
Browse files Browse the repository at this point in the history
  • Loading branch information
bckohan committed Jan 27, 2024
1 parent 8ae86b9 commit 03679a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 7 additions & 0 deletions django_typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.translation import gettext as _

# this has to go here before rich Consoles are instantiated by Typer
Expand Down Expand Up @@ -778,7 +779,13 @@ def print_help(self, *command_path: str):
)
command_node = self.django_command.get_subcommand(*command_path)
with contextlib.redirect_stdout(self.django_command.stdout):
unset = False
if self.django_command.style == no_style():
os.environ['NO_COLOR'] = '1'
unset = True
command_node.print_help()
if unset:
os.environ.pop('NO_COLOR')

def parse_args(self, args=None, namespace=None):
try:
Expand Down
10 changes: 10 additions & 0 deletions django_typer/management/commands/shellcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
shellcompletion scripts for supported shells (bash, fish, zsh, powershell). This
command is also the entry point for running the completion logic and can be used to
debug completer code.
It invokes typer's shell completion installation logic, but does have to patch the
installed scripts. This is because there is only one installation for all django
management commands, not each individual command. The completion logic here will
failover to django's builtin autocomplete if the command in question is not a
TyperCommand. To promote compatibility with other management command libraries or
custom completion logic, a fallback completion function can also be specified. A
needed refactoring here would be to provide root hooks for completion logic in django
that base classes can register for. This would provide a coordinated way for libraries
like django-typer to plug in their own completion logic.
"""

import contextlib
Expand Down
12 changes: 6 additions & 6 deletions django_typer/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ def test_verbosity(self):
class TestHelpPrecedence(TestCase):
def test_help_precedence1(self):
buffer = StringIO()
cmd = get_command("help_precedence1", stdout=buffer)
cmd = get_command("help_precedence1", stdout=buffer, no_color=True)
cmd.print_help("./manage.py", "help_precedence1")
self.assertTrue(
re.search(
Expand All @@ -715,7 +715,7 @@ def test_help_precedence1(self):

def test_help_precedence2(self):
buffer = StringIO()
cmd = get_command("help_precedence2", stdout=buffer)
cmd = get_command("help_precedence2", stdout=buffer, no_color=True)
cmd.print_help("./manage.py", "help_precedence2")
self.assertIn(
"Test minimal TyperCommand subclass - class member", buffer.getvalue()
Expand All @@ -729,7 +729,7 @@ def test_help_precedence2(self):

def test_help_precedence3(self):
buffer = StringIO()
cmd = get_command("help_precedence3", stdout=buffer)
cmd = get_command("help_precedence3", stdout=buffer, no_color=True)
cmd.print_help("./manage.py", "help_precedence3")
self.assertTrue(
re.search(
Expand All @@ -743,7 +743,7 @@ def test_help_precedence3(self):

def test_help_precedence4(self):
buffer = StringIO()
cmd = get_command("help_precedence4", stdout=buffer)
cmd = get_command("help_precedence4", stdout=buffer, no_color=True)
cmd.print_help("./manage.py", "help_precedence4")
self.assertIn(
"Test minimal TyperCommand subclass - callback docstring", buffer.getvalue()
Expand All @@ -757,15 +757,15 @@ def test_help_precedence4(self):

def test_help_precedence5(self):
buffer = StringIO()
cmd = get_command("help_precedence5", stdout=buffer)
cmd = get_command("help_precedence5", stdout=buffer, no_color=True)
cmd.print_help("./manage.py", "help_precedence5")
self.assertIn(
"Test minimal TyperCommand subclass - command method", buffer.getvalue()
)

def test_help_precedence6(self):
buffer = StringIO()
cmd = get_command("help_precedence6", stdout=buffer)
cmd = get_command("help_precedence6", stdout=buffer, no_color=True)
cmd.print_help("./manage.py", "help_precedence6")
self.assertIn(
"Test minimal TyperCommand subclass - docstring", buffer.getvalue()
Expand Down

0 comments on commit 03679a9

Please sign in to comment.