Repository: lapce/lapce
File: lapce-data/src/terminal.rs (Rust)

Code before:
                        let mut raw = self.terminal.raw.lock();
                        let term = &mut raw.term;
                        self.terminal.search_next(
                            term,
                            search_string,
                            Direction::Right,
                        );
                    }
                }
                FocusCommand::SearchBackward => {
                    if let Some(search_string) = self.find.search_string.as_ref() {
                        let mut raw = self.terminal.raw.lock();
                        let term = &mut raw.term;
                        self.terminal.search_next(
                            term,
                            search_string,
                            Direction::Left,
                        );
                    }
                }
                FocusCommand::TerminalCopySelection => {
                    if self.terminal.mode == Mode::Terminal {
                        let mut raw = self.terminal.raw.lock();
                        let term = &mut raw.term;
                        if let Some(content) = term.selection_to_string() {
                            Application::global().clipboard().put_string(content);
                        }
                    }
                }
                FocusCommand::TerminalPasteSelection => {
                    if self.terminal.mode == Mode::Terminal {
                        if let Some(s) = 
                            Application::global().clipboard().get_string() {
                            self.receive_char(ctx, &s);
                        }
                    }
                }
                _ => return CommandExecuted::No,
            },
            _ => return CommandExecuted::No,
        };
        CommandExecuted::Yes
    }

    fn receive_char(&mut self, _ctx: &mut EventCtx, c: &str) {
        if self.terminal.mode == Mode::Terminal {
            self.terminal
                .proxy
                .proxy_rpc
                .terminal_write(self.terminal.term_id, c);
            self.terminal.raw.lock().term.scroll_display(Scroll::Bottom);

Review comment (refactor):
There should be a `SystemClipboard` struct somewhere, that could be used to deduplicate these lines.

Code after:
                }
                FocusCommand::SplitExchange => {
                    ctx.submit_command(Command::new(
                        LAPCE_UI_COMMAND,
                        LapceUICommand::SplitEditorExchange(self.terminal.widget_id),
                        Target::Widget(self.terminal.split_id),
                    ));
                }
                FocusCommand::SearchForward => {
                    if let Some(search_string) = self.find.search_string.as_ref() {
                        let mut raw = self.terminal.raw.lock();
                        let term = &mut raw.term;
                        self.terminal.search_next(
                            term,
                            search_string,
                            Direction::Right,
                        );
                    }
                }
                FocusCommand::SearchBackward => {
                    if let Some(search_string) = self.find.search_string.as_ref() {
                        let mut raw = self.terminal.raw.lock();
                        let term = &mut raw.term;
                        self.terminal.search_next(
                            term,
                            search_string,
                            Direction::Left,
                        );
                    }
                }
                _ => return CommandExecuted::No,
            },
            _ => return CommandExecuted::No,
        };
        CommandExecuted::Yes
    }

    fn receive_char(&mut self, _ctx: &mut EventCtx, c: &str) {
        if self.terminal.mode == Mode::Terminal {
            self.terminal
                .proxy
                .proxy_rpc
                .terminal_write(self.terminal.term_id, c);
            self.terminal.raw.lock().term.scroll_display(Scroll::Bottom);
        }
    }
}

pub struct RawTerminal {
    pub parser: ansi::Processor,
    pub term: Term<EventProxy>,