use crate::backend::Backend;
use crate::mouse::MouseMode;
use crate::msg::View;
use std::io;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TerminalMode {
BracketedPaste,
ReportFocus,
MouseCellMotion,
MouseAllMotion,
KeyboardEnhancements(u8),
KeyboardFlags(u8),
}
impl TerminalMode {
#[must_use]
pub fn sequence(self, on: bool) -> Vec<u8> {
match (self, on) {
(Self::KeyboardEnhancements(flags), true) => {
format!("\x1b[>4;2m\x1b[>{flags}u").into_bytes()
}
(Self::KeyboardFlags(flags), true) => {
format!("\x1b[>4;2m\x1b[={flags};1u").into_bytes()
}
(mode, true) => mode.enable().to_vec(),
(mode, false) => mode.disable().to_vec(),
}
}
const fn enable(self) -> &'static [u8] {
match self {
Self::BracketedPaste => b"\x1b[?2004h",
Self::ReportFocus => b"\x1b[?1004h",
Self::MouseCellMotion => b"\x1b[?1002h\x1b[?1006h",
Self::MouseAllMotion => b"\x1b[?1003h\x1b[?1006h",
Self::KeyboardEnhancements(_) | Self::KeyboardFlags(_) => b"",
}
}
pub(crate) const fn disable(self) -> &'static [u8] {
match self {
Self::BracketedPaste => b"\x1b[?2004l",
Self::ReportFocus => b"\x1b[?1004l",
Self::MouseCellMotion | Self::MouseAllMotion => b"\x1b[?1002l\x1b[?1003l\x1b[?1006l",
Self::KeyboardEnhancements(_) => b"\x1b[>4m\x1b[<u",
Self::KeyboardFlags(_) => b"",
}
}
pub(crate) const fn kind(self) -> Self {
match self {
Self::KeyboardEnhancements(_) => Self::KeyboardEnhancements(0),
Self::KeyboardFlags(_) => Self::KeyboardFlags(0),
mode => mode,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyboardEnhancements(u8);
impl Default for KeyboardEnhancements {
fn default() -> Self {
Self::new()
}
}
impl KeyboardEnhancements {
#[must_use]
pub const fn new() -> Self {
Self(1)
}
#[must_use]
pub const fn report_event_types(self) -> Self {
Self(self.0 | 2)
}
#[must_use]
pub const fn report_alternate_keys(self) -> Self {
Self(self.0 | 4)
}
#[must_use]
pub const fn report_all_keys_as_escape_codes(self) -> Self {
Self(self.0 | 8)
}
#[must_use]
pub const fn report_associated_text(self) -> Self {
Self(self.0 | 16)
}
#[must_use]
pub const fn flags(self) -> u8 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct KeyboardEnhancementsReport {
pub flags: i32,
}
impl KeyboardEnhancementsReport {
#[must_use]
pub const fn supports_key_disambiguation(self) -> bool {
self.flags > 0
}
#[must_use]
pub const fn supports_event_types(self) -> bool {
self.flags & 2 != 0
}
#[must_use]
pub const fn supports_alternate_keys(self) -> bool {
self.flags & 4 != 0
}
#[must_use]
pub const fn supports_all_keys_as_escape_codes(self) -> bool {
self.flags & 8 != 0
}
#[must_use]
pub const fn supports_associated_text(self) -> bool {
self.flags & 16 != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ReportedMode {
Dec(u32),
Ansi(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModeSetting {
NotRecognized,
Set,
Reset,
PermanentlySet,
PermanentlyReset,
Other(u8),
}
impl From<u8> for ModeSetting {
fn from(value: u8) -> Self {
match value {
0 => Self::NotRecognized,
1 => Self::Set,
2 => Self::Reset,
3 => Self::PermanentlySet,
4 => Self::PermanentlyReset,
v => Self::Other(v),
}
}
}
impl From<ModeSetting> for u8 {
fn from(setting: ModeSetting) -> Self {
match setting {
ModeSetting::NotRecognized => 0,
ModeSetting::Set => 1,
ModeSetting::Reset => 2,
ModeSetting::PermanentlySet => 3,
ModeSetting::PermanentlyReset => 4,
ModeSetting::Other(v) => v,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Wanted {
paste: bool,
focus: bool,
mouse: MouseMode,
keyboard: u8,
}
impl Wanted {
const OFF: Self = Self {
paste: false,
focus: false,
mouse: MouseMode::None,
keyboard: 0,
};
fn of(view: &View) -> Self {
Self {
paste: !view.disable_bracketed_paste_mode,
focus: view.report_focus,
mouse: view.mouse_mode,
keyboard: view.keyboard_enhancements.flags(),
}
}
}
const fn mouse_mode(mode: MouseMode) -> Option<TerminalMode> {
match mode {
MouseMode::None => None,
MouseMode::CellMotion => Some(TerminalMode::MouseCellMotion),
MouseMode::AllMotion => Some(TerminalMode::MouseAllMotion),
}
}
#[derive(Debug, Default)]
pub(crate) struct Modes(Option<Wanted>);
impl Modes {
pub(crate) fn apply(&mut self, view: &View, backend: &mut impl Backend) -> io::Result<()> {
let want = Wanted::of(view);
self.change(want, backend)?;
self.0 = Some(want);
Ok(())
}
pub(crate) fn clear(&mut self, backend: &mut impl Backend) -> io::Result<()> {
if self.0.is_some() {
self.change(Wanted::OFF, backend)?;
}
self.0 = None;
Ok(())
}
fn change(&self, want: Wanted, backend: &mut impl Backend) -> io::Result<()> {
let was = self.0.unwrap_or(Wanted::OFF);
let toggle = |mode, on: bool, was: bool| (on != was).then_some((mode, on));
let mut changes: Vec<(TerminalMode, bool)> = Vec::new();
changes.extend(toggle(TerminalMode::BracketedPaste, want.paste, was.paste));
changes.extend(toggle(TerminalMode::ReportFocus, want.focus, was.focus));
if want.mouse != was.mouse {
changes.extend(mouse_mode(was.mouse).map(|m| (m, false)));
changes.extend(mouse_mode(want.mouse).map(|m| (m, true)));
}
changes.extend(keyboard_change(was.keyboard, want.keyboard));
if want == Wanted::OFF {
changes.reverse();
}
changes
.into_iter()
.try_for_each(|(mode, on)| backend.set_mode(mode, on))
}
}
fn keyboard_change(was: u8, want: u8) -> Option<(TerminalMode, bool)> {
match (was, want) {
(w, n) if w == n => None,
(0, n) => Some((TerminalMode::KeyboardEnhancements(n), true)),
(w, 0) => Some((TerminalMode::KeyboardEnhancements(w), false)),
(_, n) => Some((TerminalMode::KeyboardFlags(n), true)),
}
}
#[cfg(test)]
mod tests;