DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Terminal/
AttachToProcess.rs1#![allow(non_snake_case)]
2
3use std::sync::Arc;
16
17use CommonLibrary::{Environment::Requires::Requires, Terminal::TerminalProvider::TerminalProvider};
18use serde_json::{Value, json};
19
20use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
21
22pub async fn AttachToProcess(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
23 let TerminalId = match Arguments.first() {
24 Some(Value::Number(N)) => N.as_u64().unwrap_or(0),
25 Some(Value::Object(Obj)) => Obj.get("id").and_then(Value::as_u64).unwrap_or(0),
26 _ => 0,
27 };
28
29 if TerminalId == 0 {
30 dev_log!("terminal", "warn: [AttachToProcess] called with id=0, ignoring");
31 return Ok(Value::Null);
32 }
33
34 let Provider:Arc<dyn TerminalProvider> = RunTime.Environment.Require();
35
36 match Provider.GetTerminalProcessId(TerminalId).await {
39 Ok(Some(Pid)) => {
40 dev_log!("terminal", "[AttachToProcess] attached id={} pid={}", TerminalId, Pid);
41 Ok(json!({ "id": TerminalId, "pid": Pid }))
42 },
43 Ok(None) => {
44 dev_log!(
45 "terminal",
46 "warn: [AttachToProcess] id={} not found in active terminals",
47 TerminalId
48 );
49 Ok(Value::Null)
50 },
51 Err(Error) => {
52 dev_log!("terminal", "warn: [AttachToProcess] id={} error: {}", TerminalId, Error);
53 Ok(Value::Null)
54 },
55 }
56}