diff options
author | Michael Davis | 2024-03-21 00:16:58 +0000 |
---|---|---|
committer | GitHub | 2024-03-21 00:16:58 +0000 |
commit | 7f5fd63835d2d539c8ef3f8cfd40beebe9ed51ae (patch) | |
tree | b59d866e8d3059b59fa2c453f2827e7c1cf0dff2 /helix-term/src/ui | |
parent | 52a0734120772f4dc2e523f50b6533466849fbeb (diff) |
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.
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r-- | helix-term/src/ui/statusline.rs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/helix-term/src/ui/statusline.rs b/helix-term/src/ui/statusline.rs index 79a66cc1..2939a257 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<Span> = 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; |