Skip to content

Commit

Permalink
fix: unexpected spaces after large buffer input
Browse files Browse the repository at this point in the history
close #297
  • Loading branch information
sigoden committed Apr 22, 2024
1 parent 46f410b commit 9e88cc5
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/painting/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct Painter {
stdout: W,
prompt_start_row: u16,
terminal_size: (u16, u16),
last_required_lines: u16,
after_cursor_lines: Option<String>,
large_buffer: bool,
}

Expand All @@ -65,7 +65,7 @@ impl Painter {
stdout,
prompt_start_row: 0,
terminal_size: (0, 0),
last_required_lines: 0,
after_cursor_lines: None,
large_buffer: false,
}
}
Expand Down Expand Up @@ -181,9 +181,11 @@ impl Painter {
self.print_small_buffer(prompt, lines, menu, use_ansi_coloring)?;
}

// The last_required_lines is used to move the cursor at the end where stdout
// can print without overwriting the things written during the painting
self.last_required_lines = required_lines;
self.after_cursor_lines = if !lines.after_cursor.is_empty() {
Some(lines.after_cursor.to_string())
} else {
None
};

self.stdout.queue(RestorePosition)?;

Expand Down Expand Up @@ -474,18 +476,13 @@ impl Painter {
}

// The prompt is moved to the end of the buffer after the event was handled
// If the prompt is in the middle of a multiline buffer, then the output to stdout
// could overwrite the buffer writing
pub(crate) fn move_cursor_to_end(&mut self) -> Result<()> {
let final_row = self.prompt_start_row + self.last_required_lines;
let scroll = final_row.saturating_sub(self.screen_height() - 1);
if scroll != 0 {
self.queue_universal_scroll(scroll)?;
if let Some(after_cursor) = &self.after_cursor_lines {
self.stdout
.queue(Clear(ClearType::FromCursorDown))?
.queue(Print(after_cursor))?;
}
self.stdout
.queue(MoveTo(0, final_row.min(self.screen_height() - 1)))?;

self.stdout.flush()
self.print_crlf()
}

/// Prints an external message
Expand Down

0 comments on commit 9e88cc5

Please sign in to comment.