-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_format.py
More file actions
85 lines (55 loc) · 1.8 KB
/
Copy pathcli_format.py
File metadata and controls
85 lines (55 loc) · 1.8 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
"""TTY-aware ANSI formatting for CLI stderr progress."""
from __future__ import annotations
import sys
_RESET = "\033[0m"
_BOLD = "\033[1m"
_DIM = "\033[2m"
_GREEN = "\033[32m"
_RED = "\033[31m"
_CYAN = "\033[36m"
CHECK = "✓"
CROSS = "✗"
_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