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