From e4aa1a53dcaa5c85bc76f61cc4f321ce04f49c0b Mon Sep 17 00:00:00 2001 From: mcolonna Date: Fri, 1 Mar 2024 16:29:36 +0100 Subject: [PATCH 1/3] Change error displaying (colors!) - For each file, the "OK!" and "Error!" are changed to [OK] and [KO] and moved to the left of the file names. - The [OK] are green and the [KO] are red. - If there is a "Notice:" for a file, the [OK] becomes yellow. --- norminette/errors.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/norminette/errors.py b/norminette/errors.py index e8818f7..77588e7 100644 --- a/norminette/errors.py +++ b/norminette/errors.py @@ -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)) @@ -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}" for error in file.errors: highlight = error.highlights[0] output += f"\n{error.level}: {error.name:<20} " From aa1bbe9d2e4fc59e1e9aae0dd2b8a504b4bdf61a Mon Sep 17 00:00:00 2001 From: mcolonna Date: Wed, 6 Mar 2024 14:55:01 +0100 Subject: [PATCH 2/3] Change code structure (property colorful_status) --- norminette/errors.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/norminette/errors.py b/norminette/errors.py index 77588e7..f1085b6 100644 --- a/norminette/errors.py +++ b/norminette/errors.py @@ -149,8 +149,13 @@ 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 colorful_status(self) -> str: + if self.status == "OK": + if any(it.level == "Notice" for it in self._inner): + return "\033[93m[OK]\033[0m" + return "\033[92m[OK]\033[0m" + return "\033[91m[KO]\033[0m" + def append(self, value: Union[NormError, NormWarning]) -> None: # TODO Remove NormError and NormWarning since it does not provide `length` data @@ -185,10 +190,7 @@ def colorize(status: Literal["OK", "Error", "Notice"], txt: str) -> str: output = '' for file in self.files: - 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 += file.errors.colorful_status output += f" {file.basename}" for error in file.errors: highlight = error.highlights[0] From b46f610307c3c4c2f881013166188a04fd1072d8 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Wed, 6 Mar 2024 15:32:59 +0100 Subject: [PATCH 3/3] fix flake8 error --- norminette/errors.py | 1 - 1 file changed, 1 deletion(-) diff --git a/norminette/errors.py b/norminette/errors.py index f1085b6..17e6c7f 100644 --- a/norminette/errors.py +++ b/norminette/errors.py @@ -156,7 +156,6 @@ def colorful_status(self) -> str: return "\033[92m[OK]\033[0m" return "\033[91m[KO]\033[0m" - 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))