Repository navigation
Expand file tree
/
Copy path_branding.py
More file actions
224 lines (189 loc) · 7.14 KB
/
Copy path_branding.py
File metadata and controls
224 lines (189 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import annotations
import os
import sys
import threading
import time
from dataclasses import dataclass, field
from typing import Iterable
import click
from ifixai.types import InspectionCategory
_SPINNER_FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
class _Spinner:
def __init__(self, message: str) -> None:
self._message = message
self._stop_event = threading.Event()
self._thread: threading.Thread | None = None
self._frame = 0
self._start_time = 0.0
def start(self) -> None:
if self._thread is not None:
return
self._start_time = time.monotonic()
sys.stdout.write(self._render() + "\n")
sys.stdout.flush()
self._thread = threading.Thread(target=self._run, daemon=True)
self._thread.start()
def stop(self) -> None:
if self._thread is None:
return
self._stop_event.set()
self._thread.join(timeout=0.5)
self._thread = None
def _render(self) -> str:
glyph = _SPINNER_FRAMES[self._frame % len(_SPINNER_FRAMES)]
elapsed = int(time.monotonic() - self._start_time) if self._start_time else 0
suffix = f" ({elapsed}s)" if elapsed >= 3 else ""
return _truecolor(f" {glyph} {self._message}{suffix}", _DIM_RGB)
def _run(self) -> None:
while not self._stop_event.wait(0.12):
self._frame += 1
sys.stdout.write("\033[1F\033[2K" + self._render() + "\n")
sys.stdout.flush()
_LOGO_LINES: tuple[str, ...] = (
"██ ███████ ██ ██ ██ █████ ██",
"██ ██ ██ ██ ██ ██ ██ ██",
"██ █████ ██ ███ ███████ ██",
"██ ██ ██ ██ ██ ██ ██ ██",
"██ ██ ██ ██ ██ ██ ██ ██",
)
_ACCENT_RGB = (232, 99, 42)
_DIM_RGB = (110, 110, 117)
_CATEGORY_COLORS: dict[InspectionCategory, tuple[int, int, int]] = {
InspectionCategory.FABRICATION: (255, 139, 92),
InspectionCategory.MANIPULATION: (255, 99, 99),
InspectionCategory.DECEPTION: (167, 139, 250),
InspectionCategory.UNPREDICTABILITY: (251, 191, 36),
InspectionCategory.OPACITY: (96, 165, 250),
}
_CATEGORY_ORDER: tuple[InspectionCategory, ...] = (
InspectionCategory.FABRICATION,
InspectionCategory.MANIPULATION,
InspectionCategory.DECEPTION,
InspectionCategory.UNPREDICTABILITY,
InspectionCategory.OPACITY,
)
_BAR_WIDTH = 26
_BLOCK_FULL = "█"
_BLOCK_EMPTY = "·"
def supports_color(stream=None) -> bool:
s = stream or sys.stdout
if os.environ.get("NO_COLOR"):
return False
if not hasattr(s, "isatty"):
return False
return bool(s.isatty())
def _truecolor(text: str, rgb: tuple[int, int, int], bold: bool = False) -> str:
if not supports_color():
return text
r, g, b = rgb
prefix = f"\033[38;2;{r};{g};{b}m"
if bold:
prefix = "\033[1m" + prefix
return f"{prefix}{text}\033[0m"
def print_startup_banner(version: str, *, quiet: bool = False) -> None:
if quiet or not supports_color():
return
click.echo()
for line in _LOGO_LINES:
click.echo(" " + _truecolor(line, _ACCENT_RGB, bold=True))
click.echo()
click.echo(_truecolor(f" ™ · v{version} · powered by iMe", _DIM_RGB))
click.echo()
@dataclass
class _CategoryRow:
category: InspectionCategory
total: int = 0
done: int = 0
failed: int = 0
@dataclass
class CategoryProgress:
rows: dict[InspectionCategory, _CategoryRow] = field(default_factory=dict)
_printed_lines: int = 0
_started: bool = False
_interactive: bool = False
_spinner: _Spinner | None = None
@classmethod
def from_totals(cls, totals: dict[InspectionCategory, int]) -> "CategoryProgress":
rows = {cat: _CategoryRow(category=cat, total=totals.get(cat, 0)) for cat in _CATEGORY_ORDER}
return cls(rows=rows)
def start(self) -> None:
if self._started:
return
self._started = True
self._interactive = supports_color()
if not self._interactive:
return
total = sum(r.total for r in self.rows.values())
if total == 0:
return
self._spinner = _Spinner(f"Running {total} tests")
self._spinner.start()
self._printed_lines = 1
def record(self, category: InspectionCategory, passing: bool) -> None:
row = self.rows.get(category)
if row is None:
return
if row.total == 0:
row.total = max(1, row.total)
row.done += 1
if not passing:
row.failed += 1
self._redraw()
def finalize(self) -> None:
if self._spinner is not None:
self._spinner.stop()
self._spinner = None
self._redraw(final=True)
if self._interactive and self._printed_lines:
click.echo()
def _redraw(self, *, final: bool = False) -> None:
if not self._started:
return
rendered = self._render()
if not rendered:
return
if self._spinner is not None:
self._spinner.stop()
self._spinner = None
if self._interactive and self._printed_lines:
sys.stdout.write(f"\033[{self._printed_lines}F")
for line in rendered:
sys.stdout.write("\033[2K")
sys.stdout.write(line + "\n")
sys.stdout.flush()
self._printed_lines = len(rendered)
elif self._interactive and not self._printed_lines:
for line in rendered:
sys.stdout.write(line + "\n")
sys.stdout.flush()
self._printed_lines = len(rendered)
elif not self._interactive and final:
for line in rendered:
click.echo(line)
def _render(self) -> list[str]:
out: list[str] = []
for cat in _CATEGORY_ORDER:
row = self.rows[cat]
if row.total == 0:
continue
label = cat.value.upper().ljust(16)
ratio = row.done / row.total if row.total else 0.0
filled = int(round(ratio * _BAR_WIDTH))
bar = _BLOCK_FULL * filled + _BLOCK_EMPTY * (_BAR_WIDTH - filled)
colored_bar = _truecolor(bar, _CATEGORY_COLORS[cat], bold=True)
count = f"{row.done}/{row.total}".rjust(7)
tail = ""
if row.done >= row.total:
if row.failed == 0:
tail = " " + _truecolor("✓", (74, 222, 128))
else:
tail = " " + _truecolor(f"✗ {row.failed} failed", (239, 68, 68))
out.append(f" {label} {colored_bar} {count}{tail}")
return out
def category_totals_from_specs(specs: Iterable[object]) -> dict[InspectionCategory, int]:
counts: dict[InspectionCategory, int] = {cat: 0 for cat in _CATEGORY_ORDER}
for spec in specs:
cat = getattr(spec, "category", None)
if isinstance(cat, InspectionCategory):
counts[cat] = counts.get(cat, 0) + 1
return counts