From daa70022c9c8f54abae42f6830814f8ede3ec7b6 Mon Sep 17 00:00:00 2001 From: Aram Drevekenin Date: Wed, 3 Nov 2021 17:59:28 +0100 Subject: [PATCH] fix(cwd): open pane in cwd even if default shell is explicit (#834) * fix(cwd): open pane in cwd even if default shell is explicit * style(fmt): make rustfmt happy --- zellij-server/src/pty.rs | 51 ++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/zellij-server/src/pty.rs b/zellij-server/src/pty.rs index 93699a32..8c5a1133 100644 --- a/zellij-server/src/pty.rs +++ b/zellij-server/src/pty.rs @@ -267,36 +267,51 @@ impl Pty { task_handles: HashMap::new(), } } - pub fn get_default_terminal(&self, client_id: Option) -> TerminalAction { + pub fn get_default_terminal(&self) -> TerminalAction { TerminalAction::RunCommand(RunCommand { args: vec![], command: PathBuf::from(env::var("SHELL").expect("Could not find the SHELL variable")), - cwd: client_id - .and_then(|client_id| self.active_panes.get(&client_id)) - .and_then(|pane| match pane { - PaneId::Plugin(..) => None, - PaneId::Terminal(id) => self.id_to_child_pid.get(id), - }) - .and_then(|id| { - self.bus - .os_input - .as_ref() - .map(|input| input.get_cwd(Pid::from_raw(*id))) - }) - .flatten(), + cwd: None, // this should be filled by the calling function, eg. spawn_terminal }) } + fn fill_cwd(&self, terminal_action: &mut TerminalAction, client_id: ClientId) { + if let TerminalAction::RunCommand(run_command) = terminal_action { + if run_command.cwd.is_none() { + run_command.cwd = self + .active_panes + .get(&client_id) + .and_then(|pane| match pane { + PaneId::Plugin(..) => None, + PaneId::Terminal(id) => self.id_to_child_pid.get(id), + }) + .and_then(|id| { + self.bus + .os_input + .as_ref() + .map(|input| input.get_cwd(Pid::from_raw(*id))) + }) + .flatten(); + }; + }; + } pub fn spawn_terminal( &mut self, terminal_action: Option, client_or_tab_index: ClientOrTabIndex, ) -> RawFd { + log::info!( + "spawn_terminal, client_or_tab_index: {:?}", + client_or_tab_index + ); let terminal_action = match client_or_tab_index { ClientOrTabIndex::ClientId(client_id) => { - terminal_action.unwrap_or_else(|| self.get_default_terminal(Some(client_id))) + let mut terminal_action = + terminal_action.unwrap_or_else(|| self.get_default_terminal()); + self.fill_cwd(&mut terminal_action, client_id); + terminal_action } ClientOrTabIndex::TabIndex(_) => { - terminal_action.unwrap_or_else(|| self.get_default_terminal(None)) + terminal_action.unwrap_or_else(|| self.get_default_terminal()) } }; let quit_cb = Box::new({ @@ -327,8 +342,8 @@ impl Pty { default_shell: Option, client_id: ClientId, ) { - let default_shell = - default_shell.unwrap_or_else(|| self.get_default_terminal(Some(client_id))); + let mut default_shell = default_shell.unwrap_or_else(|| self.get_default_terminal()); + self.fill_cwd(&mut default_shell, client_id); let extracted_run_instructions = layout.extract_run_instructions(); let mut new_pane_pids = vec![]; for run_instruction in extracted_run_instructions {