Skip to content

Commit

Permalink
Don't strip colors when redirected, warn about missing colorama
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Oct 26, 2024
1 parent faa0d40 commit 4f354d7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
22 changes: 21 additions & 1 deletion gersemi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@
from gersemi.runner import run, print_to_stderr
from gersemi.__version__ import __title__, __version__

MISSING = "(missing)"


try:
from colorama import __version__ as colorama_version

except ImportError:
colorama_version = MISSING


class ShowVersion(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
print(f"{__title__} {__version__}")
print(f"lark {lark_version}")
print(f"colorama {colorama_version}")
print(f"Python {sys.version}")
sys.exit(SUCCESS)

Expand Down Expand Up @@ -186,14 +196,24 @@ def create_argparser():
action="store_true",
help=f"{control_conf_doc['quiet']} [default: don't skip]",
)

if colorama_version == MISSING:
warn_about_missing_colorama = " Warning: missing colorama."
else:
warn_about_missing_colorama = ""

control_configuration_group.add_argument(
"--color",
"--no-color",
dest="color",
action=toggle_with_no_prefix,
nargs=0,
default=None,
help=f"{control_conf_doc['color']} [default: don't colorize diff]",
help=f"""
{control_conf_doc['color']}
{warn_about_missing_colorama}
[default: don't colorize diff]
""",
)
control_configuration_group.add_argument(
"-w",
Expand Down
7 changes: 6 additions & 1 deletion gersemi/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ class ControlConfiguration:
default=False,
metadata=dict(
title="Colorized diff",
description="If --diff is selected showed diff is colorized.",
description=doc(
"""
If --diff is selected showed diff is colorized.
Colorama has to be installed for this option to work.
"""
),
),
)

Expand Down
2 changes: 1 addition & 1 deletion gersemi/tasks/show_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
try:
from colorama import Fore, Style, init # type: ignore

init()
init(strip=False)

def colorize(diff):
for line in diff:
Expand Down
3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[mypy-appdirs]
ignore_missing_imports = True

[mypy-colorama]
ignore_missing_imports = True

[mypy-pytest]
ignore_missing_imports = True

Expand Down

0 comments on commit 4f354d7

Please sign in to comment.