mod active;
pub(crate) use active::Modes;
use crate::color::Rgb;
use crate::view::{ProgressBar, ProgressBarState};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TerminalMode {
BracketedPaste,
ReportFocus,
MouseCellMotion,
MouseAllMotion,
KeyboardEnhancements(u8),
KeyboardFlags(u8),
AltScreen,
NoAutowrap,
UnicodeCore,
SavedTitle,
CursorStyle(u8),
ForegroundColor(Rgb),
BackgroundColor(Rgb),
CursorColor(Rgb),
ProgressBar(ProgressBar),
}
impl TerminalMode {
#[must_use]
pub fn sequence(self, on: bool) -> Vec<u8> {
if on {
self.enter_sequence()
} else {
self.disable().to_vec()
}
}
fn enter_sequence(self) -> Vec<u8> {
match self {
Self::KeyboardEnhancements(flags) => format!("\x1b[>4;2m\x1b[>{flags}u").into_bytes(),
Self::KeyboardFlags(flags) => format!("\x1b[>4;2m\x1b[={flags};1u").into_bytes(),
Self::CursorStyle(n) => format!("\x1b[{n} q").into_bytes(),
Self::ForegroundColor(c) => osc_colour(10, c),
Self::BackgroundColor(c) => osc_colour(11, c),
Self::CursorColor(c) => osc_colour(12, c),
Self::ProgressBar(bar) => bar.sequence().into_bytes(),
mode => mode.entry().1.to_vec(),
}
}
fn entry(self) -> &'static (TerminalMode, &'static [u8], &'static [u8]) {
let kind = self.kind();
SEQUENCES
.iter()
.find(|e| e.0 == kind)
.unwrap_or(&SEQUENCES[0])
}
pub(crate) fn disable(self) -> &'static [u8] {
self.entry().2
}
pub(crate) const fn kind(self) -> Self {
const BLACK: Rgb = Rgb::new(0, 0, 0);
match self {
Self::KeyboardEnhancements(_) => Self::KeyboardEnhancements(0),
Self::KeyboardFlags(_) => Self::KeyboardFlags(0),
Self::CursorStyle(_) => Self::CursorStyle(0),
Self::ForegroundColor(_) => Self::ForegroundColor(BLACK),
Self::BackgroundColor(_) => Self::BackgroundColor(BLACK),
Self::CursorColor(_) => Self::CursorColor(BLACK),
Self::ProgressBar(_) => Self::ProgressBar(ProgressBar {
state: ProgressBarState::None,
value: 0,
}),
mode => mode,
}
}
}
const SEQUENCES: [(TerminalMode, &[u8], &[u8]); 15] = {
use TerminalMode as M;
const BLACK: Rgb = Rgb::new(0, 0, 0);
const NO_BAR: ProgressBar = ProgressBar {
state: ProgressBarState::None,
value: 0,
};
const MOUSE_OFF: &[u8] = b"\x1b[?1002l\x1b[?1003l\x1b[?1006l";
[
(M::BracketedPaste, b"\x1b[?2004h", b"\x1b[?2004l"),
(M::ReportFocus, b"\x1b[?1004h", b"\x1b[?1004l"),
(M::MouseCellMotion, b"\x1b[?1002h\x1b[?1006h", MOUSE_OFF),
(M::MouseAllMotion, b"\x1b[?1003h\x1b[?1006h", MOUSE_OFF),
(M::KeyboardEnhancements(0), b"", b"\x1b[>4m\x1b[<u"),
(M::KeyboardFlags(0), b"", b""),
(M::AltScreen, b"\x1b[?1049h", b"\x1b[?1049l"),
(M::NoAutowrap, b"\x1b[?7l", b"\x1b[?7h"),
(M::UnicodeCore, b"\x1b[?2027h", b"\x1b[?2027l"),
(M::SavedTitle, b"\x1b[22;2t", b"\x1b]2;\x07\x1b[23;2t"),
(M::CursorStyle(0), b"", b"\x1b[0 q"),
(M::ForegroundColor(BLACK), b"", b"\x1b]110\x07"),
(M::BackgroundColor(BLACK), b"", b"\x1b]111\x07"),
(M::CursorColor(BLACK), b"", b"\x1b]112\x07"),
(M::ProgressBar(NO_BAR), b"", b"\x1b]9;4;0\x07"),
]
};
fn osc_colour(n: u8, c: Rgb) -> Vec<u8> {
format!("\x1b]{n};#{:02x}{:02x}{:02x}\x07", c.r, c.g, c.b).into_bytes()
}
#[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, on: bool) -> Self {
self.with(2, on)
}
#[must_use]
pub const fn report_alternate_keys(self, on: bool) -> Self {
self.with(4, on)
}
#[must_use]
pub const fn report_all_keys_as_escape_codes(self, on: bool) -> Self {
self.with(8, on)
}
#[must_use]
pub const fn report_associated_text(self, on: bool) -> Self {
self.with(16, on)
}
const fn with(self, bit: u8, on: bool) -> Self {
if on {
Self(self.0 | bit)
} else {
Self(self.0 & !bit)
}
}
#[must_use]
pub const fn flags(self) -> u8 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct KeyboardEnhancementsReport {
pub flags: i32,
}
impl KeyboardEnhancementsReport {
#[must_use]
pub const fn new(flags: i32) -> Self {
Self { flags }
}
#[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)]
#[non_exhaustive]
pub enum ReportedMode {
Dec(u32),
Ansi(u32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
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,
}
}
}
#[cfg(test)]
mod tests;