diff --git a/term/src/test/csi.rs b/term/src/test/csi.rs index e682f6605..ea895dc52 100644 --- a/term/src/test/csi.rs +++ b/term/src/test/csi.rs @@ -199,6 +199,7 @@ fn test_789() { zones: [], seqno: 5, bits: NONE, + appdata: None, }, ] "# diff --git a/termwiz/src/surface/line/line.rs b/termwiz/src/surface/line/line.rs index 9959a4cda..e2e41fb3a 100644 --- a/termwiz/src/surface/line/line.rs +++ b/termwiz/src/surface/line/line.rs @@ -9,8 +9,10 @@ use crate::surface::line::vecstorage::{VecStorage, VecStorageIter}; use crate::surface::{Change, SequenceNo, SEQ_ZERO}; #[cfg(feature = "use_serde")] use serde::{Deserialize, Serialize}; +use std::any::Any; use std::borrow::Cow; use std::ops::Range; +use std::sync::{Arc, Weak}; use unicode_segmentation::UnicodeSegmentation; use wezterm_bidi::{Direction, ParagraphDirectionHint}; @@ -28,12 +30,20 @@ pub enum DoubleClickRange { } #[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub struct Line { pub(crate) cells: CellStorage, zones: Vec, seqno: SequenceNo, bits: LineBits, + #[cfg_attr(feature = "use_serde", serde(skip))] + appdata: Option>, +} + +impl PartialEq for Line { + fn eq(&self, other: &Self) -> bool { + self.seqno == other.seqno && self.bits == other.bits && self.cells == other.cells + } } impl Line { @@ -46,6 +56,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], + appdata: None, } } @@ -56,6 +67,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], + appdata: None, } } @@ -69,6 +81,7 @@ impl Line { cells: CellStorage::C(ClusteredLine::new()), seqno, zones: vec![], + appdata: None, } } @@ -81,6 +94,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], + appdata: None, } } @@ -106,6 +120,7 @@ impl Line { bits: LineBits::NONE, seqno, zones: vec![], + appdata: None, } } @@ -183,6 +198,23 @@ impl Line { } } + /// Set arbitrary application specific data for the line. + /// Only one piece of appdata can be tracked per line, + /// so this is only suitable for the overall application + /// and not for use by "middleware" crates. + /// A Weak reference is stored. + /// `get_appdata` is used to retrieve a previously stored reference. + pub fn set_appdata(&mut self, appdata: Arc) { + self.appdata.replace(Arc::downgrade(&appdata)); + } + + /// Retrieve the appdata for the line, if any. + /// This may return None in the case where the underlying data has + /// been released: Line only stores a Weak reference to it. + pub fn get_appdata(&self) -> Option> { + self.appdata.as_ref().and_then(|data| data.upgrade()) + } + /// Returns true if the line's last changed seqno is more recent /// than the provided seqno parameter pub fn changed_since(&self, seqno: SequenceNo) -> bool { @@ -509,6 +541,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno, zones: vec![], + appdata: None, } } @@ -592,6 +625,7 @@ impl Line { cells: CellStorage::V(VecStorage::new(cells)), seqno: self.current_seqno(), zones: vec![], + appdata: None, } } diff --git a/termwiz/src/surface/line/test.rs b/termwiz/src/surface/line/test.rs index a3b05c29a..e066ba86a 100644 --- a/termwiz/src/surface/line/test.rs +++ b/termwiz/src/surface/line/test.rs @@ -274,6 +274,7 @@ Line { zones: [], seqno: 1, bits: NONE, + appdata: None, } "# ); @@ -579,6 +580,7 @@ Line { zones: [], seqno: 5, bits: NONE, + appdata: None, } "# );