Skip to content

Commit

Permalink
Evenly space statusline areas when there isn't space to align middle (h…
Browse files Browse the repository at this point in the history
…elix-editor#9950)

The refactor in bcf7b26 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.
  • Loading branch information
the-mikedavis authored and Schuyler Mortimer committed Jul 10, 2024
1 parent adb55d6 commit ae3d1a9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ae3d1a9

Please sign in to comment.