-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdisplay_mode.py
More file actions
226 lines (194 loc) · 6.4 KB
/
Copy pathdisplay_mode.py
File metadata and controls
226 lines (194 loc) · 6.4 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
225
226
"""Launch ShaderClaw's VoxTerm display from VoxTerm."""
from __future__ import annotations
import json
import os
import shutil
import subprocess
import sys
import time
import urllib.error
import urllib.request
from dataclasses import dataclass
from pathlib import Path
from config import DATA_DIR, LIVE_DIR
DEFAULT_DISPLAY_PORT = 7778
SHADERCLAW_REPO_URL = "https://github.com/G3993/ShaderClaw3.git"
@dataclass(frozen=True)
class DisplayLaunchResult:
ok: bool
url: str
message: str
started: bool = False
pid: int | None = None
log_path: Path | None = None
shaderclaw_dir: Path | None = None
def display_url(port: int = DEFAULT_DISPLAY_PORT) -> str:
return f"http://localhost:{port}/voxterm"
def find_shaderclaw_dir(explicit: str | Path | None = None, repo_root: Path | None = None) -> Path | None:
"""Find a local ShaderClaw checkout that can serve VoxTerm display mode."""
candidates: list[Path] = []
def add(value: str | Path | None) -> None:
if not value:
return
path = Path(value).expanduser()
if path not in candidates:
candidates.append(path)
add(explicit)
add(os.environ.get("VOXTERM_SHADERCLAW_DIR"))
add(os.environ.get("SHADERCLAW_DIR"))
root = repo_root or Path(__file__).resolve().parent
home = Path.home()
names = (
"shader-claw3",
"shaderclaw-3",
"shaderclaw3",
"shader-claw",
"shaderclaw",
)
bases = (
root.parent,
home,
home / "Code",
home / "Developer",
home / "Projects",
home / "src",
)
for base in bases:
for name in names:
add(base / name)
for candidate in candidates:
if (candidate / "server.js").is_file() and (candidate / "package.json").is_file():
return candidate
return None
def _get_json(url: str, timeout: float = 0.4) -> dict | None:
try:
with urllib.request.urlopen(url, timeout=timeout) as response:
raw = response.read(1024 * 128).decode("utf-8", errors="replace")
data = json.loads(raw)
return data if isinstance(data, dict) else None
except (OSError, urllib.error.URLError, TimeoutError, json.JSONDecodeError):
return None
def is_display_running(port: int = DEFAULT_DISPLAY_PORT) -> bool:
status = _get_json(f"http://localhost:{port}/api/voxterm/status")
return bool(status and status.get("ok") is True)
def open_display_browser(port: int = DEFAULT_DISPLAY_PORT) -> bool:
url = display_url(port)
if sys.platform == "darwin":
command = ["open", url]
elif sys.platform == "win32":
command = ["cmd", "/c", "start", "", url]
else:
opener = shutil.which("xdg-open")
if not opener:
return False
command = [opener, url]
try:
subprocess.Popen(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return True
except OSError:
return False
def launch_display(
*,
port: int = DEFAULT_DISPLAY_PORT,
shaderclaw_dir: str | Path | None = None,
live_dir: str | Path = LIVE_DIR,
open_browser: bool = True,
wait_seconds: float = 6.0,
) -> DisplayLaunchResult:
"""Start or reveal ShaderClaw's VoxTerm display."""
url = display_url(port)
if is_display_running(port):
if open_browser:
open_display_browser(port)
return DisplayLaunchResult(
ok=True,
url=url,
message=f"display mode already running: {url}",
started=False,
)
shaderclaw = find_shaderclaw_dir(shaderclaw_dir)
if shaderclaw is None:
return DisplayLaunchResult(
ok=False,
url=url,
message=(
"ShaderClaw checkout not found. Install it with "
f"`git clone {SHADERCLAW_REPO_URL} ~/shader-claw3 && "
"cd ~/shader-claw3 && npm install`, or set "
"VOXTERM_SHADERCLAW_DIR to an existing shader-claw3 folder."
),
)
node = shutil.which("node")
if not node:
return DisplayLaunchResult(
ok=False,
url=url,
message="node was not found on PATH; install Node.js to use display mode.",
shaderclaw_dir=shaderclaw,
)
log_dir = DATA_DIR / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
log_path = log_dir / "shaderclaw-display.log"
env = os.environ.copy()
env["SHADERCLAW_PORT"] = str(port)
env["SHADERCLAW_VOXTERM_DISPLAY"] = "1"
env["SHADERCLAW_NO_OPEN"] = "1"
env["VOXTERM_LIVE_DIR"] = str(Path(live_dir).expanduser())
log_file = open(log_path, "ab")
try:
process = subprocess.Popen(
[node, "server.js", "--voxterm"],
cwd=str(shaderclaw),
env=env,
stdout=log_file,
stderr=subprocess.STDOUT,
start_new_session=(sys.platform != "win32"),
)
except OSError as exc:
log_file.close()
return DisplayLaunchResult(
ok=False,
url=url,
message=f"could not start ShaderClaw display: {exc}",
log_path=log_path,
shaderclaw_dir=shaderclaw,
)
finally:
if not log_file.closed:
log_file.close()
deadline = time.monotonic() + max(0.0, wait_seconds)
while time.monotonic() < deadline:
if process.poll() is not None:
return DisplayLaunchResult(
ok=False,
url=url,
message=f"ShaderClaw display exited early; see {log_path}",
started=True,
pid=process.pid,
log_path=log_path,
shaderclaw_dir=shaderclaw,
)
if is_display_running(port):
if open_browser:
open_display_browser(port)
return DisplayLaunchResult(
ok=True,
url=url,
message=f"display mode opened: {url}",
started=True,
pid=process.pid,
log_path=log_path,
shaderclaw_dir=shaderclaw,
)
time.sleep(0.15)
if open_browser:
open_display_browser(port)
return DisplayLaunchResult(
ok=True,
url=url,
message=f"display mode starting: {url}",
started=True,
pid=process.pid,
log_path=log_path,
shaderclaw_dir=shaderclaw,
)