Skip to content

Commit

Permalink
Fix space remaining (again) (#3377)
Browse files Browse the repository at this point in the history
This commit was tested in a separate project
  • Loading branch information
jkelleyrtp authored Dec 17, 2024
1 parent 95cde59 commit fa48016
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions packages/cli/src/serve/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

// The only reliable way we can force the terminal downards is through "insert_before"
// - 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".
//
// 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!(
Expand Down Expand Up @@ -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(())
}

Expand Down

0 comments on commit fa48016

Please sign in to comment.