1use crate::Project;
2use collections::HashMap;
3use gpui::{AnyWindowHandle, Context, Entity, Model, ModelContext, WeakModel};
4use settings::Settings;
5use smol::channel::bounded;
6use std::path::{Path, PathBuf};
7use terminal::{
8 terminal_settings::{self, Shell, TerminalSettings, VenvSettingsContent},
9 SpawnTask, TaskState, TaskStatus, Terminal, TerminalBuilder,
10};
11use util::ResultExt;
12
13// #[cfg(target_os = "macos")]
14// use std::os::unix::ffi::OsStrExt;
15
16pub struct Terminals {
17 pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>,
18}
19
20impl Project {
21 pub fn create_terminal(
22 &mut self,
23 working_directory: Option<PathBuf>,
24 spawn_task: Option<SpawnTask>,
25 window: AnyWindowHandle,
26 cx: &mut ModelContext<Self>,
27 ) -> anyhow::Result<Model<Terminal>> {
28 anyhow::ensure!(
29 !self.is_remote(),
30 "creating terminals as a guest is not supported yet"
31 );
32
33 let is_terminal = spawn_task.is_none();
34 let settings = TerminalSettings::get_global(cx);
35 let python_settings = settings.detect_venv.clone();
36 let (completion_tx, completion_rx) = bounded(1);
37
38 let mut env = settings.env.clone();
39 // Alacritty uses parent project's working directory when no working directory is provided
40 // https://github.com/alacritty/alacritty/blob/fd1a3cc79192d1d03839f0fd8c72e1f8d0fce42e/extra/man/alacritty.5.scd?plain=1#L47-L52
41
42 let venv_base_directory = working_directory
43 .as_deref()
44 .unwrap_or_else(|| Path::new(""));
45
46 let (spawn_task, shell) = if let Some(spawn_task) = spawn_task {
47 log::debug!("Spawning task: {spawn_task:?}");
48 env.extend(spawn_task.env);
49 // Activate minimal Python virtual environment
50 if let Some(python_settings) = &python_settings.as_option() {
51 self.set_python_venv_path_for_tasks(python_settings, venv_base_directory, &mut env);
52 }
53 (
54 Some(TaskState {
55 id: spawn_task.id,
56 full_label: spawn_task.full_label,
57 label: spawn_task.label,
58 command_label: spawn_task.args.iter().fold(
59 spawn_task.command.clone(),
60 |mut command_label, new_arg| {
61 command_label.push(' ');
62 command_label.push_str(new_arg);
63 command_label
64 },
65 ),
66 status: TaskStatus::Running,
67 completion_rx,
68 }),
69 Shell::WithArguments {
70 program: spawn_task.command,
71 args: spawn_task.args,
72 },
73 )
74 } else {
75 (None, settings.shell.clone())
76 };
77
78 let terminal = TerminalBuilder::new(
79 working_directory.clone(),
80 spawn_task,
81 shell,
82 env,
83 Some(settings.blinking.clone()),
84 settings.alternate_scroll,
85 settings.max_scroll_history_lines,
86 window,
87 completion_tx,
88 )
89 .map(|builder| {
90 let terminal_handle = cx.new_model(|cx| builder.subscribe(cx));
91
92 self.terminals
93 .local_handles
94 .push(terminal_handle.downgrade());
95
96 let id = terminal_handle.entity_id();
97 cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
98 let handles = &mut project.terminals.local_handles;
99
100 if let Some(index) = handles
101 .iter()
102 .position(|terminal| terminal.entity_id() == id)
103 {
104 handles.remove(index);
105 cx.notify();
106 }
107 })
108 .detach();
109
110 // if the terminal is not a task, activate full Python virtual environment
111 if is_terminal {
112 if let Some(python_settings) = &python_settings.as_option() {
113 if let Some(activate_script_path) =
114 self.find_activate_script_path(python_settings, venv_base_directory)
115 {
116 self.activate_python_virtual_environment(
117 Project::get_activate_command(python_settings),
118 activate_script_path,
119 &terminal_handle,
120 cx,
121 );
122 }
123 }
124 }
125 terminal_handle
126 });
127
128 terminal
129 }
130
131 pub fn find_activate_script_path(
132 &mut self,
133 settings: &VenvSettingsContent,
134 venv_base_directory: &Path,
135 ) -> Option<PathBuf> {
136 let activate_script_name = match settings.activate_script {
137 terminal_settings::ActivateScript::Default => "activate",
138 terminal_settings::ActivateScript::Csh => "activate.csh",
139 terminal_settings::ActivateScript::Fish => "activate.fish",
140 terminal_settings::ActivateScript::Nushell => "activate.nu",
141 };
142
143 settings
144 .directories
145 .into_iter()
146 .find_map(|virtual_environment_name| {
147 let path = venv_base_directory
148 .join(virtual_environment_name)
149 .join("bin")
150 .join(activate_script_name);
151 path.exists().then_some(path)
152 })
153 }
154
155 pub fn set_python_venv_path_for_tasks(
156 &mut self,
157 settings: &VenvSettingsContent,
158 venv_base_directory: &Path,
159 env: &mut HashMap<String, String>,
160 ) {
161 let activate_path = settings
162 .directories
163 .into_iter()
164 .find_map(|virtual_environment_name| {
165 let path = venv_base_directory.join(virtual_environment_name);
166 path.exists().then_some(path)
167 });
168
169 if let Some(path) = activate_path {
170 // Some tools use VIRTUAL_ENV to detect the virtual environment
171 env.insert(
172 "VIRTUAL_ENV".to_string(),
173 path.to_string_lossy().to_string(),
174 );
175
176 let path_bin = path.join("bin");
177 // We need to set the PATH to include the virtual environment's bin directory
178 if let Some(paths) = std::env::var_os("PATH") {
179 let paths = std::iter::once(path_bin).chain(std::env::split_paths(&paths));
180 if let Some(new_path) = std::env::join_paths(paths).log_err() {
181 env.insert("PATH".to_string(), new_path.to_string_lossy().to_string());
182 }
183 } else {
184 env.insert(
185 "PATH".to_string(),
186 path.join("bin").to_string_lossy().to_string(),
187 );
188 }
189 }
190 }
191
192 fn get_activate_command(settings: &VenvSettingsContent) -> &'static str {
193 match settings.activate_script {
194 terminal_settings::ActivateScript::Nushell => "overlay use",
195 _ => "source",
196 }
197 }
198
199 fn activate_python_virtual_environment(
200 &mut self,
201 activate_command: &'static str,
202 activate_script: PathBuf,
203 terminal_handle: &Model<Terminal>,
204 cx: &mut ModelContext<Project>,
205 ) {
206 // Paths are not strings so we need to jump through some hoops to format the command without `format!`
207 let mut command = Vec::from(activate_command.as_bytes());
208 command.push(b' ');
209 // Wrapping path in double quotes to catch spaces in folder name
210 command.extend_from_slice(b"\"");
211 command.extend_from_slice(activate_script.as_os_str().as_encoded_bytes());
212 command.extend_from_slice(b"\"");
213 command.push(b'\n');
214
215 terminal_handle.update(cx, |this, _| this.input_bytes(command));
216 }
217
218 pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> {
219 &self.terminals.local_handles
220 }
221}
222
223// TODO: Add a few tests for adding and removing terminal tabs