Skip to content

Commit e96b5d7

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 6811ca1 commit e96b5d7

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
@@ -446,14 +446,16 @@ impl Term {
446446
clear_line(self)
447447
}
448448

449-
/// Clear the last `n` lines before the current line.
449+
/// Clear the last `n` lines before the current line, if possible.
450450
///
451451
/// Position the cursor at the beginning of the first line that was cleared.
452-
///
453-
/// **Caution.** When `n` is larger than the number of lines above the
454-
/// current cursor, the top `n` lines are cleared --- including some lines
455-
/// below the cursor.
452+
/// Error when `n` is larger than the number of lines before the current cursor.
456453
pub fn clear_last_lines(&self, n: usize) -> io::Result<()> {
454+
let (current_row, _) = get_cursor_position(self)?;
455+
if usize::from(current_row) < n {
456+
// We cannot move up n lines, only current_row ones.
457+
return Err(io::Error::new(io::ErrorKind::Other, format!("can only move up {} lines, not {}", current_row, n)));
458+
}
457459
self.move_cursor_up(n)?;
458460
for _ in 0..n {
459461
self.clear_line()?;

0 commit comments

Comments
 (0)