Repository: alacritty/alacritty
File: alacritty/src/input/keyboard.rs (Rust)

Code before:
            bytes
        };

        // Write only if we have something to write.
        if !bytes.is_empty() {
            self.ctx.on_terminal_input_start();
            self.ctx.write_to_pty(bytes);
        }
    }

    /// Check whether we should try to build escape sequence for the [`KeyEvent`].
    fn should_build_sequence(
        key: &KeyEvent,
        text: &str,
        mode: TermMode,
        mods: ModifiersState,
    ) -> bool {
        if mode.contains(TermMode::REPORT_ALL_KEYS_AS_ESC) {
            return true;
        }

        let disambiguate = if mode.contains(TermMode::DISAMBIGUATE_ESC_CODES) {
            let on_numpad = key.location == KeyLocation::Numpad;
            let is_escape = key.logical_key == Key::Named(NamedKey::Escape);
            is_escape || (!mods.is_empty() && mods != ModifiersState::SHIFT) || on_numpad
        } else {
            false
        };

        match key.logical_key {
            _ if disambiguate => true,
            // Exclude all the named keys unless they have textual representation.
            Key::Named(named) => named.to_text().is_none(),
            _ => text.is_empty(),
        }
    }

    /// Whether we should send `ESC` due to `Alt` being pressed.
    #[cfg(not(target_os = "macos"))]
    fn alt_send_esc(&mut self) -> bool {
        self.ctx.modifiers().state().alt_key()
    }

    #[cfg(target_os = "macos")]
    fn alt_send_esc(&mut self) -> bool {
        let option_as_alt = self.ctx.config().window.option_as_alt();
        self.ctx.modifiers().state().alt_key()
            && (option_as_alt == OptionAsAlt::Both
                || (option_as_alt == OptionAsAlt::OnlyLeft
                    && self.ctx.modifiers().lalt_state() == ModifiersKeyState::Pressed)
                || (option_as_alt == OptionAsAlt::OnlyRight

Review comment (refactor):
The else branch is just `false`, so this can probably be written as a single conditional?

You could still keep the variables extracted for documentation purposes, but they're also pretty self-documenting.

Code after:
            bytes
        };

        // Write only if we have something to write.
        if !bytes.is_empty() {
            self.ctx.on_terminal_input_start();
            self.ctx.write_to_pty(bytes);
        }
    }

    /// Check whether we should try to build escape sequence for the [`KeyEvent`].
    fn should_build_sequence(
        key: &KeyEvent,
        text: &str,
        mode: TermMode,
        mods: ModifiersState,
    ) -> bool {
        if mode.contains(TermMode::REPORT_ALL_KEYS_AS_ESC) {
            return true;
        }

        let disambiguate = mode.contains(TermMode::DISAMBIGUATE_ESC_CODES)
            && (key.logical_key == Key::Named(NamedKey::Escape)
                || (!mods.is_empty() && mods != ModifiersState::SHIFT)
                || key.location == KeyLocation::Numpad);

        match key.logical_key {
            _ if disambiguate => true,
            // Exclude all the named keys unless they have textual representation.
            Key::Named(named) => named.to_text().is_none(),
            _ => text.is_empty(),
        }
    }

    /// Whether we should send `ESC` due to `Alt` being pressed.
    #[cfg(not(target_os = "macos"))]
    fn alt_send_esc(&mut self) -> bool {
        self.ctx.modifiers().state().alt_key()
    }

    #[cfg(target_os = "macos")]
    fn alt_send_esc(&mut self) -> bool {
        let option_as_alt = self.ctx.config().window.option_as_alt();
        self.ctx.modifiers().state().alt_key()
            && (option_as_alt == OptionAsAlt::Both
                || (option_as_alt == OptionAsAlt::OnlyLeft
                    && self.ctx.modifiers().lalt_state() == ModifiersKeyState::Pressed)
                || (option_as_alt == OptionAsAlt::OnlyRight
                    && self.ctx.modifiers().ralt_state() == ModifiersKeyState::Pressed))
    }