Skip to content

Commit

Permalink
Make sure clear_last_lines never clears below the current cursor.
Browse files Browse the repository at this point in the history
Instead, check the number of rows above the cursor position,
and error if passed a too large number.
  • Loading branch information
grunweg committed Apr 29, 2022
1 parent 6811ca1 commit e96b5d7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,14 +446,16 @@ impl Term {
clear_line(self)
}

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

0 comments on commit e96b5d7

Please sign in to comment.