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

Code before:
    pub fn write_to_pane_id(
        &mut self,
        input_bytes: Vec<u8>,
        pane_id: PaneId,
        is_synced: bool,
    ) -> Result<bool> {
        // returns true if we need to update the UI (eg. when a command pane is closed with ctrl-c)
        let err_context = || format!("failed to write to pane with id {pane_id:?}");

        let mut should_update_ui = false;
        match pane_id {
            PaneId::Terminal(active_terminal_id) => {
                let active_terminal = self
                    .floating_panes
                    .get_mut(&pane_id)
                    .or_else(|| self.tiled_panes.get_pane_mut(pane_id))
                    .or_else(|| self.suppressed_panes.get_mut(&pane_id))
                    .ok_or_else(|| anyhow!(format!("failed to find pane with id {pane_id:?}")))
                    .with_context(err_context)?;
                match active_terminal.adjust_input_to_terminal(input_bytes) {
                    Some(AdjustedInput::WriteBytesToTerminal(adjusted_input)) => {
                        // We always write for non-synced terminals.
                        // However if the terminal is part of a tab-sync, we need to
                        // check if the terminal should receive input or not (depending on its
                        // 'exclude_from_sync' configuration).
                        if !is_synced || !active_terminal.exclude_from_sync() {
                            self.senders
                                .send_to_pty_writer(PtyWriteInstruction::Write(
                                    adjusted_input,
                                    active_terminal_id,
                                ))
                                .with_context(err_context)?;
                        }
                    },
                    Some(AdjustedInput::ReRunCommandInThisPane(command)) => {
                        self.pids_waiting_resize.insert(active_terminal_id);
                        self.senders
                            .send_to_pty(PtyInstruction::ReRunCommandInPane(
                                PaneId::Terminal(active_terminal_id),
                                command,
                            ))
                            .with_context(err_context)?;
                    },
                    Some(AdjustedInput::CloseThisPane) => {
                        self.close_pane(PaneId::Terminal(active_terminal_id), false, None);
                        should_update_ui = true;
                    },
                    None => {},
                }
            },
            PaneId::Plugin(pid) => {

Review comment (suggestion):
What do you think about making this something like:
```rust
let should_write_to_terminal = /* ... */
if should_write_to_terminal {
    // ...
}
```

Code after:
        if let Some(terminal_output) = self
            .tiled_panes
            .get_pane_mut(PaneId::Terminal(pid))
            .or_else(|| self.floating_panes.get_pane_mut(PaneId::Terminal(pid)))
            .or_else(|| {
                self.suppressed_panes
                    .values_mut()
                    .find(|s_p| s_p.pid() == PaneId::Terminal(pid))
            })
        {
            if self.pids_waiting_resize.remove(&pid) {
                resize_pty!(
                    terminal_output,
                    self.os_api,
                    self.senders,
                    self.character_cell_size
                )
                .with_context(err_context)?;
            }
            terminal_output.handle_pty_bytes(bytes);
            let messages_to_pty = terminal_output.drain_messages_to_pty();
            let clipboard_update = terminal_output.drain_clipboard_update();
            for message in messages_to_pty {
                self.write_to_pane_id(message, PaneId::Terminal(pid), None)
                    .with_context(err_context)?;
            }
            if let Some(string) = clipboard_update {
                self.write_selection_to_clipboard(&string)
                    .with_context(err_context)?;
            }
        }
        Ok(())
    }

    pub fn write_to_terminals_on_current_tab(
        &mut self,
        input_bytes: Vec<u8>,
        client_id: ClientId,
    ) -> Result<bool> {
        // returns true if a UI update should be triggered (eg. when closing a command pane with
        // ctrl-c)
        let mut should_trigger_ui_change = false;
        let pane_ids = self.get_static_and_floating_pane_ids();
        for pane_id in pane_ids {
            let ui_change_triggered = self
                .write_to_pane_id(input_bytes.clone(), pane_id, Some(client_id))
                .context("failed to write to terminals on current tab")?;
            if ui_change_triggered {
                should_trigger_ui_change = true;
            }
        }