From fa48016d0cb5411dccb0797c454a57d0c17d1887 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Tue, 17 Dec 2024 13:42:09 -0800 Subject: [PATCH] Fix space remaining (again) (#3377) This commit was tested in a separate project --- packages/cli/src/serve/output.rs | 43 +++++++++++++------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/packages/cli/src/serve/output.rs b/packages/cli/src/serve/output.rs index 4f3750959..486fd507d 100644 --- a/packages/cli/src/serve/output.rs +++ b/packages/cli/src/serve/output.rs @@ -778,36 +778,32 @@ impl Output { // The viewport might be clipped, but the math still needs to work out. let actual_vh_height = self.viewport_current_height().min(term_size.height); - // We don't need to add any pushback if the frame is in the middle of the viewport - // We'll then add some pushback to ensure the log scrolls up above the viewport. - let max_scrollback = lines_printed.min(actual_vh_height.saturating_sub(1)); - // Move the terminal's cursor down to the number of lines printed let remaining_space = term_size .height .saturating_sub(frame_rect.y + frame_rect.height); // Calculate how many lines we need to push back - // - to_push equals lines_printed when the frame is at the bottom - // - to_push is zero when the remaining space is greater/equal than the scrollback (the frame will get pushed naturally) - let mut padding = max_scrollback.saturating_sub(remaining_space); + // - padding equals lines_printed when the frame is at the bottom + // - padding is zero when the remaining space is greater/equal than the scrollback (the frame will get pushed naturally) + // Determine what extra padding is remaining after we've shifted the terminal down + // this will be the distance between the final line and the top of the frame, only if the + // final line has extended into the frame + let final_line = frame_rect.y + lines_printed; + let max_frame_top = term_size.height - actual_vh_height; + let padding = final_line + .saturating_sub(max_frame_top) + .clamp(0, actual_vh_height - 1); - // The only reliable way we can force the terminal downards is through "insert_before" + // The only reliable way we can force the terminal downards is through "insert_before". + // // If we need to push the terminal down, we'll use this method with the number of lines // Ratatui will handle this rest. - // FIXME(jon): eventuallay insert_before will get scroll regions, breaking this, but making the logic here simpler - if padding == 0 { - terminal.insert_before(remaining_space.min(lines_printed), |_| {})?; - - // Determine what extra padding is remaining after we've shifted the terminal down - // this will be the distance between the final line and the top of the frame, only if the - // final line has extended into the frame - let frame_top = term_size.height - actual_vh_height; - let end_y = (frame_rect.y + lines_printed).min(term_size.height); - if end_y > frame_top { - padding = end_y.saturating_sub(frame_top); - } - } + // + // This also calls `.clear()` so we don't need to call clear at the end of this function. + // + // FIXME(jon): eventually insert_before will get scroll regions, breaking this, but making the logic here simpler + terminal.insert_before(remaining_space.min(lines_printed), |_| {})?; // Wipe the viewport clean so it doesn't tear crossterm::queue!( @@ -838,11 +834,6 @@ impl Output { )?; } - // Force a clear - // Might've been triggered by insert_before already, but supposedly double-queuing is fine - // since this isn't a "real" synchronous clear - terminal.clear()?; - Ok(()) }