From dec5ca034980bd3d7f3565dbf750e1f2c333d075 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 24 Jul 2022 10:57:05 -0700 Subject: [PATCH] term: refactor getting logical lines This will make it easier to refactor search in a subsequent commit --- mux/src/localpane.rs | 6 +- mux/src/pane.rs | 114 +++++++++++++++++++++++++++++++----- mux/src/renderable.rs | 40 ++++++++++++- term/src/screen.rs | 72 +++++++++++++++++++++++ termwiz/src/surface/line.rs | 4 +- 5 files changed, 217 insertions(+), 19 deletions(-) diff --git a/mux/src/localpane.rs b/mux/src/localpane.rs index b22e63976..26d92a2a7 100644 --- a/mux/src/localpane.rs +++ b/mux/src/localpane.rs @@ -1,5 +1,5 @@ use crate::domain::DomainId; -use crate::pane::{CloseReason, Pane, PaneId, Pattern, SearchResult}; +use crate::pane::{CloseReason, LogicalLine, Pane, PaneId, Pattern, SearchResult}; use crate::renderable::*; use crate::tmux::{TmuxDomain, TmuxDomainState}; use crate::{Domain, Mux, MuxNotification}; @@ -111,6 +111,10 @@ impl Pane for LocalPane { (first, lines) } + fn get_logical_lines(&self, lines: Range) -> Vec { + terminal_get_logical_lines(&mut self.terminal.borrow_mut(), lines) + } + fn get_dimensions(&self) -> RenderableDimensions { terminal_get_dimensions(&mut self.terminal.borrow_mut()) } diff --git a/mux/src/pane.rs b/mux/src/pane.rs index 11818e778..0aceb786d 100644 --- a/mux/src/pane.rs +++ b/mux/src/pane.rs @@ -230,6 +230,9 @@ pub trait Pane: Downcast { fn get_lines(&self, lines: Range) -> (StableRowIndex, Vec); fn get_logical_lines(&self, lines: Range) -> Vec { + // NOTE: see terminal_get_logical_lines() for the implementation that is + // actually used by most panes; this particular method is the fallback + // implementation for other Panes. let (mut first, mut phys) = self.get_lines(lines); // Avoid pathological cases where we have eg: a really long logical line @@ -572,6 +575,95 @@ mod test { physical_lines } + #[test] + fn logical_lines_terminal() { + use wezterm_term::{Terminal, TerminalConfiguration}; + + #[derive(Debug)] + struct TermConfig {} + impl TerminalConfiguration for TermConfig { + fn color_palette(&self) -> ColorPalette { + ColorPalette::default() + } + } + + let mut terminal = Terminal::new( + TerminalSize { + rows: 24, + cols: 20, + pixel_width: 0, + pixel_height: 0, + dpi: 0, + }, + Arc::new(TermConfig {}), + "WezTerm", + "o_O", + Box::new(Vec::new()), + ); + + let text = "Hello there this is a long line.\r\nlogical line two\r\nanother long line here\r\nlogical line four\r\nlogical line five\r\ncap it off with another long line"; + terminal.advance_bytes(text.as_bytes()); + + let logical = terminal_get_logical_lines(&mut terminal, 0..9); + + snapshot!( + summarize_logical_lines(&logical), + r#" +[ + ( + 0, + "Hello there this is a long line.", + ), + ( + 2, + "logical line two", + ), + ( + 3, + "another long line here", + ), + ( + 5, + "logical line four", + ), + ( + 6, + "logical line five", + ), + ( + 7, + "cap it off with another long line", + ), +] +"# + ); + + // Now try with offset bounds + let offset = terminal_get_logical_lines(&mut terminal, 1..3); + snapshot!( + summarize_logical_lines(&offset), + r#" +[ + ( + 0, + "Hello there this is a long line.", + ), + ( + 2, + "logical line two", + ), +] +"# + ); + } + + fn summarize_logical_lines(lines: &[LogicalLine]) -> Vec<(StableRowIndex, Cow)> { + lines + .iter() + .map(|l| (l.first_row, l.logical.as_str())) + .collect::>() + } + #[test] fn logical_lines() { let text = "Hello there this is a long line.\nlogical line two\nanother long line here\nlogical line four\nlogical line five\ncap it off with another long line"; @@ -604,13 +696,6 @@ mod test { lines: physical_lines, }; - fn summarize_logical_lines(lines: &[LogicalLine]) -> Vec<(StableRowIndex, Cow)> { - lines - .iter() - .map(|l| (l.first_row, l.logical.as_str())) - .collect::>() - } - let logical = pane.get_logical_lines(0..30); snapshot!( summarize_logical_lines(&logical), @@ -932,6 +1017,14 @@ mod test { ); } + fn is_double_click_word(s: &str) -> bool { + match s.chars().count() { + 1 => !" \t\n{[}]()\"'`".contains(s), + 0 => false, + _ => true, + } + } + #[test] fn double_click() { let attr = Default::default(); @@ -949,13 +1042,6 @@ mod test { let start_idx = logical.xy_to_logical_x(2, 1); - fn is_double_click_word(s: &str) -> bool { - match s.chars().count() { - 1 => !" \t\n{[}]()\"'`".contains(s), - 0 => false, - _ => true, - } - } use termwiz::surface::line::DoubleClickRange; assert_eq!(start_idx, 7); diff --git a/mux/src/renderable.rs b/mux/src/renderable.rs index d36db3eb7..385a4375f 100644 --- a/mux/src/renderable.rs +++ b/mux/src/renderable.rs @@ -1,8 +1,9 @@ +use crate::pane::LogicalLine; use luahelper::impl_lua_conversion_dynamic; use rangeset::RangeSet; use serde::{Deserialize, Serialize}; use std::ops::Range; -use termwiz::surface::{SequenceNo, SEQ_ZERO}; +use termwiz::surface::SequenceNo; use wezterm_dynamic::{FromDynamic, ToDynamic}; use wezterm_term::{Line, StableRowIndex, Terminal}; @@ -69,6 +70,40 @@ pub fn terminal_get_dirty_lines( set } +pub fn terminal_get_logical_lines( + term: &mut Terminal, + lines: Range, +) -> Vec { + let screen = term.screen(); + let mut result = vec![]; + let reverse = term.get_reverse_video(); + screen.for_each_logical_line_in_stable_range(lines.clone(), |sr, lines| { + let mut physical_lines: Vec = lines + .iter() + .map(|line| { + let mut line = (*line).clone(); + let seqno = line.current_seqno(); + line.set_reverse(reverse, seqno); + line + }) + .collect(); + + let mut logical = physical_lines[0].clone(); + for line in &mut physical_lines[1..] { + let seqno = line.current_seqno(); + logical.set_last_cell_was_wrapped(false, seqno); + logical.append_line((*line).clone(), seqno); + } + + result.push(LogicalLine { + physical_lines, + logical, + first_row: sr.start, + }); + }); + result +} + /// Implements Pane::get_lines for Terminal pub fn terminal_get_lines( term: &mut Terminal, @@ -81,7 +116,8 @@ pub fn terminal_get_lines( let first = screen.phys_to_stable_row_index(phys_range.start); let mut lines = screen.lines_in_phys_range(phys_range); for line in &mut lines { - line.set_reverse(reverse, SEQ_ZERO); + let seqno = line.current_seqno(); + line.set_reverse(reverse, seqno); } (first, lines) diff --git a/term/src/screen.rs b/term/src/screen.rs index 288b547ca..cf50ea8e6 100644 --- a/term/src/screen.rs +++ b/term/src/screen.rs @@ -902,4 +902,76 @@ impl Screen { f(idx, line); } } + + pub fn for_each_logical_line_in_stable_range( + &self, + stable_range: Range, + mut f: F, + ) where + F: FnMut(Range, &[&Line]), + { + let mut phys_range = self.stable_range(&stable_range); + + // Avoid pathological cases where we have eg: a really long logical line + // (such as 1.5MB of json) that we previously wrapped. We don't want to + // un-wrap, scan, and re-wrap that thing. + // This is an imperfect length constraint to partially manage the cost. + const MAX_LOGICAL_LINE_LEN: usize = 1024; + + // Look backwards to find the start of the first logical line + let mut back_len = 0; + while phys_range.start > 0 { + let prior = &self.lines[phys_range.start - 1]; + if !prior.last_cell_was_wrapped() { + break; + } + if prior.len() + back_len > MAX_LOGICAL_LINE_LEN { + break; + } + back_len += prior.len(); + phys_range.start -= 1 + } + + let mut phys_row = phys_range.start; + let mut line_vec: Vec<&Line> = vec![]; + while phys_row < phys_range.end { + // Look forwards until we find the end of this logical line + let mut total_len = 0; + let mut end_inclusive = phys_row; + line_vec.clear(); + + for idx in phys_row.. { + if let Some(line) = self.lines.get(idx) { + if total_len > 0 && total_len + line.len() > MAX_LOGICAL_LINE_LEN { + break; + } + end_inclusive = idx; + total_len += line.len(); + line_vec.push(line); + if !line.last_cell_was_wrapped() { + break; + } + } else if idx == phys_row { + // No more rows exist + return; + } else { + break; + } + } + + let logical_stable_range = self.phys_to_stable_row_index(phys_row) + ..self.phys_to_stable_row_index(end_inclusive + 1); + + phys_row = end_inclusive + 1; + + if logical_stable_range.end < stable_range.start { + continue; + } + if logical_stable_range.start > stable_range.end { + break; + } + + f(logical_stable_range, &line_vec); + } + } } diff --git a/termwiz/src/surface/line.rs b/termwiz/src/surface/line.rs index 2be2f8603..2c2216aeb 100644 --- a/termwiz/src/surface/line.rs +++ b/termwiz/src/surface/line.rs @@ -675,7 +675,7 @@ impl Line { } if let CellStorage::C(cl) = &mut self.cells { - if idx >= cl.len && text == " " && attr == CellAttributes::blank() { + if idx > cl.len && text == " " && attr == CellAttributes::blank() { // Appending blank beyond end of line; is already // implicitly blank return; @@ -728,7 +728,7 @@ impl Line { } if let CellStorage::C(cl) = &mut self.cells { - if idx >= cl.len && cell == Cell::blank() { + if idx > cl.len && cell == Cell::blank() { // Appending blank beyond end of line; is already // implicitly blank return;