terminals.rs

  1use crate::Project;
  2use gpui::{AnyWindowHandle, Context, Entity, Model, ModelContext, WeakModel};
  3use settings::Settings;
  4use std::path::{Path, PathBuf};
  5use terminal::{
  6    terminal_settings::{self, TerminalSettings, VenvSettingsContent},
  7    Terminal, TerminalBuilder,
  8};
  9
 10#[cfg(target_os = "macos")]
 11use std::os::unix::ffi::OsStrExt;
 12
 13pub struct Terminals {
 14    pub(crate) local_handles: Vec<WeakModel<terminal::Terminal>>,
 15}
 16
 17impl Project {
 18    pub fn create_terminal(
 19        &mut self,
 20        working_directory: Option<PathBuf>,
 21        window: AnyWindowHandle,
 22        cx: &mut ModelContext<Self>,
 23    ) -> anyhow::Result<Model<Terminal>> {
 24        if self.is_remote() {
 25            return Err(anyhow::anyhow!(
 26                "creating terminals as a guest is not supported yet"
 27            ));
 28        } else {
 29            let settings = TerminalSettings::get_global(cx);
 30            let python_settings = settings.detect_venv.clone();
 31            let shell = settings.shell.clone();
 32
 33            let terminal = TerminalBuilder::new(
 34                working_directory.clone(),
 35                shell.clone(),
 36                settings.env.clone(),
 37                Some(settings.blinking.clone()),
 38                settings.alternate_scroll,
 39                window,
 40            )
 41            .map(|builder| {
 42                let terminal_handle = cx.new_model(|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: &Model<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<WeakModel<terminal::Terminal>> {
124        &self.terminals.local_handles
125    }
126}
127
128// TODO: Add a few tests for adding and removing terminal tabs