Skip to content

Commit

Permalink
Adds color to ERROR and SUCCESS in check mode.
Browse files Browse the repository at this point in the history
Fixes PyCQA#1177.
  • Loading branch information
sztamas committed Jul 26, 2020
1 parent 2d76984 commit 97e96f4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
11 changes: 9 additions & 2 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from typing import List, Optional, TextIO, Union, cast
from warnings import warn

import colorama

from . import io, output, parse
from .exceptions import (
ExistingSyntaxErrors,
Expand All @@ -19,6 +21,8 @@
format_natural,
remove_whitespace,
show_unified_diff,
style_error,
style_success,
)
from .io import Empty
from .place import module as place_module # skipcq: PYL-W0611 (intended export of public API)
Expand Down Expand Up @@ -219,10 +223,13 @@ def check_stream(

if not changed:
if config.verbose:
print(f"SUCCESS: {file_path or ''} Everything Looks Good!")
print(f"{style_success('SUCCESS')}: {file_path or ''} Everything Looks Good!")
return True
else:
print(f"ERROR: {file_path or ''} Imports are incorrectly sorted and/or formatted.")
print(
f"{style_error('ERROR')}: "
f"{file_path or ''} Imports are incorrectly sorted and/or formatted."
)
if show_diff:
output_stream = StringIO()
input_stream.seek(0)
Expand Down
16 changes: 15 additions & 1 deletion isort/format.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import sys
from datetime import datetime
from difflib import unified_diff
from functools import partial
from pathlib import Path
from typing import Optional, TextIO
from typing import Iterable, Optional, TextIO

import colorama

colorama.init()


def format_simplified(import_line: str) -> str:
Expand Down Expand Up @@ -71,3 +76,12 @@ def ask_whether_to_apply_changes_to_file(file_path: str) -> bool:
def remove_whitespace(content: str, line_separator: str = "\n") -> str:
content = content.replace(line_separator, "").replace(" ", "").replace("\x0c", "")
return content


def style_text(text: str, styles: Iterable[str]) -> str:
escape_sequences = "".join(styles)
return escape_sequences + text + colorama.Style.RESET_ALL


style_error = partial(style_text, styles=(colorama.Fore.RED,))
style_success = partial(style_text, styles=(colorama.Fore.GREEN,))
3 changes: 2 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pipreqs = {version = "*", optional = true}
requirementslib = {version = "*", optional = true}
tomlkit = {version = ">=0.5.3", optional = true}
pip-api = {version = "*", optional = true}
colorama = "^0.4.3"

[tool.poetry.extras]
pipfile_deprecated_finder = ["pipreqs", "tomlkit", "requirementslib", "pip-shims<=0.3.4"]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import MagicMock, patch

import colorama
import pytest
from hypothesis_auto import auto_pytest_magic

Expand All @@ -16,3 +17,17 @@ def test_ask_whether_to_apply_changes_to_file():
with patch("isort.format.input", MagicMock(return_value="q")):
with pytest.raises(SystemExit):
assert isort.format.ask_whether_to_apply_changes_to_file("")


def test_style_error():
message = "ERROR"
styled = isort.format.style_error(message)
assert message in styled
assert styled == colorama.Fore.RED + message + colorama.Style.RESET_ALL


def test_style_success():
message = "OK"
styled = isort.format.style_success(message)
assert message in styled
assert styled == colorama.Fore.GREEN + message + colorama.Style.RESET_ALL
3 changes: 2 additions & 1 deletion tests/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2656,7 +2656,8 @@ def test_strict_whitespace_by_default(capsys) -> None:
test_input = "import os\nfrom django.conf import settings\n"
assert not api.check_code_string(test_input)
out, _ = capsys.readouterr()
assert out == "ERROR: Imports are incorrectly sorted and/or formatted.\n"
assert "ERROR" in out
assert out.endswith("Imports are incorrectly sorted and/or formatted.\n")


def test_strict_whitespace_no_closing_newline_issue_676(capsys) -> None:
Expand Down

0 comments on commit 97e96f4

Please sign in to comment.