1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

refactor: hide Screen::lines field

I want to make some changes to the scrollback structure that
first require routing some operations through an accessor
method, so this commit does that.

It should have no functional difference.
This commit is contained in:
Wez Furlong 2022-04-20 23:28:12 -07:00
parent 35054ed291
commit 2839293674
5 changed files with 86 additions and 51 deletions

View File

@ -564,7 +564,7 @@ impl Pane for LocalPane {
} }
} }
for (idx, line) in screen.lines.iter().enumerate() { screen.for_each_phys_line(|idx, line| {
let stable_row = screen.phys_to_stable_row_index(idx); let stable_row = screen.phys_to_stable_row_index(idx);
let mut wrapped = false; let mut wrapped = false;
@ -607,7 +607,7 @@ impl Pane for LocalPane {
coords.clear(); coords.clear();
} }
} }
} });
collect_matches( collect_matches(
&mut results, &mut results,

View File

@ -55,18 +55,10 @@ pub fn terminal_get_dirty_lines(
seqno: SequenceNo, seqno: SequenceNo,
) -> RangeSet<StableRowIndex> { ) -> RangeSet<StableRowIndex> {
let screen = term.screen(); let screen = term.screen();
let phys = screen.stable_range(&lines); let lines = screen.get_changed_stable_rows(lines, seqno);
let mut set = RangeSet::new(); let mut set = RangeSet::new();
for (idx, line) in screen for line in lines {
.lines set.add(line);
.iter()
.enumerate()
.skip(phys.start)
.take(phys.end - phys.start)
{
if line.changed_since(seqno) {
set.add(screen.phys_to_stable_row_index(idx))
}
} }
set set
} }
@ -79,20 +71,14 @@ pub fn terminal_get_lines(
let reverse = term.get_reverse_video(); let reverse = term.get_reverse_video();
let screen = term.screen_mut(); let screen = term.screen_mut();
let phys_range = screen.stable_range(&lines); let phys_range = screen.stable_range(&lines);
(
screen.phys_to_stable_row_index(phys_range.start), let first = screen.phys_to_stable_row_index(phys_range.start);
screen let mut lines = screen.lines_in_phys_range(phys_range);
.lines for line in &mut lines {
.iter_mut() line.set_reverse(reverse, SEQ_ZERO);
.skip(phys_range.start) }
.take(phys_range.end - phys_range.start)
.map(|line| { (first, lines)
let mut cloned = line.clone();
cloned.set_reverse(reverse, SEQ_ZERO);
cloned
})
.collect(),
)
} }
/// Implements Pane::get_dimensions for Terminal /// Implements Pane::get_dimensions for Terminal
@ -101,7 +87,7 @@ pub fn terminal_get_dimensions(term: &mut Terminal) -> RenderableDimensions {
RenderableDimensions { RenderableDimensions {
cols: screen.physical_cols, cols: screen.physical_cols,
viewport_rows: screen.physical_rows, viewport_rows: screen.physical_rows,
scrollback_rows: screen.lines.len(), scrollback_rows: screen.scrollback_rows(),
physical_top: screen.visible_row_to_stable_row(0), physical_top: screen.visible_row_to_stable_row(0),
scrollback_top: screen.phys_to_stable_row_index(0), scrollback_top: screen.phys_to_stable_row_index(0),
} }

View File

@ -20,7 +20,7 @@ pub struct Screen {
/// on the current window size) and will be the first line to be /// on the current window size) and will be the first line to be
/// popped off the front of the screen when a new line is added that /// popped off the front of the screen when a new line is added that
/// would otherwise have exceeded the line capacity /// would otherwise have exceeded the line capacity
pub lines: VecDeque<Line>, lines: VecDeque<Line>,
/// Whenever we scroll a line off the top of the scrollback, we /// Whenever we scroll a line off the top of the scrollback, we
/// increment this. We use this offset to translate between /// increment this. We use this offset to translate between
@ -296,6 +296,11 @@ impl Screen {
&mut self.lines[idx] &mut self.lines[idx]
} }
/// Returns the number of occupied rows of scrollback
pub fn scrollback_rows(&self) -> usize {
self.lines.len()
}
/// Sets a line dirty. The line is relative to the visible origin. /// Sets a line dirty. The line is relative to the visible origin.
#[inline] #[inline]
pub fn dirty_line(&mut self, idx: VisibleRowIndex, seqno: SequenceNo) { pub fn dirty_line(&mut self, idx: VisibleRowIndex, seqno: SequenceNo) {
@ -810,4 +815,52 @@ impl Screen {
} }
} }
} }
pub fn lines_in_phys_range(&self, phys_range: Range<PhysRowIndex>) -> Vec<Line> {
self.lines
.iter()
.skip(phys_range.start)
.take(phys_range.end - phys_range.start)
.map(|line| line.clone())
.collect()
}
pub fn get_changed_stable_rows(
&self,
stable_lines: Range<StableRowIndex>,
seqno: SequenceNo,
) -> Vec<StableRowIndex> {
let phys = self.stable_range(&stable_lines);
let mut set = vec![];
for (idx, line) in self
.lines
.iter()
.enumerate()
.skip(phys.start)
.take(phys.end - phys.start)
{
if line.changed_since(seqno) {
set.push(self.phys_to_stable_row_index(idx))
}
}
set
}
pub fn for_each_phys_line<F>(&self, mut f: F)
where
F: FnMut(usize, &Line),
{
for (idx, line) in self.lines.iter().enumerate() {
f(idx, line);
}
}
pub fn for_each_phys_line_mut<F>(&mut self, mut f: F)
where
F: FnMut(usize, &mut Line),
{
for (idx, line) in self.lines.iter_mut().enumerate() {
f(idx, line);
}
}
} }

