Skip to content

Commit 1315ceb

Browse files
committed
Make sure clear_last_lines never clears below the current cursor.
Instead, check the number of rows above the cursor position, and error if passed a too large number.
1 parent 59009bd commit 1315ceb

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/term.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,16 @@ impl Term {
434434
clear_line(self)
435435
}
436436

437-
/// Clear the last `n` lines before the current line.
437+
/// Clear the last `n` lines before the current line, if possible.
438438
///
439439
/// Position the cursor at the beginning of the first line that was cleared.
440-
///
441-
/// **Caution.** When `n` is larger than the number of lines above the
442-
/// current cursor, the top `n` lines are cleared --- including some lines
443-
/// below the cursor.
440+
/// Error when `n` is larger than the number of lines before the current cursor.
444441
pub fn clear_last_lines(&self, n: usize) -> io::Result<()> {
442+
let (current_row, _) = get_cursor_position(self)?;
443+
if usize::from(current_row) < n {
444+
// We cannot move up n lines, only current_row ones.
445+
return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n)));
446+
}
445447
self.move_cursor_up(n)?;
446448
for _ in 0..n {
447449
self.clear_line()?;

0 commit comments

Comments
 (0)