Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change error displaying and add colors! #490

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion norminette/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ def add(self, *args, **kwargs) -> None:
def status(self) -> Literal["OK", "Error"]:
return "OK" if all(it.level == "Notice" for it in self._inner) else "Error"

@property
def has_notice(self) -> bool:
return any(it.level == "Notice" for it in self._inner)

def append(self, value: Union[NormError, NormWarning]) -> None:
# TODO Remove NormError and NormWarning since it does not provide `length` data
assert isinstance(value, (NormError, NormWarning))
Expand All @@ -172,9 +176,20 @@ def __init_subclass__(cls) -> None:

class HumanizedErrorsFormatter(_formatter):
def __str__(self) -> str:

def colorize(status: Literal["OK", "Error", "Notice"], txt: str) -> str:
return "\033[9" + (
"1" if status == "Error" else (
"2" if status == "OK" else "3"
)) + "m" + txt + "\033[0m"

output = ''
for file in self.files:
output += f"{file.basename}: {file.errors.status}!"
status = file.errors.status
if status == "OK" and file.errors.has_notice:
status = "Notice"
output += colorize(status, f"[{'KO' if status == 'Error' else 'OK'}]")
output += f" {file.basename}"
zylosophe marked this conversation as resolved.
Show resolved Hide resolved
for error in file.errors:
highlight = error.highlights[0]
output += f"\n{error.level}: {error.name:<20} "
Expand Down
Loading