Skip to content

Commit

Permalink
Make RenderSeparator a RenderDynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini committed Oct 21, 2023
1 parent 50a8548 commit 37473db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
45 changes: 37 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -822,6 +819,39 @@ impl RenderOnDemand for RunCodeOperation {
}
}

#[derive(Clone, Debug, Default)]
struct RenderSeparator {
heading: String,
}

impl RenderSeparator {
fn new<S: Into<String>>(heading: S) -> Self {
Self { heading: heading.into() }
}
}

impl From<RenderSeparator> for RenderOperation {
fn from(separator: RenderSeparator) -> Self {
Self::RenderDynamic(Rc::new(separator))
}
}

impl AsRenderOperations for RenderSeparator {
fn as_render_operations(&self, dimensions: &WindowSize) -> Vec<RenderOperation> {
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;
Expand Down Expand Up @@ -870,7 +900,6 @@ mod test {
| ApplyMargin(_)
| PopMargin => false,
RenderTextLine { .. }
| RenderSeparator
| RenderLineBreak
| RenderImage(_)
| RenderPreformattedLine(_)
Expand Down
1 change: 0 additions & 1 deletion src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()})]
Expand Down
3 changes: 0 additions & 3 deletions src/presentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
8 changes: 0 additions & 8 deletions src/render/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit 37473db

Please sign in to comment.