1use crate::Project;
2use gpui::{AnyWindowHandle, ModelContext, ModelHandle, WeakModelHandle};
3use std::path::{Path, PathBuf};
4use terminal::{
5 terminal_settings::{self, TerminalSettings, VenvSettingsContent},
6 Terminal, TerminalBuilder,
7};
8
9#[cfg(target_os = "macos")]
10use std::os::unix::ffi::OsStrExt;
11
12pub struct Terminals {
13 pub(crate) local_handles: Vec<WeakModelHandle<terminal::Terminal>>,
14}
15
16impl Project {
17 pub fn create_terminal(
18 &mut self,
19 working_directory: Option<PathBuf>,
20 window: AnyWindowHandle,
21 cx: &mut ModelContext<Self>,
22 ) -> anyhow::Result<ModelHandle<Terminal>> {
23 if self.is_remote() {
24 return Err(anyhow::anyhow!(
25 "creating terminals as a guest is not supported yet"
26 ));
27 } else {
28 let settings = settings::get::<TerminalSettings>(cx);
29 let python_settings = settings.detect_venv.clone();
30 let shell = settings.shell.clone();
31
32 let terminal = TerminalBuilder::new(
33 working_directory.clone(),
34 shell.clone(),
35 settings.env.clone(),
36 Some(settings.blinking.clone()),
37 settings.alternate_scroll,
38 window,
39 )
40 .map(|builder| {
41 let terminal_handle = cx.add_model(|cx| builder.subscribe(cx));
42
43 self.terminals
44 .local_handles
45 .push(terminal_handle.downgrade());
46
47 let id = terminal_handle.id();
48 cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
49 let handles = &mut project.terminals.local_handles;
50
51 if let Some(index) = handles.iter().position(|terminal| terminal.id() == id) {
52 handles.remove(index);
53 cx.notify();
54 }
55 })
56 .detach();
57
58 if let Some(python_settings) = &python_settings.as_option() {
59 let activate_script_path =
60 self.find_activate_script_path(&python_settings, working_directory);
61 self.activate_python_virtual_environment(
62 activate_script_path,
63 &terminal_handle,
64 cx,
65 );
66 }
67 terminal_handle
68 });
69
70 terminal
71 }
72 }
73
74 pub fn find_activate_script_path(
75 &mut self,
76 settings: &VenvSettingsContent,
77 working_directory: Option<PathBuf>,
78 ) -> Option<PathBuf> {
79 // When we are unable to resolve the working directory, the terminal builder
80 // defaults to '/'. We should probably encode this directly somewhere, but for
81 // now, let's just hard code it here.
82 let working_directory = working_directory.unwrap_or_else(|| Path::new("/").to_path_buf());
83 let activate_script_name = match settings.activate_script {
84 terminal_settings::ActivateScript::Default => "activate",
85 terminal_settings::ActivateScript::Csh => "activate.csh",
86 terminal_settings::ActivateScript::Fish => "activate.fish",
87 terminal_settings::ActivateScript::Nushell => "activate.nu",
88 };
89
90 for virtual_environment_name in settings.directories {
91 let mut path = working_directory.join(virtual_environment_name);
92 path.push("bin/");
93 path.push(activate_script_name);
94
95 if path.exists() {
96 return Some(path);
97 }
98 }
99
100 None
101 }
102
103 fn activate_python_virtual_environment(
104 &mut self,
105 activate_script: Option<PathBuf>,
106 terminal_handle: &ModelHandle<Terminal>,
107 cx: &mut ModelContext<Project>,
108 ) {
109 if let Some(activate_script) = activate_script {
110 // Paths are not strings so we need to jump through some hoops to format the command without `format!`
111 let mut command = Vec::from("source ".as_bytes());
112 command.extend_from_slice(activate_script.as_os_str().as_bytes());
113 command.push(b'\n');
114
115 terminal_handle.update(cx, |this, _| this.input_bytes(command));
116 }
117 }
118
119 pub fn local_terminal_handles(&self) -> &Vec<WeakModelHandle<terminal::Terminal>> {
120 &self.terminals.local_handles
121 }
122}
123
124// TODO: Add a few tests for adding and removing terminal tabs