|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from enum import Enum |
4 | | -from typing import TYPE_CHECKING, NewType |
| 4 | +from typing import TYPE_CHECKING, ClassVar, NewType |
5 | 5 |
|
6 | 6 | from attrs import define, field |
7 | 7 | from tabulate import tabulate |
@@ -47,6 +47,13 @@ class Report: |
47 | 47 | reasons: list[Reason] | None = field(default=None) |
48 | 48 | fixed: bool = False |
49 | 49 |
|
| 50 | + PALETTE: ClassVar[dict[str, str]] = { |
| 51 | + "RED": "\033[31m", |
| 52 | + "GREEN": "\033[32m", |
| 53 | + "YELLOW": "\033[33m", |
| 54 | + "CYAN": "\033[36m", |
| 55 | + } |
| 56 | + |
50 | 57 | def __attrs_post_init__(self) -> None: |
51 | 58 | if self.status == Status.PASSED: |
52 | 59 | assert self.reasons is None, "Passed report cannot have reasons" |
@@ -82,21 +89,35 @@ def to_str(self, *, strict: bool = False) -> str: |
82 | 89 | A string representation of the report. |
83 | 90 | """ |
84 | 91 | parts = [] |
85 | | - is_fixed = " --> FIXED" if self.fixed else "" |
86 | 92 | if not self.is_passed(strict=strict): |
87 | | - parts.append(f"\033[31m {self.id}:\033[0m{is_fixed}\n") |
| 93 | + # print failure reasons |
| 94 | + color = self.PALETTE["RED"] |
| 95 | + parts.append(f"{color} {self.id}:\033[0m\n") |
88 | 96 | for reason in self.reasons or []: |
89 | | - parts.append(f"\033[31m - {reason}\033[0m\n") |
| 97 | + parts.append(f"{color} - {reason}\033[0m\n") |
90 | 98 | elif self.is_skipped(): |
91 | | - parts.append(f"\033[36m {self.id}: [SKIPPED]\033[0m\n") |
| 99 | + # print skipped reasons |
| 100 | + color = self.PALETTE["CYAN"] |
| 101 | + parts.append(f"{color} {self.id}: [SKIPPED]\033[0m\n") |
92 | 102 | for reason in self.reasons or []: |
93 | | - parts.append(f"\033[36m - {reason}\033[0m\n") |
| 103 | + parts.append(f"{color} - {reason}\033[0m\n") |
94 | 104 | elif self.severity.is_warning() and self.reasons: |
95 | | - parts.append(f"\033[33m {self.id}:\033[0m{is_fixed}\n") |
| 105 | + # print warning reasons |
| 106 | + color = self.PALETTE["YELLOW"] |
| 107 | + parts.append(f"{color} {self.id}:\033[0m\n") |
| 108 | + for reason in self.reasons or []: |
| 109 | + parts.append(f"{color} - {reason}\033[0m\n") |
| 110 | + elif self.is_passed() and self.fixed: |
| 111 | + # print failure or warning but fixed reasons |
| 112 | + color1 = self.PALETTE["YELLOW"] if self.severity.is_warning() else self.PALETTE["RED"] |
| 113 | + color2 = self.PALETTE["GREEN"] |
| 114 | + parts.append(f"{color1} {self.id}:\033[0m {color2}--> FIXED🎉\033[0m\n") |
96 | 115 | for reason in self.reasons or []: |
97 | | - parts.append(f"\033[33m - {reason}\033[0m\n") |
| 116 | + parts.append(f"{color1} - {reason}\033[0m\n") |
98 | 117 | else: |
99 | | - parts.append(f"\033[32m {self.id}: ✅\033[0m\n") |
| 118 | + # print passed |
| 119 | + color = self.PALETTE["GREEN"] |
| 120 | + parts.append(f"{color} {self.id}: ✅\033[0m\n") |
100 | 121 | return "".join(parts) |
101 | 122 |
|
102 | 123 |
|
|
0 commit comments