1use crate::Project;
2use gpui::{AnyWindowHandle, Context, Entity, Model, ModelContext, WeakModel};
3use settings::Settings;
4use smol::channel::bounded;
5use std::path::{Path, PathBuf};
6use terminal::{
7 terminal_settings::{self, Shell, TerminalSettings, VenvSettingsContent},
8 RunableState, SpawnRunnable, Terminal, TerminalBuilder,
9};
10
11// #[cfg(target_os = "macos")]
12// use std::os::unix::ffi::OsStrExt;
13
14pub struct Terminals {
15 pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>,
16}
17
18impl Project {
19 pub fn create_terminal(
20 &mut self,
21 working_directory: Option<PathBuf>,
22 spawn_runnable: Option<SpawnRunnable>,
23 window: AnyWindowHandle,
24 cx: &mut ModelContext<Self>,
25 ) -> anyhow::Result<Model<Terminal>> {
26 anyhow::ensure!(
27 !self.is_remote(),
28 "creating terminals as a guest is not supported yet"
29 );
30
31 let settings = TerminalSettings::get_global(cx);
32 let python_settings = settings.detect_venv.clone();
33 let (completion_tx, completion_rx) = bounded(1);
34 let mut env = settings.env.clone();
35 let (spawn_runnable, shell) = if let Some(spawn_runnable) = spawn_runnable {
36 env.extend(spawn_runnable.env);
37 (
38 Some(RunableState {
39 id: spawn_runnable.id,
40 label: spawn_runnable.label,
41 completed: false,
42 completion_rx,
43 }),
44 Shell::WithArguments {
45 program: spawn_runnable.command,
46 args: spawn_runnable.args,
47 },
48 )
49 } else {
50 (None, settings.shell.clone())
51 };
52
53 let terminal = TerminalBuilder::new(
54 working_directory.clone(),
55 spawn_runnable,
56 shell,
57 env,
58 Some(settings.blinking.clone()),
59 settings.alternate_scroll,
60 window,
61 completion_tx,
62 )
63 .map(|builder| {
64 let terminal_handle = cx.new_model(|cx| builder.subscribe(cx));
65
66 self.terminals
67 .local_handles
68 .push(terminal_handle.downgrade());
69
70 let id = terminal_handle.entity_id();
71 cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
72 let handles = &mut project.terminals.local_handles;
73
74 if let Some(index) = handles
75 .iter()
76 .position(|terminal| terminal.entity_id() == id)
77 {
78 handles.remove(index);
79 cx.notify();
80 }
81 })
82 .detach();
83
84 if let Some(python_settings) = &python_settings.as_option() {
85 let activate_command = Project::get_activate_command(python_settings);
86 let activate_script_path =
87 self.find_activate_script_path(python_settings, working_directory);
88 self.activate_python_virtual_environment(
89 activate_command,
90 activate_script_path,
91 &terminal_handle,
92 cx,
93 );
94 }
95 terminal_handle
96 });
97
98 terminal
99 }
100
101 pub fn find_activate_script_path(
102 &mut self,
103 settings: &VenvSettingsContent,
104 working_directory: Option<PathBuf>,
105 ) -> Option<PathBuf> {
106 // When we are unable to resolve the working directory, the terminal builder
107 // defaults to '/'. We should probably encode this directly somewhere, but for
108 // now, let's just hard code it here.
109 let working_directory = working_directory.unwrap_or_else(|| Path::new("/").to_path_buf());
110 let activate_script_name = match settings.activate_script {
111 terminal_settings::ActivateScript::Default => "activate",
112 terminal_settings::ActivateScript::Csh => "activate.csh",
113 terminal_settings::ActivateScript::Fish => "activate.fish",
114 terminal_settings::ActivateScript::Nushell => "activate.nu",
115 };
116
117 for virtual_environment_name in settings.directories {
118 let mut path = working_directory.join(virtual_environment_name);
119 path.push("bin/");
120 path.push(activate_script_name);
121
122 if path.exists() {
123 return Some(path);
124 }
125 }
126
127 None
128 }
129
130 fn get_activate_command(settings: &VenvSettingsContent) -> &'static str {
131 match settings.activate_script {
132 terminal_settings::ActivateScript::Nushell => "overlay use",
133 _ => "source",
134 }
135 }
136
137 fn activate_python_virtual_environment(
138 &mut self,
139 activate_command: &'static str,
140 activate_script: Option<PathBuf>,
141 terminal_handle: &Model<Terminal>,
142 cx: &mut ModelContext<Project>,
143 ) {
144 if let Some(activate_script) = activate_script {
145 // Paths are not strings so we need to jump through some hoops to format the command without `format!`
146 let mut command = Vec::from(activate_command.as_bytes());
147 command.push(b' ');
148 // Wrapping path in double quotes to catch spaces in folder name
149 command.extend_from_slice(b"\"");
150 command.extend_from_slice(activate_script.as_os_str().as_encoded_bytes());
151 command.extend_from_slice(b"\"");
152 command.push(b'\n');
153
154 terminal_handle.update(cx, |this, _| this.input_bytes(command));
155 }
156 }
157
158 pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> {
159 &self.terminals.local_handles
160 }
161}
162
163// TODO: Add a few tests for adding and removing terminal tabs