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")]
 11// use 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_command = Project::get_activate_command(python_settings);
 64                    let activate_script_path =
 65                        self.find_activate_script_path(python_settings, working_directory);
 66                    self.activate_python_virtual_environment(
 67                        activate_command,
 68                        activate_script_path,
 69                        &terminal_handle,
 70                        cx,
 71                    );
 72                }
 73                terminal_handle
 74            });
 75
 76            terminal
 77        }
 78    }
 79
 80    pub fn find_activate_script_path(
 81        &mut self,
 82        settings: &VenvSettingsContent,
 83        working_directory: Option<PathBuf>,
 84    ) -> Option<PathBuf> {
 85        // When we are unable to resolve the working directory, the terminal builder
 86        // defaults to '/'. We should probably encode this directly somewhere, but for
 87        // now, let's just hard code it here.
 88        let working_directory = working_directory.unwrap_or_else(|| Path::new("/").to_path_buf());
 89        let activate_script_name = match settings.activate_script {
 90            terminal_settings::ActivateScript::Default => "activate",
 91            terminal_settings::ActivateScript::Csh => "activate.csh",
 92            terminal_settings::ActivateScript::Fish => "activate.fish",
 93            terminal_settings::ActivateScript::Nushell => "activate.nu",
 94        };
 95
 96        for virtual_environment_name in settings.directories {
 97            let mut path = working_directory.join(virtual_environment_name);
 98            path.push("bin/");
 99            path.push(activate_script_name);
100
101            if path.exists() {
102                return Some(path);
103            }
104        }
105
106        None
107    }
108
109    fn get_activate_command(settings: &VenvSettingsContent) -> &'static str {
110        match settings.activate_script {
111            terminal_settings::ActivateScript::Nushell => "overlay use",
112            _ => "source",
113        }
114    }
115
116    fn activate_python_virtual_environment(
117        &mut self,
118        activate_command: &'static str,
119        activate_script: Option<PathBuf>,
120        terminal_handle: &Model<Terminal>,
121        cx: &mut ModelContext<Project>,
122    ) {
123        if let Some(activate_script) = activate_script {
124            // Paths are not strings so we need to jump through some hoops to format the command without `format!`
125            let mut command = Vec::from(activate_command.as_bytes());
126            command.push(b' ');
127            command.extend_from_slice(activate_script.as_os_str().as_encoded_bytes());
128            command.push(b'\n');
129
130            terminal_handle.update(cx, |this, _| this.input_bytes(command));
131        }
132    }
133
134    pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> {
135        &self.terminals.local_handles
136    }
137}
138
139// TODO: Add a few tests for adding and removing terminal tabs