termwiz: refactor Line::visible_cells()
Don't promise that we iterate Cell directly, but things that smell like Cell. That makes it easier to adjust internal storage in a later commit.
This commit is contained in:
1 parent
355f3d3975
commit
e26d634da1
5 files changed
+94
-45
No files matched your search
@@ -584,10 +584,10 @@ impl Pane for LocalPane {
|
||||
let mut wrapped = false;
|
||||
let mut trailing_spaces = None;
|
||||
|
||||
for (grapheme_idx, cell) in line.visible_cells() {
|
||||
for cell in line.visible_cells() {
|
||||
coords.push(Coord {
|
||||
byte_idx: haystack.len(),
|
||||
grapheme_idx,
|
||||
grapheme_idx: cell.cell_index(),
|
||||
stable_row,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::cell::{Cell, CellAttributes};
|
||||
use crate::cell::CellAttributes;
|
||||
use crate::emoji::Presentation;
|
||||
use crate::surface::line::CellIter;
|
||||
use std::borrow::Cow;
|
||||
use wezterm_bidi::{BidiContext, Direction, ParagraphDirectionHint};
|
||||
|
||||
@@ -43,7 +44,7 @@ impl CellCluster {
|
||||
/// The input is typically the result of calling `Line::visible_cells()`.
|
||||
pub fn make_cluster<'a>(
|
||||
hint: usize,
|
||||
iter: impl Iterator<Item = (usize, &'a Cell)>,
|
||||
iter: impl Iterator<Item = CellIter<'a>>,
|
||||
bidi_hint: Option<ParagraphDirectionHint>,
|
||||
) -> Vec<CellCluster> {
|
||||
let mut last_cluster = None;
|
||||
@@ -51,7 +52,8 @@ impl CellCluster {
|
||||
let mut whitespace_run = 0;
|
||||
let mut only_whitespace = false;
|
||||
|
||||
for (cell_idx, c) in iter {
|
||||
for c in iter {
|
||||
let cell_idx = c.cell_index();
|
||||
let presentation = c.presentation();
|
||||
let cell_str = c.str();
|
||||
let normalized_attr = if c.attrs().wrapped() {
|
||||
|
||||
+74
-24
@@ -1,5 +1,6 @@
|
||||
use crate::cell::{Cell, CellAttributes, SemanticType, UnicodeVersion};
|
||||
use crate::cellcluster::CellCluster;
|
||||
use crate::emoji::Presentation;
|
||||
use crate::hyperlink::Rule;
|
||||
use crate::surface::{Change, SequenceNo, SEQ_ZERO};
|
||||
use bitflags::bitflags;
|
||||
@@ -375,7 +376,7 @@ impl Line {
|
||||
|
||||
fn compute_zones(&mut self) {
|
||||
let blank_cell = Cell::blank();
|
||||
let mut last_cell: Option<&Cell> = None;
|
||||
let mut last_cell: Option<CellIter> = None;
|
||||
let mut current_zone: Option<ZoneRange> = None;
|
||||
let mut zones = vec![];
|
||||
|
||||
@@ -389,15 +390,15 @@ impl Line {
|
||||
.rposition(|cell| *cell != blank_cell)
|
||||
.unwrap_or(self.cells().len());
|
||||
|
||||
for (grapheme_idx, cell) in self.visible_cells() {
|
||||
if grapheme_idx > last_non_blank {
|
||||
for cell in self.visible_cells() {
|
||||
if cell.cell_index() > last_non_blank {
|
||||
break;
|
||||
}
|
||||
let grapheme_idx = grapheme_idx as u16;
|
||||
let grapheme_idx = cell.cell_index() as u16;
|
||||
let semantic_type = cell.attrs().semantic_type();
|
||||
let new_zone = match last_cell {
|
||||
None => true,
|
||||
Some(c) => c.attrs().semantic_type() != semantic_type,
|
||||
Some(ref c) => c.attrs().semantic_type() != semantic_type,
|
||||
};
|
||||
|
||||
if new_zone {
|
||||
@@ -524,7 +525,7 @@ impl Line {
|
||||
/// Recompose line into the corresponding utf8 string.
|
||||
pub fn as_str(&self) -> String {
|
||||
let mut s = String::new();
|
||||
for (_, cell) in self.visible_cells() {
|
||||
for cell in self.visible_cells() {
|
||||
s.push_str(cell.str());
|
||||
}
|
||||
s
|
||||
@@ -582,11 +583,11 @@ impl Line {
|
||||
/// Returns a substring from the line.
|
||||
pub fn columns_as_str(&self, range: Range<usize>) -> String {
|
||||
let mut s = String::new();
|
||||
for (n, c) in self.visible_cells() {
|
||||
if n < range.start {
|
||||
for c in self.visible_cells() {
|
||||
if c.cell_index() < range.start {
|
||||
continue;
|
||||
}
|
||||
if n >= range.end {
|
||||
if c.cell_index() >= range.end {
|
||||
break;
|
||||
}
|
||||
s.push_str(c.str());
|
||||
@@ -596,14 +597,14 @@ impl Line {
|
||||
|
||||
pub fn columns_as_line(&self, range: Range<usize>) -> Self {
|
||||
let mut cells = vec![];
|
||||
for (n, c) in self.visible_cells() {
|
||||
if n < range.start {
|
||||
for c in self.visible_cells() {
|
||||
if c.cell_index() < range.start {
|
||||
continue;
|
||||
}
|
||||
if n >= range.end {
|
||||
if c.cell_index() >= range.end {
|
||||
break;
|
||||
}
|
||||
cells.push(c.clone());
|
||||
cells.push(c.as_cell());
|
||||
}
|
||||
Self {
|
||||
bits: LineBits::NONE,
|
||||
@@ -811,17 +812,20 @@ impl Line {
|
||||
/// the characters that follow wide characters, the column index may
|
||||
/// skip some positions. It is returned as a convenience to the consumer
|
||||
/// as using .enumerate() on this iterator wouldn't be as useful.
|
||||
pub fn visible_cells(&self) -> impl Iterator<Item = (usize, &Cell)> {
|
||||
pub fn visible_cells(&self) -> impl Iterator<Item = CellIter> {
|
||||
let mut skip_width = 0;
|
||||
self.cells.iter().enumerate().filter(move |(_idx, cell)| {
|
||||
if skip_width > 0 {
|
||||
skip_width -= 1;
|
||||
false
|
||||
} else {
|
||||
skip_width = cell.width().saturating_sub(1);
|
||||
true
|
||||
}
|
||||
})
|
||||
self.cells
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(move |(cell_index, cell)| {
|
||||
if skip_width > 0 {
|
||||
skip_width -= 1;
|
||||
None
|
||||
} else {
|
||||
skip_width = cell.width().saturating_sub(1);
|
||||
Some(CellIter::CellRef { cell_index, cell })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn cluster(&self, bidi_hint: Option<ParagraphDirectionHint>) -> Vec<CellCluster> {
|
||||
@@ -884,7 +888,7 @@ impl Line {
|
||||
let mut attr = start_attr.clone();
|
||||
let mut text_run = String::new();
|
||||
|
||||
for (_, cell) in self.visible_cells() {
|
||||
for cell in self.visible_cells() {
|
||||
if *cell.attrs() == attr {
|
||||
text_run.push_str(cell.str());
|
||||
} else {
|
||||
@@ -951,6 +955,52 @@ impl<'a> From<&'a str> for Line {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum CellIter<'a> {
|
||||
CellRef { cell_index: usize, cell: &'a Cell },
|
||||
}
|
||||
|
||||
impl<'a> CellIter<'a> {
|
||||
pub fn cell_index(&self) -> usize {
|
||||
match self {
|
||||
Self::CellRef { cell_index, .. } => *cell_index,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn str(&self) -> &str {
|
||||
match self {
|
||||
Self::CellRef { cell, .. } => cell.str(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
match self {
|
||||
Self::CellRef { cell, .. } => cell.width(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn attrs(&self) -> &CellAttributes {
|
||||
match self {
|
||||
Self::CellRef { cell, .. } => cell.attrs(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn presentation(&self) -> Presentation {
|
||||
match self {
|
||||
Self::CellRef { cell, .. } => cell.presentation(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_cell(&self) -> Cell {
|
||||
match self {
|
||||
Self::CellRef { cell, .. } => (*cell).clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn same_contents(&self, other: &Self) -> bool {
|
||||
self.str() == other.str() && self.width() == other.width() && self.attrs() == other.attrs()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
+11
-14
@@ -1,6 +1,7 @@
|
||||
use crate::cell::{AttributeChange, Cell, CellAttributes};
|
||||
use crate::color::ColorAttribute;
|
||||
use crate::image::ImageCell;
|
||||
use crate::surface::line::CellIter;
|
||||
use ordered_float::NotNan;
|
||||
#[cfg(feature = "use_serde")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -132,8 +133,8 @@ struct DiffState {
|
||||
|
||||
impl DiffState {
|
||||
#[inline]
|
||||
fn diff_cells(&mut self, col_num: usize, row_num: usize, cell: &Cell, other_cell: &Cell) {
|
||||
if cell == other_cell {
|
||||
fn diff_cells(&mut self, col_num: usize, row_num: usize, cell: CellIter, other_cell: CellIter) {
|
||||
if cell.same_contents(&other_cell) {
|
||||
return;
|
||||
}
|
||||
self.cursor = match self.cursor.take() {
|
||||
@@ -509,7 +510,7 @@ impl Surface {
|
||||
let mut s = String::new();
|
||||
|
||||
for line in &self.lines {
|
||||
for (_, cell) in line.visible_cells() {
|
||||
for cell in line.visible_cells() {
|
||||
s.push_str(cell.str());
|
||||
}
|
||||
s.push('\n');
|
||||
@@ -779,13 +780,13 @@ impl Surface {
|
||||
.take_while(|(row_num, _)| *row_num < y + height)
|
||||
.zip(other.lines.iter().skip(other_y))
|
||||
{
|
||||
for ((col_num, cell), (_, other_cell)) in line
|
||||
for (cell, other_cell) in line
|
||||
.visible_cells()
|
||||
.skip(x)
|
||||
.take_while(|(col_num, _)| *col_num < x + width)
|
||||
.take_while(|cell| cell.cell_index() < x + width)
|
||||
.zip(other_line.visible_cells().skip(other_x))
|
||||
{
|
||||
diff_state.diff_cells(col_num, row_num, cell, other_cell);
|
||||
diff_state.diff_cells(cell.cell_index(), row_num, cell, other_cell);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -795,10 +796,8 @@ impl Surface {
|
||||
pub fn diff_lines(&self, other_lines: Vec<&Line>) -> Vec<Change> {
|
||||
let mut diff_state = DiffState::default();
|
||||
for ((row_num, line), other_line) in self.lines.iter().enumerate().zip(other_lines.iter()) {
|
||||
for ((col_num, cell), (_, other_cell)) in
|
||||
line.visible_cells().zip(other_line.visible_cells())
|
||||
{
|
||||
diff_state.diff_cells(col_num, row_num, cell, other_cell);
|
||||
for (cell, other_cell) in line.visible_cells().zip(other_line.visible_cells()) {
|
||||
diff_state.diff_cells(cell.cell_index(), row_num, cell, other_cell);
|
||||
}
|
||||
}
|
||||
diff_state.changes
|
||||
@@ -807,10 +806,8 @@ impl Surface {
|
||||
pub fn diff_against_numbered_line(&self, row_num: usize, other_line: &Line) -> Vec<Change> {
|
||||
let mut diff_state = DiffState::default();
|
||||
if let Some(line) = self.lines.get(row_num) {
|
||||
for ((col_num, cell), (_, other_cell)) in
|
||||
line.visible_cells().zip(other_line.visible_cells())
|
||||
{
|
||||
diff_state.diff_cells(col_num, row_num, cell, other_cell);
|
||||
for (cell, other_cell) in line.visible_cells().zip(other_line.visible_cells()) {
|
||||
diff_state.diff_cells(cell.cell_index(), row_num, cell, other_cell);
|
||||
}
|
||||
}
|
||||
diff_state.changes
|
||||
|
||||
@@ -88,7 +88,7 @@ impl UserData for PaneObject {
|
||||
let (_first_row, lines) = pane.get_lines(top_row..bottom_row);
|
||||
let mut text = String::new();
|
||||
for line in lines {
|
||||
for (_, cell) in line.visible_cells() {
|
||||
for cell in line.visible_cells() {
|
||||
text.push_str(cell.str());
|
||||
}
|
||||
let trimmed = text.trim_end().len();
|
||||
@@ -111,7 +111,7 @@ impl UserData for PaneObject {
|
||||
let lines = pane.get_logical_lines(top_row..bottom_row);
|
||||
let mut text = String::new();
|
||||
for line in lines {
|
||||
for (_, cell) in line.logical.visible_cells() {
|
||||
for cell in line.logical.visible_cells() {
|
||||
text.push_str(cell.str());
|
||||
}
|
||||
let trimmed = text.trim_end().len();
|
||||
|
||||
Reference in new issue
Block a user