term: refactor getting logical lines
This will make it easier to refactor search in a subsequent commit
This commit is contained in:
1 parent
c722db22d6
commit
dec5ca0349
5 files changed
+217
-19
No files matched your search
@@ -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<StableRowIndex>) -> Vec<LogicalLine> {
|
||||
terminal_get_logical_lines(&mut self.terminal.borrow_mut(), lines)
|
||||
}
|
||||
|
||||
fn get_dimensions(&self) -> RenderableDimensions {
|
||||
terminal_get_dimensions(&mut self.terminal.borrow_mut())
|
||||
}
|
||||
|
||||
+100
-14
@@ -230,6 +230,9 @@ pub trait Pane: Downcast {
|
||||
fn get_lines(&self, lines: Range<StableRowIndex>) -> (StableRowIndex, Vec<Line>);
|
||||
|
||||
fn get_logical_lines(&self, lines: Range<StableRowIndex>) -> Vec<LogicalLine> {
|
||||
// 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<str>)> {
|
||||
lines
|
||||
.iter()
|
||||
.map(|l| (l.first_row, l.logical.as_str()))
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
#[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<str>)> {
|
||||
lines
|
||||
.iter()
|
||||
.map(|l| (l.first_row, l.logical.as_str()))
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
+38
-2
@@ -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<StableRowIndex>,
|
||||
) -> Vec<LogicalLine> {
|
||||
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<Line> = 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)
|
||||
|
||||
@@ -902,4 +902,76 @@ impl Screen {
|
||||
f(idx, line);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_each_logical_line_in_stable_range<F>(
|
||||
&self,
|
||||
stable_range: Range<StableRowIndex>,
|
||||
mut f: F,
|
||||
) where
|
||||
F: FnMut(Range<StableRowIndex>, &[&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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user