From 37473dbbaa198daf7fb9fbfc82737ca74ff8fec3 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Sat, 21 Oct 2023 06:20:42 -0700 Subject: [PATCH] Make `RenderSeparator` a `RenderDynamic` --- src/builder.rs | 45 ++++++++++++++++++++++++++++++++++-------- src/diff.rs | 1 - src/presentation.rs | 3 --- src/render/operator.rs | 8 -------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 183f985d..56ed8b20 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -295,7 +295,7 @@ impl<'a> PresentationBuilder<'a> { self.push_line_break(); } if style.separator { - self.slide_operations.push(RenderOperation::RenderSeparator); + self.slide_operations.push(RenderSeparator::default().into()); } self.push_line_break(); self.ignore_element_line_break = true; @@ -339,7 +339,7 @@ impl<'a> PresentationBuilder<'a> { } fn push_separator(&mut self) { - self.slide_operations.extend([RenderOperation::RenderSeparator, RenderOperation::RenderLineBreak]); + self.slide_operations.extend([RenderSeparator::default().into(), RenderOperation::RenderLineBreak]); } fn push_image(&mut self, path: PathBuf) -> Result<(), BuildError> { @@ -760,13 +760,10 @@ impl AsRenderOperations for RunCodeOperation { _ => "running", }; let heading = format!(" [{state}] "); - // TODO remove `RenderSeparator` and turn it into a dynamic operation with optional heading - let dashes_len = (dimensions.columns as usize).saturating_sub(heading.len()) / 2; - let dashes = "—".repeat(dashes_len); - let separator = format!("{dashes}{heading}{dashes}"); + let separator = RenderSeparator::new(heading); let mut operations = vec![ RenderOperation::RenderLineBreak, - self.render_line(separator), + RenderOperation::RenderDynamic(Rc::new(separator)), RenderOperation::RenderLineBreak, RenderOperation::RenderLineBreak, RenderOperation::SetColors(self.block_colors.clone()), @@ -822,6 +819,39 @@ impl RenderOnDemand for RunCodeOperation { } } +#[derive(Clone, Debug, Default)] +struct RenderSeparator { + heading: String, +} + +impl RenderSeparator { + fn new>(heading: S) -> Self { + Self { heading: heading.into() } + } +} + +impl From for RenderOperation { + fn from(separator: RenderSeparator) -> Self { + Self::RenderDynamic(Rc::new(separator)) + } +} + +impl AsRenderOperations for RenderSeparator { + fn as_render_operations(&self, dimensions: &WindowSize) -> Vec { + let character = "—"; + let separator = match self.heading.is_empty() { + true => character.repeat(dimensions.columns as usize), + false => { + let dashes_len = (dimensions.columns as usize).saturating_sub(self.heading.len()) / 2; + let dashes = character.repeat(dashes_len); + let heading = &self.heading; + format!("{dashes}{heading}{dashes}") + } + }; + vec![RenderOperation::RenderTextLine { line: separator.into(), alignment: Default::default() }] + } +} + #[cfg(test)] mod test { use rstest::rstest; @@ -870,7 +900,6 @@ mod test { | ApplyMargin(_) | PopMargin => false, RenderTextLine { .. } - | RenderSeparator | RenderLineBreak | RenderImage(_) | RenderPreformattedLine(_) diff --git a/src/diff.rs b/src/diff.rs index 4709ee18..042241a1 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -110,7 +110,6 @@ mod test { #[case(RenderOperation::ClearScreen)] #[case(RenderOperation::JumpToVerticalCenter)] #[case(RenderOperation::JumpToBottom)] - #[case(RenderOperation::RenderSeparator)] #[case(RenderOperation::RenderLineBreak)] #[case(RenderOperation::SetColors(Colors{background: None, foreground: None}))] #[case(RenderOperation::RenderTextLine{line: String::from("asd").into(), alignment: Default::default()})] diff --git a/src/presentation.rs b/src/presentation.rs index 48da20bf..b2098518 100644 --- a/src/presentation.rs +++ b/src/presentation.rs @@ -194,9 +194,6 @@ pub(crate) enum RenderOperation { /// Render a line of text. RenderTextLine { line: WeightedLine, alignment: Alignment }, - /// Render a horizontal separator line. - RenderSeparator, - /// Render a line break. RenderLineBreak, diff --git a/src/render/operator.rs b/src/render/operator.rs index 3c912383..05752fc4 100644 --- a/src/render/operator.rs +++ b/src/render/operator.rs @@ -53,7 +53,6 @@ where RenderOperation::JumpToVerticalCenter => self.jump_to_vertical_center(), RenderOperation::JumpToBottom => self.jump_to_bottom(), RenderOperation::RenderTextLine { line: texts, alignment } => self.render_text(texts, alignment), - RenderOperation::RenderSeparator => self.render_separator(), RenderOperation::RenderLineBreak => self.render_line_break(), RenderOperation::RenderImage(image) => self.render_image(image), RenderOperation::RenderPreformattedLine(operation) => self.render_preformatted_line(operation), @@ -127,13 +126,6 @@ where text_drawer.draw(self.terminal) } - fn render_separator(&mut self) -> RenderResult { - let separator: String = "—".repeat(self.current_dimensions().columns as usize); - self.terminal.move_to_column(self.current_rect().start_column)?; - self.terminal.print_line(&separator)?; - Ok(()) - } - fn render_line_break(&mut self) -> RenderResult { self.terminal.move_to_next_line(1)?; Ok(())