From 7f5fd63835d2d539c8ef3f8cfd40beebe9ed51ae Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Wed, 20 Mar 2024 20:16:58 -0400 Subject: [PATCH] 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. --- helix-term/src/ui/statusline.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/statusline.rs b/helix-term/src/ui/statusline.rs index 79a66cc14..2939a2573 100644 --- a/helix-term/src/ui/statusline.rs +++ b/helix-term/src/ui/statusline.rs @@ -93,11 +93,26 @@ pub fn render_statusline<'a>(context: &mut RenderContext, width: usize) -> Spans let mut statusline: Vec = vec![]; 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 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.push(" ".repeat(center_margin - left_area_width).into()); + statusline.push(" ".repeat(left_spacers).into()); statusline.append(&mut center); - statusline.push(" ".repeat(center_margin - right_area_width).into()); + statusline.push(" ".repeat(right_spacers).into()); statusline.append(&mut right); } else if right_area_width > 0 && sides_space_required <= width { let side_areas_width = left_area_width + right_area_width;