termwiz/surface/line/
cellref.rs

1use crate::cell::{Cell, CellAttributes};
2use crate::emoji::Presentation;
3use std::hash::{Hash, Hasher};
4
5#[derive(Debug, Clone, Copy)]
6pub enum CellRef<'a> {
7    CellRef {
8        cell_index: usize,
9        cell: &'a Cell,
10    },
11    ClusterRef {
12        cell_index: usize,
13        text: &'a str,
14        width: usize,
15        attrs: &'a CellAttributes,
16    },
17}
18
19impl<'a> CellRef<'a> {
20    pub fn cell_index(&self) -> usize {
21        match self {
22            Self::ClusterRef { cell_index, .. } | Self::CellRef { cell_index, .. } => *cell_index,
23        }
24    }
25
26    pub fn str(&self) -> &str {
27        match self {
28            Self::CellRef { cell, .. } => cell.str(),
29            Self::ClusterRef { text, .. } => text,
30        }
31    }
32
33    pub fn width(&self) -> usize {
34        match self {
35            Self::CellRef { cell, .. } => cell.width(),
36            Self::ClusterRef { width, .. } => *width,
37        }
38    }
39
40    pub fn attrs(&self) -> &CellAttributes {
41        match self {
42            Self::CellRef { cell, .. } => cell.attrs(),
43            Self::ClusterRef { attrs, .. } => attrs,
44        }
45    }
46
47    pub fn presentation(&self) -> Presentation {
48        match self {
49            Self::CellRef { cell, .. } => cell.presentation(),
50            Self::ClusterRef { text, .. } => match Presentation::for_grapheme(text) {
51                (_, Some(variation)) => variation,
52                (presentation, None) => presentation,
53            },
54        }
55    }
56
57    pub fn as_cell(&self) -> Cell {
58        match self {
59            Self::CellRef { cell, .. } => (*cell).clone(),
60            Self::ClusterRef {
61                text, width, attrs, ..
62            } => Cell::new_grapheme_with_width(text, *width, (*attrs).clone()),
63        }
64    }
65
66    pub fn same_contents(&self, other: &Self) -> bool {
67        self.str() == other.str() && self.width() == other.width() && self.attrs() == other.attrs()
68    }
69
70    pub fn compute_shape_hash<H: Hasher>(&self, hasher: &mut H) {
71        self.str().hash(hasher);
72        self.attrs().compute_shape_hash(hasher);
73    }
74}