From 8779370cd2e44c054434fe8573c37d6bc37ef108 Mon Sep 17 00:00:00 2001 From: Szabin Date: Mon, 6 May 2024 18:51:20 +0200 Subject: [PATCH] Revert "Refactor statusline elements to build `Spans` (#9122)" (#10642) --- helix-term/src/ui/statusline.rs | 412 +++++++++++++++++++------------- 1 file changed, 242 insertions(+), 170 deletions(-) diff --git a/helix-term/src/ui/statusline.rs b/helix-term/src/ui/statusline.rs index 8a87242f99e8..7437cbd074e5 100644 --- a/helix-term/src/ui/statusline.rs +++ b/helix-term/src/ui/statusline.rs @@ -4,6 +4,7 @@ use helix_view::document::DEFAULT_LANGUAGE_NAME; use helix_view::{ document::{Mode, SCRATCH_BUFFER_NAME}, graphics::Rect, + theme::Style, Document, Editor, View, }; @@ -19,6 +20,7 @@ pub struct RenderContext<'a> { pub view: &'a View, pub focused: bool, pub spinners: &'a ProgressSpinners, + pub parts: RenderBuffer<'a>, } impl<'a> RenderContext<'a> { @@ -35,10 +37,18 @@ impl<'a> RenderContext<'a> { view, focused, spinners, + parts: RenderBuffer::default(), } } } +#[derive(Default)] +pub struct RenderBuffer<'a> { + pub left: Spans<'a>, + pub center: Spans<'a>, + pub right: Spans<'a>, +} + pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface) { let base_style = if context.focused { context.editor.theme.get("ui.statusline") @@ -48,87 +58,85 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface surface.set_style(viewport.with_height(1), base_style); - let statusline = render_statusline(context, viewport.width as usize); + let write_left = |context: &mut RenderContext, text, style| { + append(&mut context.parts.left, text, &base_style, style) + }; + let write_center = |context: &mut RenderContext, text, style| { + append(&mut context.parts.center, text, &base_style, style) + }; + let write_right = |context: &mut RenderContext, text, style| { + append(&mut context.parts.right, text, &base_style, style) + }; + + // Left side of the status line. + + let config = context.editor.config(); + + let element_ids = &config.statusline.left; + element_ids + .iter() + .map(|element_id| get_render_function(*element_id)) + .for_each(|render| render(context, write_left)); surface.set_spans( viewport.x, viewport.y, - &statusline, - statusline.width() as u16, + &context.parts.left, + context.parts.left.width() as u16, ); -} -pub fn render_statusline<'a>(context: &mut RenderContext, width: usize) -> Spans<'a> { - let config = context.editor.config(); + // Right side of the status line. - let element_ids = &config.statusline.left; - let mut left = element_ids + let element_ids = &config.statusline.right; + element_ids .iter() .map(|element_id| get_render_function(*element_id)) - .flat_map(|render| render(context).0) - .collect::>(); + .for_each(|render| render(context, write_right)); + + surface.set_spans( + viewport.x + + viewport + .width + .saturating_sub(context.parts.right.width() as u16), + viewport.y, + &context.parts.right, + context.parts.right.width() as u16, + ); + + // Center of the status line. let element_ids = &config.statusline.center; - let mut center = element_ids + element_ids .iter() .map(|element_id| get_render_function(*element_id)) - .flat_map(|render| render(context).0) - .collect::>(); + .for_each(|render| render(context, write_center)); - let element_ids = &config.statusline.right; - let mut right = element_ids - .iter() - .map(|element_id| get_render_function(*element_id)) - .flat_map(|render| render(context).0) - .collect::>(); - - let left_area_width: usize = left.iter().map(|s| s.width()).sum(); - let center_area_width: usize = center.iter().map(|s| s.width()).sum(); - let right_area_width: usize = right.iter().map(|s| s.width()).sum(); - - let min_spacing_between_areas = 1usize; - let sides_space_required = left_area_width + right_area_width + min_spacing_between_areas; - let total_space_required = sides_space_required + center_area_width + min_spacing_between_areas; - - 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(left_spacers).into()); - statusline.append(&mut center); - 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; - statusline.append(&mut left); - statusline.push(" ".repeat(width - side_areas_width).into()); - statusline.append(&mut right); - } else if left_area_width <= width { - statusline.append(&mut left); - } + // Width of the empty space between the left and center area and between the center and right area. + let spacing = 1u16; - statusline.into() + let edge_width = context.parts.left.width().max(context.parts.right.width()) as u16; + let center_max_width = viewport.width.saturating_sub(2 * edge_width + 2 * spacing); + let center_width = center_max_width.min(context.parts.center.width() as u16); + + surface.set_spans( + viewport.x + viewport.width / 2 - center_width / 2, + viewport.y, + &context.parts.center, + center_width, + ); +} + +fn append(buffer: &mut Spans, text: String, base_style: &Style, style: Option