diff --git a/src/widgets/paragraph.rs b/src/widgets/paragraph.rs index 445860f6..bf7c2449 100644 --- a/src/widgets/paragraph.rs +++ b/src/widgets/paragraph.rs @@ -6,7 +6,7 @@ use crate::{ style::{Style, Styled}, text::{StyledGrapheme, Text}, widgets::{ - reflow::{LineComposer, LineTruncator, WordWrapper}, + reflow::{LineComposer, LineTruncator, WordWrapper, WrappedLine}, Block, Widget, }, }; @@ -254,8 +254,11 @@ impl<'a> Widget for Paragraph<'a> { impl<'a> Paragraph<'a> { fn render_text>(&self, mut composer: C, area: Rect, buf: &mut Buffer) { let mut y = 0; - while let Some((current_line, current_line_width, current_line_alignment)) = - composer.next_line() + while let Some(WrappedLine { + line: current_line, + width: current_line_width, + alignment: current_line_alignment, + }) = composer.next_line() { if y >= self.scroll.0 { let mut x = get_line_offset(current_line_width, area.width, current_line_alignment); diff --git a/src/widgets/reflow.rs b/src/widgets/reflow.rs index e08747e4..036f66b5 100644 --- a/src/widgets/reflow.rs +++ b/src/widgets/reflow.rs @@ -11,7 +11,16 @@ const NBSP: &str = "\u{00a0}"; /// Cannot implement it as Iterator since it yields slices of the internal buffer (need streaming /// iterators for that). pub trait LineComposer<'a> { - fn next_line(&mut self) -> Option<(&[StyledGrapheme<'a>], u16, Alignment)>; + fn next_line<'lend>(&'lend mut self) -> Option>; +} + +pub struct WrappedLine<'lend, 'text> { + /// One line reflowed to the correct width + pub line: &'lend [StyledGrapheme<'text>], + /// The width of the line + pub width: u16, + /// Whether the line was aligned left or right + pub alignment: Alignment, } /// A state machine that wraps lines on word boundaries. @@ -56,7 +65,7 @@ where O: Iterator, I: Iterator>, { - fn next_line(&mut self) -> Option<(&[StyledGrapheme<'a>], u16, Alignment)> { + fn next_line<'lend>(&'lend mut self) -> Option> { if self.max_line_width == 0 { return None; } @@ -200,7 +209,11 @@ where if let Some(line) = current_line { self.current_line = line; - Some((&self.current_line[..], line_width, self.current_alignment)) + Some(WrappedLine { + line: &self.current_line[..], + width: line_width, + alignment: self.current_alignment, + }) } else { None } @@ -249,7 +262,7 @@ where O: Iterator, I: Iterator>, { - fn next_line(&mut self) -> Option<(&[StyledGrapheme<'a>], u16, Alignment)> { + fn next_line<'lend>(&'lend mut self) -> Option> { if self.max_line_width == 0 { return None; } @@ -296,11 +309,11 @@ where if lines_exhausted { None } else { - Some(( - &self.current_line[..], - current_line_width, - current_alignment, - )) + Some(WrappedLine { + line: &self.current_line[..], + width: current_line_width, + alignment: current_alignment, + }) } } } @@ -360,7 +373,12 @@ mod test { let mut lines = vec![]; let mut widths = vec![]; let mut alignments = vec![]; - while let Some((styled, width, alignment)) = composer.next_line() { + while let Some(WrappedLine { + line: styled, + width, + alignment, + }) = composer.next_line() + { let line = styled .iter() .map(|StyledGrapheme { symbol, .. }| *symbol)