terminals.rs

  1use crate::Project;
  2use gpui2::{AnyWindowHandle, Context, Handle, ModelContext, WeakHandle};
  3use std::path::{Path, PathBuf};
  4use terminal2::{
  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<WeakHandle<terminal2::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<Handle<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 = settings2::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                |index, cx| todo!("color_for_index"),
 40            )
 41            .map(|builder| {
 42                let terminal_handle = cx.entity(|cx| builder.subscribe(cx));
 43
 44                self.terminals
 45                    .local_handles
 46                    .push(terminal_handle.downgrade());
 47
 48                let id = terminal_handle.entity_id();
 49                cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
 50                    let handles = &mut project.terminals.local_handles;
 51
 52                    if let Some(index) = handles
 53                        .iter()
 54                        .position(|terminal| terminal.entity_id() == id)
 55                    {
 56                        handles.remove(index);
 57                        cx.notify();
 58                    }
 59                })
 60                .detach();
 61
 62                if let Some(python_settings) = &python_settings.as_option() {
 63                    let activate_script_path =
 64                        self.find_activate_script_path(&python_settings, working_directory);
 65                    self.activate_python_virtual_environment(
 66                        activate_script_path,
 67                        &terminal_handle,
 68                        cx,
 69                    );
 70                }
 71                terminal_handle
 72            });
 73
 74            terminal
 75        }
 76    }
 77
 78    pub fn find_activate_script_path(
 79        &mut self,
 80        settings: &VenvSettingsContent,
 81        working_directory: Option<PathBuf>,
 82    ) -> Option<PathBuf> {
 83        // When we are unable to resolve the working directory, the terminal builder
 84        // defaults to '/'. We should probably encode this directly somewhere, but for
 85        // now, let's just hard code it here.
 86        let working_directory = working_directory.unwrap_or_else(|| Path::new("/").to_path_buf());
 87        let activate_script_name = match settings.activate_script {
 88            terminal_settings::ActivateScript::Default => "activate",
 89            terminal_settings::ActivateScript::Csh => "activate.csh",
 90            terminal_settings::ActivateScript::Fish => "activate.fish",
 91            terminal_settings::ActivateScript::Nushell => "activate.nu",
 92        };
 93
 94        for virtual_environment_name in settings.directories {
 95            let mut path = working_directory.join(virtual_environment_name);
 96            path.push("bin/");
 97            path.push(activate_script_name);
 98
 99            if path.exists() {
100                return Some(path);
101            }
102        }
103
104        None
105    }
106
107    fn activate_python_virtual_environment(
108        &mut self,
109        activate_script: Option<PathBuf>,
110        terminal_handle: &Handle<Terminal>,
111        cx: &mut ModelContext<Project>,
112    ) {
113        if let Some(activate_script) = activate_script {
114            // Paths are not strings so we need to jump through some hoops to format the command without `format!`
115            let mut command = Vec::from("source ".as_bytes());
116            command.extend_from_slice(activate_script.as_os_str().as_bytes());
117            command.push(b'\n');
118
119            terminal_handle.update(cx, |this, _| this.input_bytes(command));
120        }
121    }
122
123    pub fn local_terminal_handles(&self) -> &Vec<WeakHandle<terminal2::Terminal>> {
124        &self.terminals.local_handles
125    }
126}
127
128// TODO: Add a few tests for adding and removing terminal tabs