1use crate::Project;
2use collections::HashMap;
3use gpui::{
4 AnyWindowHandle, AppContext, Context, Entity, Model, ModelContext, SharedString, WeakModel,
5};
6use settings::{Settings, SettingsLocation};
7use smol::channel::bounded;
8use std::path::{Path, PathBuf};
9use task::SpawnInTerminal;
10use terminal::{
11 terminal_settings::{self, Shell, TerminalSettings, VenvSettingsContent},
12 TaskState, TaskStatus, Terminal, TerminalBuilder,
13};
14use util::ResultExt;
15
16// #[cfg(target_os = "macos")]
17// use std::os::unix::ffi::OsStrExt;
18
19pub struct Terminals {
20 pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>,
21}
22
23#[derive(Debug, Clone)]
24pub struct ConnectRemoteTerminal {
25 pub ssh_connection_string: SharedString,
26 pub project_path: SharedString,
27}
28
29impl Project {
30 pub fn remote_terminal_connection_data(
31 &self,
32 cx: &AppContext,
33 ) -> Option<ConnectRemoteTerminal> {
34 self.dev_server_project_id()
35 .and_then(|dev_server_project_id| {
36 let projects_store = dev_server_projects::Store::global(cx).read(cx);
37 let project_path = projects_store
38 .dev_server_project(dev_server_project_id)?
39 .path
40 .clone();
41 let ssh_connection_string = projects_store
42 .dev_server_for_project(dev_server_project_id)?
43 .ssh_connection_string
44 .clone();
45 Some(project_path).zip(ssh_connection_string)
46 })
47 .map(
48 |(project_path, ssh_connection_string)| ConnectRemoteTerminal {
49 ssh_connection_string,
50 project_path,
51 },
52 )
53 }
54
55 pub fn create_terminal(
56 &mut self,
57 working_directory: Option<PathBuf>,
58 spawn_task: Option<SpawnInTerminal>,
59 window: AnyWindowHandle,
60 cx: &mut ModelContext<Self>,
61 ) -> anyhow::Result<Model<Terminal>> {
62 let remote_connection_data = if self.is_remote() {
63 let remote_connection_data = self.remote_terminal_connection_data(cx);
64 if remote_connection_data.is_none() {
65 anyhow::bail!("Cannot create terminal for remote project without connection data")
66 }
67 remote_connection_data
68 } else {
69 None
70 };
71
72 // used only for TerminalSettings::get
73 let worktree = {
74 let terminal_cwd = working_directory.as_deref();
75 let task_cwd = spawn_task
76 .as_ref()
77 .and_then(|spawn_task| spawn_task.cwd.as_deref());
78
79 terminal_cwd
80 .and_then(|terminal_cwd| self.find_local_worktree(terminal_cwd, cx))
81 .or_else(|| task_cwd.and_then(|spawn_cwd| self.find_local_worktree(spawn_cwd, cx)))
82 };
83
84 let settings_location = worktree.as_ref().map(|(worktree, path)| SettingsLocation {
85 worktree_id: worktree.read(cx).id().to_usize(),
86 path,
87 });
88
89 let is_terminal = spawn_task.is_none() && remote_connection_data.is_none();
90 let settings = TerminalSettings::get(settings_location, cx);
91 let python_settings = settings.detect_venv.clone();
92 let (completion_tx, completion_rx) = bounded(1);
93
94 let mut env = settings.env.clone();
95 // Alacritty uses parent project's working directory when no working directory is provided
96 // https://github.com/alacritty/alacritty/blob/fd1a3cc79192d1d03839f0fd8c72e1f8d0fce42e/extra/man/alacritty.5.scd?plain=1#L47-L52
97
98 let venv_base_directory = working_directory
99 .as_deref()
100 .unwrap_or_else(|| Path::new(""));
101
102 let (spawn_task, shell) = if let Some(remote_connection_data) = remote_connection_data {
103 log::debug!("Connecting to a remote server: {remote_connection_data:?}");
104 // Alacritty sets its terminfo to `alacritty`, this requiring hosts to have it installed
105 // to properly display colors.
106 // We do not have the luxury of assuming the host has it installed,
107 // so we set it to a default that does not break the highlighting via ssh.
108 env.entry("TERM".to_string())
109 .or_insert_with(|| "xterm-256color".to_string());
110
111 (
112 None,
113 Shell::WithArguments {
114 program: "ssh".to_string(),
115 args: vec![
116 remote_connection_data.ssh_connection_string.to_string(),
117 "-t".to_string(),
118 format!(
119 "cd {} && exec $SHELL -l",
120 escape_path_for_shell(remote_connection_data.project_path.as_ref())
121 ),
122 ],
123 },
124 )
125 } else if let Some(spawn_task) = spawn_task {
126 log::debug!("Spawning task: {spawn_task:?}");
127 env.extend(spawn_task.env);
128 // Activate minimal Python virtual environment
129 if let Some(python_settings) = &python_settings.as_option() {
130 self.set_python_venv_path_for_tasks(python_settings, venv_base_directory, &mut env);
131 }
132 (
133 Some(TaskState {
134 id: spawn_task.id,
135 full_label: spawn_task.full_label,
136 label: spawn_task.label,
137 command_label: spawn_task.command_label,
138 status: TaskStatus::Running,
139 completion_rx,
140 }),
141 Shell::WithArguments {
142 program: spawn_task.command,
143 args: spawn_task.args,
144 },
145 )
146 } else {
147 (None, settings.shell.clone())
148 };
149
150 let terminal = TerminalBuilder::new(
151 working_directory.clone(),
152 spawn_task,
153 shell,
154 env,
155 Some(settings.blinking.clone()),
156 settings.alternate_scroll,
157 settings.max_scroll_history_lines,
158 window,
159 completion_tx,
160 )
161 .map(|builder| {
162 let terminal_handle = cx.new_model(|cx| builder.subscribe(cx));
163
164 self.terminals
165 .local_handles
166 .push(terminal_handle.downgrade());
167
168 let id = terminal_handle.entity_id();
169 cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
170 let handles = &mut project.terminals.local_handles;
171
172 if let Some(index) = handles
173 .iter()
174 .position(|terminal| terminal.entity_id() == id)
175 {
176 handles.remove(index);
177 cx.notify();
178 }
179 })
180 .detach();
181
182 // if the terminal is not a task, activate full Python virtual environment
183 if is_terminal {
184 if let Some(python_settings) = &python_settings.as_option() {
185 if let Some(activate_script_path) =
186 self.find_activate_script_path(python_settings, venv_base_directory)
187 {
188 self.activate_python_virtual_environment(
189 Project::get_activate_command(python_settings),
190 activate_script_path,
191 &terminal_handle,
192 cx,
193 );
194 }
195 }
196 }
197 terminal_handle
198 });
199
200 terminal
201 }
202
203 pub fn find_activate_script_path(
204 &mut self,
205 settings: &VenvSettingsContent,
206 venv_base_directory: &Path,
207 ) -> Option<PathBuf> {
208 let activate_script_name = match settings.activate_script {
209 terminal_settings::ActivateScript::Default => "activate",
210 terminal_settings::ActivateScript::Csh => "activate.csh",
211 terminal_settings::ActivateScript::Fish => "activate.fish",
212 terminal_settings::ActivateScript::Nushell => "activate.nu",
213 };
214
215 settings
216 .directories
217 .into_iter()
218 .find_map(|virtual_environment_name| {
219 let path = venv_base_directory
220 .join(virtual_environment_name)
221 .join("bin")
222 .join(activate_script_name);
223 path.exists().then_some(path)
224 })
225 }
226
227 pub fn set_python_venv_path_for_tasks(
228 &mut self,
229 settings: &VenvSettingsContent,
230 venv_base_directory: &Path,
231 env: &mut HashMap<String, String>,
232 ) {
233 let activate_path = settings
234 .directories
235 .into_iter()
236 .find_map(|virtual_environment_name| {
237 let path = venv_base_directory.join(virtual_environment_name);
238 path.exists().then_some(path)
239 });
240
241 if let Some(path) = activate_path {
242 // Some tools use VIRTUAL_ENV to detect the virtual environment
243 env.insert(
244 "VIRTUAL_ENV".to_string(),
245 path.to_string_lossy().to_string(),
246 );
247
248 let path_bin = path.join("bin");
249 // We need to set the PATH to include the virtual environment's bin directory
250 if let Some(paths) = std::env::var_os("PATH") {
251 let paths = std::iter::once(path_bin).chain(std::env::split_paths(&paths));
252 if let Some(new_path) = std::env::join_paths(paths).log_err() {
253 env.insert("PATH".to_string(), new_path.to_string_lossy().to_string());
254 }
255 } else {
256 env.insert(
257 "PATH".to_string(),
258 path.join("bin").to_string_lossy().to_string(),
259 );
260 }
261 }
262 }
263
264 fn get_activate_command(settings: &VenvSettingsContent) -> &'static str {
265 match settings.activate_script {
266 terminal_settings::ActivateScript::Nushell => "overlay use",
267 _ => "source",
268 }
269 }
270
271 fn activate_python_virtual_environment(
272 &mut self,
273 activate_command: &'static str,
274 activate_script: PathBuf,
275 terminal_handle: &Model<Terminal>,
276 cx: &mut ModelContext<Project>,
277 ) {
278 // Paths are not strings so we need to jump through some hoops to format the command without `format!`
279 let mut command = Vec::from(activate_command.as_bytes());
280 command.push(b' ');
281 // Wrapping path in double quotes to catch spaces in folder name
282 command.extend_from_slice(b"\"");
283 command.extend_from_slice(activate_script.as_os_str().as_encoded_bytes());
284 command.extend_from_slice(b"\"");
285 command.push(b'\n');
286
287 terminal_handle.update(cx, |this, _| this.input_bytes(command));
288 }
289
290 pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> {
291 &self.terminals.local_handles
292 }
293}
294
295#[cfg(unix)]
296fn escape_path_for_shell(input: &str) -> String {
297 input
298 .chars()
299 .fold(String::with_capacity(input.len()), |mut s, c| {
300 match c {
301 ' ' | '"' | '\'' | '\\' | '(' | ')' | '{' | '}' | '[' | ']' | '|' | ';' | '&'
302 | '<' | '>' | '*' | '?' | '$' | '#' | '!' | '=' | '^' | '%' | ':' => {
303 s.push('\\');
304 s.push('\\');
305 s.push(c);
306 }
307 _ => s.push(c),
308 }
309 s
310 })
311}
312
313#[cfg(windows)]
314fn escape_path_for_shell(input: &str) -> String {
315 input
316 .chars()
317 .fold(String::with_capacity(input.len()), |mut s, c| {
318 match c {
319 '^' | '&' | '|' | '<' | '>' | ' ' | '(' | ')' | '@' | '`' | '=' | ';' | '%' => {
320 s.push('^');
321 s.push(c);
322 }
323 _ => s.push(c),
324 }
325 s
326 })
327}
328
329// TODO: Add a few tests for adding and removing terminal tabs