Skip to content

Commit 4cc6421

Browse files
kchibisovchrisduerr
authored andcommitted
Fix incorrect grid.len() and grid.history_size()
1 parent f48204e commit 4cc6421

File tree

37 files changed

+163
-218
lines changed

37 files changed

+163
-218
lines changed

Diff for: alacritty_terminal/src/grid/mod.rs

+44-69
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ mod tests;
3232
mod storage;
3333
use self::storage::Storage;
3434

35-
const MIN_INIT_SIZE: usize = 1_000;
36-
3735
/// Bidirection iterator
3836
pub trait BidirectionalIterator: Iterator {
3937
fn prev(&mut self) -> Option<Self::Item>;
@@ -62,7 +60,6 @@ impl<T: PartialEq> ::std::cmp::PartialEq for Grid<T> {
6260
&& self.cols.eq(&other.cols)
6361
&& self.lines.eq(&other.lines)
6462
&& self.display_offset.eq(&other.display_offset)
65-
&& self.scroll_limit.eq(&other.scroll_limit)
6663
&& self.selection.eq(&other.selection)
6764
}
6865
}
@@ -86,11 +83,11 @@ pub trait GridCell {
8683
/// │ │
8784
/// │ UNINITIALIZED │
8885
/// │ │
89-
/// ├─────────────────────────┤ <-- raw.len()
86+
/// ├─────────────────────────┤ <-- self.raw.inner.len()
9087
/// │ │
9188
/// │ RESIZE BUFFER │
9289
/// │ │
93-
/// ├─────────────────────────┤ <-- scroll_limit + lines
90+
/// ├─────────────────────────┤ <-- self.history_size() + lines
9491
/// │ │
9592
/// │ SCROLLUP REGION │
9693
/// │ │
@@ -112,26 +109,24 @@ pub struct Grid<T> {
112109
/// columns in that row.
113110
raw: Storage<T>,
114111

115-
/// Number of columns
112+
/// Number of columns.
116113
cols: Column,
117114

118115
/// Number of visible lines.
119116
lines: Line,
120117

121-
/// Offset of displayed area
118+
/// Offset of displayed area.
122119
///
123120
/// If the displayed region isn't at the bottom of the screen, it stays
124121
/// stationary while more text is emitted. The scrolling implementation
125122
/// updates this offset accordingly.
126123
display_offset: usize,
127124

128-
/// An limit on how far back it's possible to scroll
129-
scroll_limit: usize,
130-
131-
/// Selected region
125+
/// Selected region.
132126
#[serde(skip)]
133127
pub selection: Option<Selection>,
134128

129+
/// Maximum number of lines in history.
135130
max_scroll_limit: usize,
136131
}
137132

@@ -147,15 +142,7 @@ pub enum Scroll {
147142
impl<T: GridCell + PartialEq + Copy> Grid<T> {
148143
pub fn new(lines: Line, cols: Column, scrollback: usize, template: T) -> Grid<T> {
149144
let raw = Storage::with_capacity(lines, Row::new(cols, &template));
150-
Grid {
151-
raw,
152-
cols,
153-
lines,
154-
display_offset: 0,
155-
scroll_limit: 0,
156-
selection: None,
157-
max_scroll_limit: scrollback,
158-
}
145+
Grid { raw, cols, lines, display_offset: 0, selection: None, max_scroll_limit: scrollback }
159146
}
160147

161148
pub fn buffer_to_visible(&self, point: impl Into<Point<usize>>) -> Option<Point<usize>> {
@@ -179,28 +166,30 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
179166
}
180167

181168
/// Update the size of the scrollback history
182-
pub fn update_history(&mut self, history_size: usize, template: &T) {
183-
self.raw.update_history(history_size, Row::new(self.cols, &template));
169+
pub fn update_history(&mut self, history_size: usize) {
170+
let current_history_size = self.history_size();
171+
if current_history_size > history_size {
172+
self.raw.shrink_lines(current_history_size - history_size);
173+
}
174+
self.display_offset = min(self.display_offset, history_size);
184175
self.max_scroll_limit = history_size;
185-
self.scroll_limit = min(self.scroll_limit, history_size);
186-
self.display_offset = min(self.display_offset, self.scroll_limit);
187176
}
188177

189178
pub fn scroll_display(&mut self, scroll: Scroll) {
190179
match scroll {
191180
Scroll::Lines(count) => {
192181
self.display_offset = min(
193182
max((self.display_offset as isize) + count, 0isize) as usize,
194-
self.scroll_limit,
183+
self.history_size(),
195184
);
196185
},
197186
Scroll::PageUp => {
198-
self.display_offset = min(self.display_offset + self.lines.0, self.scroll_limit);
187+
self.display_offset = min(self.display_offset + self.lines.0, self.history_size());
199188
},
200189
Scroll::PageDown => {
201190
self.display_offset -= min(self.display_offset, self.lines.0);
202191
},
203-
Scroll::Top => self.display_offset = self.scroll_limit,
192+
Scroll::Top => self.display_offset = self.history_size(),
204193
Scroll::Bottom => self.display_offset = 0,
205194
}
206195
}
@@ -232,21 +221,17 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
232221
}
233222

234223
fn increase_scroll_limit(&mut self, count: usize, template: &T) {
235-
self.scroll_limit = min(self.scroll_limit + count, self.max_scroll_limit);
236-
237-
// Initialize new lines when the history buffer is smaller than the scroll limit
238-
let history_size = self.raw.len().saturating_sub(*self.lines);
239-
if history_size < self.scroll_limit {
240-
let new = min(
241-
max(self.scroll_limit - history_size, MIN_INIT_SIZE),
242-
self.max_scroll_limit - history_size,
243-
);
244-
self.raw.initialize(new, Row::new(self.cols, template));
224+
let count = min(count, self.max_scroll_limit - self.history_size());
225+
if count != 0 {
226+
self.raw.initialize(count, template, self.cols);
245227
}
246228
}
247229

248230
fn decrease_scroll_limit(&mut self, count: usize) {
249-
self.scroll_limit = self.scroll_limit.saturating_sub(count);
231+
let count = min(count, self.history_size());
232+
if count != 0 {
233+
self.raw.shrink_lines(min(count, self.history_size()));
234+
}
250235
}
251236

252237
/// Add lines to the visible area
@@ -262,12 +247,12 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
262247
self.lines = new_line_count;
263248

264249
// Move existing lines up if there is no scrollback to fill new lines
265-
if lines_added.0 > self.scroll_limit {
266-
let scroll_lines = lines_added - self.scroll_limit;
267-
self.scroll_up(&(Line(0)..new_line_count), scroll_lines, template);
250+
let history_size = self.history_size();
251+
if lines_added.0 > history_size {
252+
self.scroll_up(&(Line(0)..new_line_count), lines_added - history_size, template);
268253
}
269254

270-
self.scroll_limit = self.scroll_limit.saturating_sub(*lines_added);
255+
self.decrease_scroll_limit(*lines_added);
271256
self.display_offset = self.display_offset.saturating_sub(*lines_added);
272257
}
273258

@@ -326,22 +311,13 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
326311
last_row.append(&mut cells);
327312

328313
if row.is_empty() {
329-
let raw_len = i + 1 + reversed.len();
330-
if raw_len < self.lines.0 || self.scroll_limit == 0 {
314+
if i + reversed.len() < self.lines.0 {
331315
// Add new line and move lines up if we can't pull from history
332316
cursor_pos.line = Line(cursor_pos.line.saturating_sub(1));
333317
new_empty_lines += 1;
334-
} else {
335-
// Make sure viewport doesn't move if line is outside of the visible
336-
// area
337-
if i < self.display_offset {
338-
self.display_offset = self.display_offset.saturating_sub(1);
339-
}
340-
341-
// Remove one line from scrollback, since we just moved it to the
342-
// viewport
343-
self.scroll_limit = self.scroll_limit.saturating_sub(1);
344-
self.display_offset = min(self.display_offset, self.scroll_limit);
318+
} else if i < self.display_offset {
319+
// Keep viewport in place if line is outside of the visible area
320+
self.display_offset = self.display_offset.saturating_sub(1);
345321
}
346322

347323
// Don't push line into the new buffer
@@ -368,6 +344,7 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
368344

369345
self.raw.replace_inner(new_raw);
370346

347+
self.display_offset = min(self.display_offset, self.history_size());
371348
self.cols = cols;
372349
}
373350

@@ -450,9 +427,6 @@ impl<T: GridCell + PartialEq + Copy> Grid<T> {
450427
wrapped.append(&mut vec![*template; cols.0 - occ]);
451428
}
452429
row = Row::from_vec(wrapped, occ);
453-
454-
// Increase scrollback history
455-
self.scroll_limit = min(self.scroll_limit + 1, self.max_scroll_limit);
456430
}
457431
}
458432
}
@@ -643,6 +617,7 @@ impl<T> Grid<T> {
643617
self.lines
644618
}
645619

620+
#[inline]
646621
pub fn display_iter(&self) -> DisplayIter<'_, T> {
647622
DisplayIter::new(self)
648623
}
@@ -652,16 +627,10 @@ impl<T> Grid<T> {
652627
self.cols
653628
}
654629

630+
#[inline]
655631
pub fn clear_history(&mut self) {
656632
// Explicitly purge all lines from history
657-
let shrinkage = self.raw.len() - self.lines.0;
658-
self.raw.shrink_lines(shrinkage);
659-
self.scroll_limit = 0;
660-
}
661-
662-
#[inline]
663-
pub fn scroll_limit(&self) -> usize {
664-
self.scroll_limit
633+
self.raw.shrink_lines(self.history_size());
665634
}
666635

667636
/// Total number of lines in the buffer, this includes scrollback + visible lines
@@ -672,23 +641,29 @@ impl<T> Grid<T> {
672641

673642
#[inline]
674643
pub fn history_size(&self) -> usize {
675-
self.raw.len().saturating_sub(*self.lines)
644+
self.raw.len() - *self.lines
676645
}
677646

678647
/// This is used only for initializing after loading ref-tests
648+
#[inline]
679649
pub fn initialize_all(&mut self, template: &T)
680650
where
681651
T: Copy + GridCell,
682652
{
683-
let history_size = self.raw.len().saturating_sub(*self.lines);
684-
self.raw.initialize(self.max_scroll_limit - history_size, Row::new(self.cols, template));
653+
// Remove all cached lines to clear them of any content
654+
self.truncate();
655+
656+
// Initialize everything with empty new lines
657+
self.raw.initialize(self.max_scroll_limit - self.history_size(), template, self.cols);
685658
}
686659

687660
/// This is used only for truncating before saving ref-tests
661+
#[inline]
688662
pub fn truncate(&mut self) {
689663
self.raw.truncate();
690664
}
691665

666+
#[inline]
692667
pub fn iter_from(&self, point: Point<usize>) -> GridIterator<'_, T> {
693668
GridIterator { grid: self, cur: point }
694669
}

0 commit comments

Comments
 (0)