Evenly space statusline areas when there isn't space to align middle (#9950)

The refactor in bcf7b263 introduced a possible subtraction with overflow
when the statusline is layed out so that the left or right sides are
larger than the padding it would take to align the center area to the
middle.

When the left or right areas are too large, we can evenly space the
elements rather than trying to align the center area to the middle.
This prevents possible underflows and makes sense visually - it's
still easy to tell the areas apart at a glance.
pull/8875/head
Michael Davis 8 months ago committed by GitHub
parent 52a0734120
commit 7f5fd63835
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -93,11 +93,26 @@ pub fn render_statusline<'a>(context: &mut RenderContext, width: usize) -> Spans
let mut statusline: Vec<Span> = vec![]; let mut statusline: Vec<Span> = vec![];
if center_area_width > 0 && total_space_required <= width { if center_area_width > 0 && total_space_required <= width {
// SAFETY: this subtraction cannot underflow because `left_area_width + center_area_width + right_area_width`
// is smaller than `total_space_required`, which is smaller than `width` in this branch.
let total_spacers = width - (left_area_width + center_area_width + right_area_width);
// This is how much padding space it would take on either side to align the center area to the middle.
let center_margin = (width - center_area_width) / 2; let center_margin = (width - center_area_width) / 2;
let left_spacers = if left_area_width < center_margin && right_area_width < center_margin {
// Align the center area to the middle if there is enough space on both sides.
center_margin - left_area_width
} else {
// Otherwise split the available space evenly and use it as margin.
// The center element won't be aligned to the middle but it will be evenly
// spaced between the left and right areas.
total_spacers / 2
};
let right_spacers = total_spacers - left_spacers;
statusline.append(&mut left); statusline.append(&mut left);
statusline.push(" ".repeat(center_margin - left_area_width).into()); statusline.push(" ".repeat(left_spacers).into());
statusline.append(&mut center); statusline.append(&mut center);
statusline.push(" ".repeat(center_margin - right_area_width).into()); statusline.push(" ".repeat(right_spacers).into());
statusline.append(&mut right); statusline.append(&mut right);
} else if right_area_width > 0 && sides_space_required <= width { } else if right_area_width > 0 && sides_space_required <= width {
let side_areas_width = left_area_width + right_area_width; let side_areas_width = left_area_width + right_area_width;

Loading…
Cancel
Save