forked from aqua5230/usage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage_statusline_forwarder.py
More file actions
59 lines (47 loc) · 1.46 KB
/
usage_statusline_forwarder.py
File metadata and controls
59 lines (47 loc) · 1.46 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
#!/usr/bin/env python3
"""usage app statusLine forwarder: fan stdin out to ~/.claude/*-statusline.py."""
from __future__ import annotations
import concurrent.futures
import glob
import os
import subprocess
import sys
__version__ = "1.0"
TIMEOUT_SECONDS = 5
HOOK_DIR = os.path.expanduser("~/.claude")
SELF_NAME = "usage-statusline-forwarder.py"
def _run_hook(py: str, hook: str, raw: str) -> str:
try:
result = subprocess.run(
[py, hook],
input=raw,
text=True,
check=False,
capture_output=True,
timeout=TIMEOUT_SECONDS,
)
except (subprocess.TimeoutExpired, OSError, UnicodeDecodeError):
return ""
return result.stdout or ""
def main() -> None:
raw = sys.stdin.read()
if not raw.strip():
return
hooks: list[str] = []
for path in sorted(glob.glob(os.path.join(HOOK_DIR, "*-statusline.py"))):
name = os.path.basename(path)
if name == SELF_NAME:
continue
if "-forwarder" in name:
continue
hooks.append(path)
py = sys.executable or "/usr/bin/python3"
with concurrent.futures.ThreadPoolExecutor(max_workers=max(1, len(hooks))) as ex:
futures = [ex.submit(_run_hook, py, hook, raw) for hook in hooks]
for future in futures:
out = future.result()
if out:
sys.stdout.write(out)
sys.stdout.flush()
if __name__ == "__main__":
main()