From bcf7b26393cd2a32e38c187f69b602be018798f4 Mon Sep 17 00:00:00 2001 From: Szabin Date: Tue, 19 Mar 2024 17:31:39 +0100 Subject: Refactor statusline elements to build `Spans` (#9122) * Refactor statusline elements to return Spans * Split render fn to build Spans and blit to Surface--- helix-term/src/ui/statusline.rs | 397 ++++++++++++++++------------------------ 1 file changed, 155 insertions(+), 242 deletions(-) (limited to 'helix-term/src/ui') diff --git a/helix-term/src/ui/statusline.rs b/helix-term/src/ui/statusline.rs index 7437cbd0..79a66cc1 100644 --- a/helix-term/src/ui/statusline.rs +++ b/helix-term/src/ui/statusline.rs @@ -4,7 +4,6 @@ use helix_view::document::DEFAULT_LANGUAGE_NAME; use helix_view::{ document::{Mode, SCRATCH_BUFFER_NAME}, graphics::Rect, - theme::Style, Document, Editor, View, }; @@ -20,7 +19,6 @@ pub struct RenderContext<'a> { pub view: &'a View, pub focused: bool, pub spinners: &'a ProgressSpinners, - pub parts: RenderBuffer<'a>, } impl<'a> RenderContext<'a> { @@ -37,18 +35,10 @@ 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") @@ -58,85 +48,72 @@ pub fn render(context: &mut RenderContext, viewport: Rect, surface: &mut Surface surface.set_style(viewport.with_height(1), base_style); - 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)); + let statusline = render_statusline(context, viewport.width as usize); surface.set_spans( viewport.x, viewport.y, - &context.parts.left, - context.parts.left.width() as u16, + &statusline, + statusline.width() as u16, ); +} - // Right side of the status line. +pub fn render_statusline<'a>(context: &mut RenderContext, width: usize) -> Spans<'a> { + let config = context.editor.config(); - let element_ids = &config.statusline.right; - element_ids + let element_ids = &config.statusline.left; + let mut left = element_ids .iter() .map(|element_id| get_render_function(*element_id)) - .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. + .flat_map(|render| render(context).0) + .collect::>(); let element_ids = &config.statusline.center; - element_ids + let mut center = element_ids .iter() .map(|element_id| get_render_function(*element_id)) - .for_each(|render| render(context, write_center)); + .flat_map(|render| render(context).0) + .collect::>(); - // Width of the empty space between the left and center area and between the center and right area. - let spacing = 1u16; - - 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, - ); -} + 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 { + let center_margin = (width - center_area_width) / 2; + statusline.append(&mut left); + statusline.push(" ".repeat(center_margin - left_area_width).into()); + statusline.append(&mut center); + statusline.push(" ".repeat(center_margin - right_area_width).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); + } -fn append(buffer: &mut Spans, text: String, base_style: &Style, style: Option