diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index 743cf4e23..1230bf0ac 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -29,8 +29,8 @@ memmem.workspace = true num-derive.workspace = true num-traits.workspace = true ordered-float.workspace = true -pest.workspace = true -pest_derive.workspace = true +pest = {workspace=true, optional=true} +pest_derive = {workspace=true, optional=true} phf.workspace = true serde = {workspace=true, features = ["rc", "derive"], optional=true} sha2 = {workspace=true, optional=true} @@ -47,12 +47,13 @@ wezterm-dynamic.workspace = true wezterm-input-types.workspace = true [features] -default = ["image"] +default = ["image", "tmux_cc"] widgets = ["cassowary", "fnv"] use_serde = ["serde", "wezterm-color-types/use_serde", "wezterm-blob-leases/serde", "bitflags/serde", "wezterm-input-types/serde"] use_image = ["image", "dep:image"] image = ["sha2", "wezterm-blob-leases"] -docs = ["widgets", "use_serde", "image"] +tmux_cc = ["pest", "pest_derive"] +docs = ["widgets", "use_serde", "image", "tmux_cc"] [dev-dependencies] criterion.workspace = true diff --git a/termwiz/src/escape/mod.rs b/termwiz/src/escape/mod.rs index 474237879..b779e514b 100644 --- a/termwiz/src/escape/mod.rs +++ b/termwiz/src/escape/mod.rs @@ -6,6 +6,7 @@ //! semantic meaning to them. It can also encode the semantic values as //! escape sequences. It provides encoding and decoding functionality //! only; it does not provide terminal emulation facilities itself. +#[cfg(feature = "tmux_cc")] use crate::tmux_cc::Event; use num_derive::*; use std::fmt::{Display, Error as FmtError, Formatter, Write as FmtWrite}; @@ -219,6 +220,7 @@ pub enum DeviceControlMode { /// A self contained (Enter, Data*, Exit) sequence ShortDeviceControl(Box), /// Tmux parsed events + #[cfg(feature = "tmux_cc")] TmuxEvents(Box>), } @@ -243,6 +245,7 @@ impl Display for DeviceControlMode { Self::Exit => Ok(()), Self::Data(c) => f.write_char(*c as char), Self::ShortDeviceControl(s) => s.fmt(f), + #[cfg(feature = "tmux_cc")] Self::TmuxEvents(_) => write!(f, "tmux event"), } } @@ -255,6 +258,7 @@ impl std::fmt::Debug for DeviceControlMode { Self::Exit => write!(fmt, "Exit"), Self::Data(b) => write!(fmt, "Data({:?} 0x{:x})", *b as char, *b), Self::ShortDeviceControl(s) => write!(fmt, "ShortDeviceControl({:?})", s), + #[cfg(feature = "tmux_cc")] Self::TmuxEvents(_) => write!(fmt, "tmux event"), } } diff --git a/termwiz/src/escape/parser/mod.rs b/termwiz/src/escape/parser/mod.rs index 22047668d..a26dd18db 100644 --- a/termwiz/src/escape/parser/mod.rs +++ b/termwiz/src/escape/parser/mod.rs @@ -3,9 +3,11 @@ use crate::escape::{ Action, DeviceControlMode, EnterDeviceControlMode, Esc, OperatingSystemCommand, ShortDeviceControl, CSI, }; +#[cfg(feature = "tmux_cc")] use crate::tmux_cc::Event; use log::error; use num_traits::FromPrimitive; +#[cfg(feature = "tmux_cc")] use std::borrow::BorrowMut; use std::cell::RefCell; use vtparse::{CsiParam, VTActor, VTParser}; @@ -47,6 +49,7 @@ struct ParseState { sixel: Option, dcs: Option, get_tcap: Option, + #[cfg(feature = "tmux_cc")] tmux_state: Option>, } @@ -76,6 +79,7 @@ impl Parser { } /// advance with tmux parser, bypass VTParse + #[cfg(feature = "tmux_cc")] fn advance_tmux_bytes(&mut self, bytes: &[u8]) -> anyhow::Result> { let parser_state = self.state.borrow(); let tmux_state = parser_state.tmux_state.as_ref().unwrap(); @@ -84,7 +88,9 @@ impl Parser { } pub fn parse(&mut self, bytes: &[u8], mut callback: F) { + #[cfg(feature = "tmux_cc")] let is_tmux_mode: bool = self.state.borrow().tmux_state.is_some(); + #[cfg(feature = "tmux_cc")] if is_tmux_mode { match self.advance_tmux_bytes(bytes) { Ok(tmux_events) => { @@ -105,13 +111,14 @@ impl Parser { .parse(unparsed_str.as_bytes(), &mut perform); } } - } else { - let mut perform = Performer { - callback: &mut callback, - state: &mut self.state.borrow_mut(), - }; - self.state_machine.parse(bytes, &mut perform); + return; } + + let mut perform = Performer { + callback: &mut callback, + state: &mut self.state.borrow_mut(), + }; + self.state_machine.parse(bytes, &mut perform); } /// A specialized version of the parser that halts after recognizing the @@ -243,6 +250,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> { data: vec![], }); } else { + #[cfg(feature = "tmux_cc")] if byte == b'p' && params == [1000] { // into tmux_cc mode self.state.borrow_mut().tmux_state = @@ -267,6 +275,7 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> { } else if let Some(tcap) = self.state.get_tcap.as_mut() { tcap.push(data); } else { + #[cfg(feature = "tmux_cc")] if let Some(tmux_state) = &self.state.tmux_state { let mut tmux_parser = tmux_state.borrow_mut(); match tmux_parser.advance_byte(data) { @@ -282,9 +291,9 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> { self.state.tmux_state = None; // drop tmux state } } - } else { - (self.callback)(Action::DeviceControl(DeviceControlMode::Data(data))); + return; } + (self.callback)(Action::DeviceControl(DeviceControlMode::Data(data))); } } diff --git a/termwiz/src/lib.rs b/termwiz/src/lib.rs index add1ce898..24c53ee60 100644 --- a/termwiz/src/lib.rs +++ b/termwiz/src/lib.rs @@ -66,6 +66,7 @@ mod readbuf; pub mod render; pub mod surface; pub mod terminal; +#[cfg(feature = "tmux_cc")] pub mod tmux_cc; #[cfg(feature = "widgets")] pub mod widgets;