View File

@ -683,11 +683,13 @@ impl TerminalState {
self.erase_in_display(EraseInDisplay::EraseScrollback); self.erase_in_display(EraseInDisplay::EraseScrollback);
let row_index = self.screen.phys_row(self.cursor.y); let row_index = self.screen.phys_row(self.cursor.y);
let row = self.screen.lines[row_index].clone(); let rows = self.screen.lines_in_phys_range(row_index..row_index + 1);
self.erase_in_display(EraseInDisplay::EraseDisplay); self.erase_in_display(EraseInDisplay::EraseDisplay);
self.screen.lines[0] = row; for (idx, row) in rows.into_iter().enumerate() {
*self.screen.line_mut(idx) = row;
}
self.cursor.y = 0; self.cursor.y = 0;
} }
@ -864,9 +866,9 @@ impl TerminalState {
pub fn make_all_lines_dirty(&mut self) { pub fn make_all_lines_dirty(&mut self) {
let seqno = self.seqno; let seqno = self.seqno;
let screen = self.screen_mut(); let screen = self.screen_mut();
for line in &mut screen.lines { screen.for_each_phys_line_mut(|_, line| {
line.update_last_change_seqno(seqno); line.update_last_change_seqno(seqno);
} });
} }
/// Returns the 0-based cursor position relative to the top left of /// Returns the 0-based cursor position relative to the top left of
@ -2506,7 +2508,7 @@ impl TerminalState {
let mut zones = vec![]; let mut zones = vec![];
let first_stable_row = screen.phys_to_stable_row_index(0); let first_stable_row = screen.phys_to_stable_row_index(0);
for (idx, line) in screen.lines.iter_mut().enumerate() { screen.for_each_phys_line_mut(|idx, line| {
let stable_row = first_stable_row + idx as StableRowIndex; let stable_row = first_stable_row + idx as StableRowIndex;
for zone_range in line.semantic_zone_ranges() { for zone_range in line.semantic_zone_ranges() {
@ -2534,7 +2536,7 @@ impl TerminalState {
zone.end_y = stable_row; zone.end_y = stable_row;
} }
} }
} });
if let Some(zone) = current_zone.take() { if let Some(zone) = current_zone.take() {
zones.push(zone); zones.push(zone);
} }

View File

@ -167,20 +167,14 @@ impl TestTerm {
fn assert_dirty_lines(&self, seqno: SequenceNo, expected: &[usize], reason: Option<&str>) { fn assert_dirty_lines(&self, seqno: SequenceNo, expected: &[usize], reason: Option<&str>) {
let mut seqs = vec![]; let mut seqs = vec![];
let dirty_indices: Vec<usize> = self let mut dirty_indices = vec![];
.screen()
.lines self.screen().for_each_phys_line(|i, line| {
.iter() seqs.push(line.current_seqno());
.enumerate() if line.changed_since(seqno) {
.filter_map(|(i, line)| { dirty_indices.push(i);
seqs.push(line.current_seqno()); }
if line.changed_since(seqno) { });
Some(i)
} else {
None
}
})
.collect();
assert_eq!( assert_eq!(
&dirty_indices, &expected, &dirty_indices, &expected,
"actual dirty lines (left) didn't match expected dirty \ "actual dirty lines (left) didn't match expected dirty \
@ -280,9 +274,9 @@ fn print_all_lines(term: &Terminal) {
let screen = term.screen(); let screen = term.screen();
println!("whole screen contents are:"); println!("whole screen contents are:");
for line in screen.lines.iter() { screen.for_each_phys_line(|_, line| {
println!("[{}]", line.as_str()); println!("[{}]", line.as_str());
} });
} }
fn print_visible_lines(term: &Terminal) { fn print_visible_lines(term: &Terminal) {