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

Code before:
                            .unwrap();
                        let pid = pid_rx.recv().unwrap();
                        let new_plugin = PluginPane::new(
                            pid,
                            *position_and_size,
                            self.senders.to_plugin.as_ref().unwrap().clone(),
                        );
                        self.panes.insert(PaneId::Plugin(pid), Box::new(new_plugin));
                        // Send an initial mode update to the newly loaded plugin only!
                        self.senders
                            .send_to_plugin(PluginInstruction::Update(
                                Some(pid),
                                Event::ModeUpdate(self.mode_info.clone()),
                            ))
                            .unwrap();
                    }
                    // A command pane
                    Run::Command(command) => {
                        let cmd = TerminalAction::RunCommand(command.clone());
                        self.senders
                            .send_to_pty(PtyInstruction::SpawnTerminal(Some(cmd)))
                            .unwrap();

                        // I would expect something like this to work,
                        // but I must still be overlooking something.
                        //let (_, pid) = self.os_api.spawn_terminal(Some(cmd));
                        //let new_terminal = TerminalPane::new(pid.into(), *position_and_size, self.colors);
                        //self.os_api.set_terminal_size_using_fd(
                        //new_terminal.pid,
                        //new_terminal.columns() as u16,
                        //new_terminal.rows() as u16,
                        //);
                        //self.panes
                        //.insert(PaneId::Terminal(pid.into()), Box::new(new_terminal));
                    }
                    Run::Plugin(None) => {}
                }
            } else {
                // there are still panes left to fill, use the pids we received in this method
                let pid = new_pids.next().unwrap(); // if this crashes it means we got less pids than there are panes in this layout
                let new_terminal = TerminalPane::new(*pid, *position_and_size, self.colors);
                self.os_api.set_terminal_size_using_fd(
                    new_terminal.pid,
                    new_terminal.columns() as u16,
                    new_terminal.rows() as u16,
                );
                self.panes
                    .insert(PaneId::Terminal(*pid), Box::new(new_terminal));
            }
        }
        for unused_pid in new_pids {

Review comment (suggestion):
This was my most recent attempt.
As far as I understand we want to:
- create the terminal -> get the pid
- use the pid to create a new terminal struct and pass the correct size
- then insert the terminal into the tab struct

Code after:
                    pid,
                    *position_and_size,
                    self.senders.to_plugin.as_ref().unwrap().clone(),
                );
                self.panes.insert(PaneId::Plugin(pid), Box::new(new_plugin));
                // Send an initial mode update to the newly loaded plugin only!
                self.senders
                    .send_to_plugin(PluginInstruction::Update(
                        Some(pid),
                        Event::ModeUpdate(self.mode_info.clone()),
                    ))
                    .unwrap();
            } else {
                // there are still panes left to fill, use the pids we received in this method
                let pid = new_pids.next().unwrap(); // if this crashes it means we got less pids than there are panes in this layout
                let new_terminal = TerminalPane::new(*pid, *position_and_size, self.colors);
                self.os_api.set_terminal_size_using_fd(
                    new_terminal.pid,
                    new_terminal.columns() as u16,
                    new_terminal.rows() as u16,
                );
                self.panes
                    .insert(PaneId::Terminal(*pid), Box::new(new_terminal));
            }
        }
        for unused_pid in new_pids {
            // this is a bit of a hack and happens because we don't have any central location that
            // can query the screen as to how many panes it needs to create a layout
            // fixing this will require a bit of an architecture change
            self.senders
                .send_to_pty(PtyInstruction::ClosePane(PaneId::Terminal(*unused_pid)))
                .unwrap();
        }
        self.active_terminal = self.panes.iter().map(|(id, _)| id.to_owned()).next();
        self.render();
    }
    pub fn new_pane(&mut self, pid: PaneId) {
        self.close_down_to_max_terminals();
        if self.fullscreen_is_active {
            self.toggle_active_pane_fullscreen();
        }
        if !self.has_panes() {
            if let PaneId::Terminal(term_pid) = pid {
                let new_terminal = TerminalPane::new(term_pid, self.full_screen_ws, self.colors);
                self.os_api.set_terminal_size_using_fd(
                    new_terminal.pid,
                    new_terminal.columns() as u16,
                    new_terminal.rows() as u16,
                );
                self.panes.insert(pid, Box::new(new_terminal));
                self.active_terminal = Some(pid);