Repository: zellij-org/zellij
File: zellij-server/src/pty.rs (Rust)

Code before:
        run_command: RunCommand,
    ) -> Result<(), SpawnTerminalError> {
        let err_context = || format!("failed to rerun command for pane {pane_id:?}");
        match pane_id {
            PaneId::Terminal(id) => {
                let _ = self.task_handles.remove(&id); // if all is well, this shouldn't be here
                let _ = self.id_to_child_pid.remove(&id); // if all is wlel, this shouldn't be here

                let quit_cb = Box::new({
                    let senders = self.bus.senders.clone();
                    move |pane_id, exit_status, command| {
                        // we only re-run held panes, so we'll never close them from Pty
                        let _ = senders.send_to_screen(ScreenInstruction::HoldPane(
                            pane_id,
                            exit_status,
                            command,
                            None,
                        ));
                    }
                });
                let (pid_primary, child_fd): (RawFd, RawFd) =
                    self.bus
                        .os_input
                        .as_mut()
                        .with_context(err_context)
                        .map_err(|_e|SpawnTerminalError::GenericSpawnError("os input is none"))?
                        .re_run_command_in_terminal(id, run_command, quit_cb)?;
                let terminal_bytes = task::spawn({
                    let err_context = || format!("failed to run async task for pane {pane_id:?}");
                    let senders = self.bus.senders.clone();
                    let os_input = self.bus.os_input.as_ref().with_context(err_context).fatal().clone();
                    let debug_to_file = self.debug_to_file;
                    async move {
                        TerminalBytes::new(pid_primary, senders, os_input, debug_to_file, id)
                            .listen()
                            .await;
                    }
                });

                self.task_handles.insert(id, terminal_bytes);
                self.id_to_child_pid.insert(id, child_fd);
                Ok(())
            },
            _ => Err(SpawnTerminalError::GenericSpawnError(
                "Cannot respawn plugin panes",
            )),
        }
    }
}

impl Drop for Pty {

Review comment (suggestion):
These two lines should be swapped, i.e. first we convert the error type (with `map_err`), and then we attach the context to that instead.

Code after:
        Ok(())
    }
    pub fn close_tab(&mut self, ids: Vec<PaneId>) -> Result<()> {
        for id in ids {
            self.close_pane(id)
                .with_context(|| format!("failed to close tab for pane {id:?}"))?;
        }
        Ok(())
    }
    pub fn set_active_pane(&mut self, pane_id: Option<PaneId>, client_id: ClientId) {
        if let Some(pane_id) = pane_id {
            self.active_panes.insert(client_id, pane_id);
        }
    }
    pub fn rerun_command_in_pane(
        &mut self,
        pane_id: PaneId,
        run_command: RunCommand,
    ) -> Result<(), SpawnTerminalError> {
        match pane_id {
            PaneId::Terminal(id) => {
                let _ = self.task_handles.remove(&id); // if all is well, this shouldn't be here
                let _ = self.id_to_child_pid.remove(&id); // if all is wlel, this shouldn't be here

                let quit_cb = Box::new({
                    let senders = self.bus.senders.clone();
                    move |pane_id, exit_status, command| {
                        // we only re-run held panes, so we'll never close them from Pty
                        let _ = senders.send_to_screen(ScreenInstruction::HoldPane(
                            pane_id,
                            exit_status,
                            command,
                            None,
                        ));
                    }
                });
                let (pid_primary, child_fd): (RawFd, RawFd) = self
                    .bus
                    .os_input
                    .as_mut()
                    .ok_or_else(|| SpawnTerminalError::GenericSpawnError("os input is none"))?
                    .re_run_command_in_terminal(id, run_command, quit_cb)?;
                let terminal_bytes = task::spawn({
                    let err_context = || format!("failed to run async task for pane {pane_id:?}");
                    let senders = self.bus.senders.clone();
                    let os_input = self
                        .bus
                        .os_input
                        .as_ref()
                        .with_context(err_context)
                        .fatal()