par_term_scripting/manager.rs
1//! Per-tab multi-script orchestrator.
2//!
3//! [`ScriptManager`] manages multiple [`ScriptProcess`] instances for a single tab,
4//! providing lifecycle management, event broadcasting, and panel state tracking.
5
6use std::collections::HashMap;
7
8use super::process::ScriptProcess;
9use super::protocol::{ScriptCommand, ScriptEvent};
10use par_term_config::ScriptConfig;
11
12/// Unique identifier for a managed script process.
13pub type ScriptId = u64;
14
15/// Interpreter candidates for `.py` scripts, in priority order.
16///
17/// Direct executables come first on Windows, with the PEP 397 launcher `py`
18/// only as a fallback. `py` is an indirection: it spawns the real interpreter
19/// as a child process and selects the version itself, so the script becomes a
20/// grandchild whose stdio is one hop further away, and it may pick a different
21/// Python than `python` resolves to (on a GitHub runner, 3.14 versus 3.12).
22/// Preferring `py` made the scripting integration tests spawn successfully but
23/// read back zero commands.
24///
25/// `python3` is kept because it exists on Windows CI images even though the
26/// official installer does not create it — and it must stay ahead of `py` for
27/// the same indirection reason.
28#[cfg(windows)]
29const PYTHON_CANDIDATES: &[&str] = &["python", "python3", "py"];
30#[cfg(not(windows))]
31const PYTHON_CANDIDATES: &[&str] = &["python3", "python"];
32
33/// Whether `name` resolves to an executable on `PATH`.
34///
35/// On Windows each `PATHEXT` suffix is tried, since `python` on disk is
36/// `python.exe`.
37fn executable_on_path(name: &str) -> bool {
38 let Some(path) = std::env::var_os("PATH") else {
39 return false;
40 };
41
42 #[cfg(windows)]
43 let extensions: Vec<String> = std::env::var("PATHEXT")
44 .unwrap_or_else(|_| ".EXE;.BAT;.CMD".to_string())
45 .split(';')
46 .filter(|e| !e.is_empty())
47 .map(str::to_string)
48 .collect();
49
50 for dir in std::env::split_paths(&path) {
51 #[cfg(windows)]
52 {
53 for ext in &extensions {
54 if dir.join(format!("{}{}", name, ext)).is_file() {
55 return true;
56 }
57 }
58 }
59 #[cfg(not(windows))]
60 {
61 if dir.join(name).is_file() {
62 return true;
63 }
64 }
65 }
66 false
67}
68
69/// The interpreter used to run `.py` scripts on this machine.
70///
71/// Returns the first candidate found on `PATH`, or `None` when no Python is
72/// installed, so callers can report that rather than failing to spawn a command
73/// that was never going to exist.
74pub fn python_interpreter() -> Option<&'static str> {
75 PYTHON_CANDIDATES
76 .iter()
77 .copied()
78 .find(|candidate| executable_on_path(candidate))
79}
80
81/// Default maximum `WriteText` writes per second.
82pub const DEFAULT_WRITE_TEXT_RATE: u32 = 10;
83/// Default maximum `RunCommand` executions per second.
84pub const DEFAULT_RUN_COMMAND_RATE: u32 = 1;
85
86/// Manages multiple script subprocess instances for a single tab.
87///
88/// Each script is assigned a unique [`ScriptId`] and can be individually started,
89/// stopped, and communicated with. Supports panel state tracking per script and
90/// event broadcasting to all running scripts.
91pub struct ScriptManager {
92 /// Next ID to assign to a new script process.
93 next_id: ScriptId,
94 /// Map of active script processes keyed by their assigned ID.
95 processes: HashMap<ScriptId, ScriptProcess>,
96 /// Panel state per script: script_id -> (title, content).
97 panels: HashMap<ScriptId, (String, String)>,
98 /// Last `WriteText` dispatch time per script (for rate limiting).
99 write_text_times: HashMap<ScriptId, std::time::Instant>,
100 /// Last `RunCommand` dispatch time per script (for rate limiting).
101 run_command_times: HashMap<ScriptId, std::time::Instant>,
102}
103
104impl ScriptManager {
105 /// Create a new empty `ScriptManager`.
106 pub fn new() -> Self {
107 Self {
108 next_id: 1,
109 processes: HashMap::new(),
110 panels: HashMap::new(),
111 write_text_times: HashMap::new(),
112 run_command_times: HashMap::new(),
113 }
114 }
115
116 /// Start a script subprocess from the given configuration.
117 ///
118 /// If `script_path` ends with `.py`, it is run with the Python interpreter
119 /// resolved by [`python_interpreter`] (the script path is prepended to the
120 /// args). Otherwise, `script_path` is used as the command directly.
121 ///
122 /// Returns the assigned [`ScriptId`] on success.
123 ///
124 /// # Errors
125 /// Returns an error string if no Python interpreter is installed (for `.py`
126 /// scripts) or if the subprocess cannot be spawned.
127 pub fn start_script(&mut self, config: &ScriptConfig) -> Result<ScriptId, String> {
128 let (command, args) = if config.script_path.ends_with(".py") {
129 let interpreter = python_interpreter().ok_or_else(|| {
130 format!(
131 "no Python interpreter found on PATH (tried {}) to run '{}'",
132 PYTHON_CANDIDATES.join(", "),
133 config.script_path
134 )
135 })?;
136 let mut full_args = vec![config.script_path.as_str()];
137 let arg_refs: Vec<&str> = config.args.iter().map(String::as_str).collect();
138 full_args.extend(arg_refs);
139 (
140 interpreter.to_string(),
141 full_args.into_iter().map(String::from).collect::<Vec<_>>(),
142 )
143 } else {
144 let arg_refs: Vec<String> = config.args.to_vec();
145 (config.script_path.clone(), arg_refs)
146 };
147
148 let arg_strs: Vec<&str> = args.iter().map(String::as_str).collect();
149 let process = ScriptProcess::spawn(&command, &arg_strs, &config.env_vars)?;
150
151 let id = self.next_id;
152 self.next_id += 1;
153 self.processes.insert(id, process);
154
155 Ok(id)
156 }
157
158 /// Check if a script with the given ID is still running.
159 ///
160 /// Returns `false` if the script ID is unknown or the process has exited.
161 pub fn is_running(&mut self, id: ScriptId) -> bool {
162 self.processes.get_mut(&id).is_some_and(|p| p.is_running())
163 }
164
165 /// Send a [`ScriptEvent`] to a specific script by ID.
166 ///
167 /// # Errors
168 /// Returns an error if the script ID is unknown or the write fails.
169 pub fn send_event(&mut self, id: ScriptId, event: &ScriptEvent) -> Result<(), String> {
170 let process = self
171 .processes
172 .get_mut(&id)
173 .ok_or_else(|| format!("No script with id {}", id))?;
174 process.send_event(event)
175 }
176
177 /// Broadcast a [`ScriptEvent`] to all running scripts.
178 ///
179 /// Errors on individual scripts are silently ignored; the event is sent on a
180 /// best-effort basis to all processes.
181 pub fn broadcast_event(&mut self, event: &ScriptEvent) {
182 for process in self.processes.values_mut() {
183 let _ = process.send_event(event);
184 }
185 }
186
187 /// Drain pending [`ScriptCommand`]s from a specific script's stdout buffer.
188 ///
189 /// Returns an empty `Vec` if the script ID is unknown.
190 pub fn read_commands(&self, id: ScriptId) -> Vec<ScriptCommand> {
191 self.processes
192 .get(&id)
193 .map(|p| p.read_commands())
194 .unwrap_or_default()
195 }
196
197 /// Drain pending error lines from a specific script's stderr buffer.
198 ///
199 /// Returns an empty `Vec` if the script ID is unknown.
200 pub fn read_errors(&self, id: ScriptId) -> Vec<String> {
201 self.processes
202 .get(&id)
203 .map(|p| p.read_errors())
204 .unwrap_or_default()
205 }
206
207 /// Stop and remove a specific script by ID.
208 ///
209 /// Also clears the associated panel state and rate-limit tracking.
210 /// Does nothing if the ID is unknown.
211 pub fn stop_script(&mut self, id: ScriptId) {
212 if let Some(mut process) = self.processes.remove(&id) {
213 process.stop();
214 }
215 self.panels.remove(&id);
216 self.write_text_times.remove(&id);
217 self.run_command_times.remove(&id);
218 }
219
220 /// Stop and remove all managed scripts.
221 pub fn stop_all(&mut self) {
222 for (_, mut process) in self.processes.drain() {
223 process.stop();
224 }
225 self.panels.clear();
226 self.write_text_times.clear();
227 self.run_command_times.clear();
228 }
229
230 /// Check whether a `WriteText` command from `id` is within rate limits.
231 ///
232 /// Returns `true` (allowed) if at least `1000 / limit_per_sec` ms have
233 /// elapsed since the last allowed write. Updates the last-write timestamp
234 /// on success. `limit_per_sec == 0` uses the [`DEFAULT_WRITE_TEXT_RATE`].
235 pub fn check_write_text_rate(&mut self, id: ScriptId, limit_per_sec: u32) -> bool {
236 let rate = if limit_per_sec == 0 {
237 DEFAULT_WRITE_TEXT_RATE
238 } else {
239 limit_per_sec
240 };
241 let min_interval_ms = 1000u64 / rate as u64;
242 let now = std::time::Instant::now();
243 if self
244 .write_text_times
245 .get(&id)
246 .is_some_and(|last| (now.duration_since(*last).as_millis() as u64) < min_interval_ms)
247 {
248 return false;
249 }
250 self.write_text_times.insert(id, now);
251 true
252 }
253
254 /// Check whether a `RunCommand` from `id` is within rate limits.
255 ///
256 /// Returns `true` (allowed) if at least `1000 / limit_per_sec` ms have
257 /// elapsed since the last allowed run. Updates the last-run timestamp on
258 /// success. `limit_per_sec == 0` uses the [`DEFAULT_RUN_COMMAND_RATE`].
259 pub fn check_run_command_rate(&mut self, id: ScriptId, limit_per_sec: u32) -> bool {
260 let rate = if limit_per_sec == 0 {
261 DEFAULT_RUN_COMMAND_RATE
262 } else {
263 limit_per_sec
264 };
265 let min_interval_ms = 1000u64 / rate as u64;
266 let now = std::time::Instant::now();
267 if self
268 .run_command_times
269 .get(&id)
270 .is_some_and(|last| (now.duration_since(*last).as_millis() as u64) < min_interval_ms)
271 {
272 return false;
273 }
274 self.run_command_times.insert(id, now);
275 true
276 }
277
278 /// Get the panel state for a script.
279 ///
280 /// Returns `None` if the script ID has no panel set.
281 pub fn get_panel(&self, id: ScriptId) -> Option<&(String, String)> {
282 self.panels.get(&id)
283 }
284
285 /// Set the panel state (title, content) for a script.
286 pub fn set_panel(&mut self, id: ScriptId, title: String, content: String) {
287 self.panels.insert(id, (title, content));
288 }
289
290 /// Clear the panel state for a script.
291 pub fn clear_panel(&mut self, id: ScriptId) {
292 self.panels.remove(&id);
293 }
294
295 /// Get the IDs of all currently managed scripts.
296 pub fn script_ids(&self) -> Vec<ScriptId> {
297 self.processes.keys().copied().collect()
298 }
299}
300
301impl Default for ScriptManager {
302 fn default() -> Self {
303 Self::new()
304 }
305}
306
307impl Drop for ScriptManager {
308 fn drop(&mut self) {
309 self.stop_all();
310 }
311}