Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Terminal.rs

1use std::sync::Arc;
2
3use CommonLibrary::{Environment::Requires::Requires, Terminal::TerminalProvider::TerminalProvider};
4use serde_json::{Value, json};
5use tauri::Runtime;
6
7use crate::{
8	Track::Effect::{
9		CreateEffectForRequest::Utilities::Params::{bool_at, i64_at, string_at, u64_at, u64_at_or, val_at},
10		MappedEffectType::MappedEffect,
11	},
12	dev_log,
13};
14
15pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
16	match MethodName {
17		"$terminal:create" => {
18			crate::effect!(run_time, {
19				let provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
20
21				let options = val_at(&Parameters, 0);
22
23				provider.CreateTerminal(options).await.map_err(|e| e.to_string())
24			})
25		},
26
27		"$terminal:sendText" => {
28			crate::effect!(run_time, {
29				let provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
30
31				let terminal_id = i64_at(&Parameters, 0) as u64;
32
33				let text = string_at(&Parameters, 1);
34
35				provider
36					.SendTextToTerminal(terminal_id, text)
37					.await
38					.map(|_| json!(null))
39					.map_err(|e| e.to_string())
40			})
41		},
42
43		"$terminal:dispose" => {
44			crate::effect!(run_time, {
45				let provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
46
47				let terminal_id = i64_at(&Parameters, 0) as u64;
48
49				provider
50					.DisposeTerminal(terminal_id)
51					.await
52					.map(|_| json!(null))
53					.map_err(|e| e.to_string())
54			})
55		},
56
57		"Terminal.Resize" | "$terminal:resize" => {
58			crate::effect!(run_time, {
59				let provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
60
61				let terminal_id = match Parameters.get(0) {
62					Some(Value::Number(n)) => n.as_u64().unwrap_or(0),
63					Some(Value::String(s)) => {
64						s.rsplit(':').next().and_then(|token| token.parse::<u64>().ok()).unwrap_or(0)
65					},
66					_ => 0,
67				};
68
69				let cols = u64_at_or(&Parameters, 1, 80) as u16;
70
71				let rows = u64_at_or(&Parameters, 2, 24) as u16;
72
73				provider
74					.ResizeTerminal(terminal_id, cols, rows)
75					.await
76					.map(|()| json!(null))
77					.map_err(|e| e.to_string())
78			})
79		},
80
81		"Terminal.GetProcessId" => {
82			crate::effect!(run_time, {
83				let Provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
84
85				let Handle = val_at(&Parameters, 0);
86
87				let Id:u64 = if let Some(n) = Handle.as_u64() {
88					n
89				} else if let Some(s) = Handle.as_str() {
90					s.trim_start_matches("terminal:").parse().unwrap_or(0)
91				} else {
92					0
93				};
94
95				match Provider.GetTerminalProcessId(Id).await {
96					Ok(Some(Pid)) => Ok(json!(Pid)),
97					Ok(None) => Ok(Value::Null),
98					Err(Error) => Err(Error.to_string()),
99				}
100			})
101		},
102
103		// `$terminal:show` / `$terminal:hide` - Cocoon's window namespace shim
104		// calls these when extensions invoke `terminal.show(preserveFocus)` or
105		// `terminal.hide()`. Wire through the same `ShowTerminal` / `HideTerminal`
106		// provider path that the `terminal:show` / `terminal:hide` IPC handlers
107		// use so both call sites share one implementation.
108		"$terminal:show" | "Terminal.Show" => {
109			crate::effect!(run_time, {
110				let provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
111
112				let terminal_id = u64_at(&Parameters, 0);
113
114				let preserve_focus = bool_at(&Parameters, 1);
115
116				provider
117					.ShowTerminal(terminal_id, preserve_focus)
118					.await
119					.map(|()| json!(null))
120					.map_err(|e| e.to_string())
121			})
122		},
123
124		"$terminal:hide" | "Terminal.Hide" => {
125			crate::effect!(run_time, {
126				let provider:Arc<dyn TerminalProvider> = run_time.Environment.Require();
127
128				let terminal_id = u64_at(&Parameters, 0);
129
130				provider
131					.HideTerminal(terminal_id)
132					.await
133					.map(|()| json!(null))
134					.map_err(|e| e.to_string())
135			})
136		},
137
138		_ => None,
139	}
140}