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