Skip to main content

Mountain/IPC/WindServiceHandlers/Terminal/
LocalPTYCreateProcess.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Wire method: `localPty:createProcess`.
4//! VS Code's `IPtyService.createProcess` is typed `Promise<number>`.
5//! The workbench does `new LocalPty(id, …)` and keys `_ptys` by that integer;
6//! returning the full `{ id, name, pid }` object causes every subsequent
7//! `_ptys.get(<integer>)` lookup to return `undefined` and xterm to receive
8//! zero bytes. This handler strips down to the integer id.
9
10use std::sync::Arc;
11
12use serde_json::Value;
13
14use crate::{
15	IPC::WindServiceHandlers::Terminal::TerminalCreate::Fn as TerminalCreate,
16	RunTime::ApplicationRunTime::ApplicationRunTime,
17};
18
19pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
20	match TerminalCreate(RunTime, Arguments).await {
21		Ok(Response) => {
22			let TerminalIdOption = Response.get("id").and_then(serde_json::Value::as_u64);
23
24			match TerminalIdOption {
25				Some(TerminalId) if TerminalId > 0 => Ok(serde_json::json!(TerminalId)),
26
27				Some(_) | None => {
28					// Defensive: if `CreateTerminal` returned without a usable id
29					// (shape drift or `GetNextTerminalIdentifier` regression),
30					// surface an error so the workbench binds `LocalPty(0, …)`
31					// and every subsequent `_proxy.input(0, data)` fails loudly.
32					crate::dev_log!(
33						"terminal",
34						"error: [localPty:createProcess] CreateTerminal returned no usable id; response={:?}",
35						Response
36					);
37
38					Err(format!(
39						"localPty:createProcess: CreateTerminal returned no terminal id (response={})",
40						Response
41					))
42				},
43			}
44		},
45
46		Err(Error) => Err(Error),
47	}
48}