-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_format.py
More file actions
120 lines (83 loc) · 2.87 KB
/
Copy pathcli_format.py
File metadata and controls
120 lines (83 loc) · 2.87 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
"""TTY-aware ANSI formatting for CLI stderr progress."""
from __future__ import annotations
import itertools
import sys
import threading
import time
_RESET = "\033[0m"
_BOLD = "\033[1m"
_DIM = "\033[2m"
_GREEN = "\033[32m"
_RED = "\033[31m"
_CYAN = "\033[36m"
CHECK = "✓"
CROSS = "✗"
_SPINNER_FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
_NOISE_CONTAINS: tuple[bytes, ...] = (
b"lance::",
b"FutureWarning",
b"Loading weights:",
b'"event": "brownfield-',
b"unknown producer source strategy",
b"unknown client source strategy",
# Builder verbose heartbeats / pass banners: in default mode the renderer's
# bar subsumes these, so they must NOT also appear as raw lines above the
# Live region. --verbose raw-relay bypasses this filter and still shows them.
b"[graph] pass ",
b"[graph] scoped write ",
b"[graph] writing ",
b"[graph] done ",
b"[increment] ",
)
def is_noise_line(line: bytes) -> bool:
return any(p in line for p in _NOISE_CONTAINS)
def stderr_is_tty() -> bool:
return hasattr(sys.stderr, "isatty") and sys.stderr.isatty()
def _styled(text: str, *codes: str) -> str:
if not stderr_is_tty():
return text
return "".join(codes) + text + _RESET
def bold(text: str) -> str:
return _styled(text, _BOLD)
def dim(text: str) -> str:
return _styled(text, _DIM)
def green(text: str) -> str:
return _styled(text, _GREEN)
def red(text: str) -> str:
return _styled(text, _RED)
def cyan(text: str) -> str:
return _styled(text, _CYAN)
def bold_green(text: str) -> str:
return _styled(text, _BOLD, _GREEN)
def bold_red(text: str) -> str:
return _styled(text, _BOLD, _RED)
def bold_cyan(text: str) -> str:
return _styled(text, _BOLD, _CYAN)
def styled_check() -> str:
return green(CHECK) if stderr_is_tty() else CHECK
def styled_cross() -> str:
return red(CROSS) if stderr_is_tty() else CROSS
class Spinner:
"""Braille spinner that overwrites the current stderr line until stopped."""
def __init__(self, label: str) -> None:
self._label = label
self._stop = threading.Event()
self._thread: threading.Thread | None = None
def start(self) -> None:
self._thread = threading.Thread(target=self._run, name="spinner", daemon=True)
self._thread.start()
def stop(self) -> None:
self._stop.set()
if self._thread is not None:
self._thread.join(timeout=2.0)
sys.stderr.buffer.write(b"\r\x1b[2K")
sys.stderr.buffer.flush()
def _run(self) -> None:
frames = itertools.cycle(_SPINNER_FRAMES)
t0 = time.monotonic()
while not self._stop.wait(0.3):
elapsed = time.monotonic() - t0
frame = next(frames)
line = f"\r{frame} {self._label} · {elapsed:.0f}s"
sys.stderr.buffer.write(line.encode())
sys.stderr.buffer.flush()