termwiz: make tmux control code optional with a Cargo feature
The tmux control code support depends on `pest`, which might be a dependency that some projects won't need. This change makes it optional with a Cargo feature. It's default on to maintain compatibility.
This commit is contained in:
1 parent
b4261af93e
commit
33b40fe629
4 files changed
+27
-12
No files matched your search
+5
-4
@@ -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
|
||||
|
||||
@@ -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<ShortDeviceControl>),
|
||||
/// Tmux parsed events
|
||||
#[cfg(feature = "tmux_cc")]
|
||||
TmuxEvents(Box<Vec<Event>>),
|
||||
}
|
||||
|
||||
@@ -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"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SixelBuilder>,
|
||||
dcs: Option<ShortDeviceControl>,
|
||||
get_tcap: Option<GetTcapBuilder>,
|
||||
#[cfg(feature = "tmux_cc")]
|
||||
tmux_state: Option<RefCell<crate::tmux_cc::Parser>>,
|
||||
}
|
||||
|
||||
@@ -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<Vec<Event>> {
|
||||
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<F: FnMut(Action)>(&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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user