Skip to content

Commit

Permalink
refactor: define struct WrappedLine instead of anonymous tuple (#608)
Browse files Browse the repository at this point in the history
It makes the type easier to document, and more obvious for users
  • Loading branch information
progval committed Dec 6, 2023
1 parent dd22e72 commit 7ced7c0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
style::{Style, Styled},
text::{StyledGrapheme, Text},
widgets::{
reflow::{LineComposer, LineTruncator, WordWrapper},
reflow::{LineComposer, LineTruncator, WordWrapper, WrappedLine},
Block, Widget,
},
};
Expand Down Expand Up @@ -254,8 +254,11 @@ impl<'a> Widget for Paragraph<'a> {
impl<'a> Paragraph<'a> {
fn render_text<C: LineComposer<'a>>(&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);
Expand Down
38 changes: 28 additions & 10 deletions src/widgets/reflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WrappedLine<'lend, 'a>>;
}

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.
Expand Down Expand Up @@ -56,7 +65,7 @@ where
O: Iterator<Item = (I, Alignment)>,
I: Iterator<Item = StyledGrapheme<'a>>,
{
fn next_line(&mut self) -> Option<(&[StyledGrapheme<'a>], u16, Alignment)> {
fn next_line<'lend>(&'lend mut self) -> Option<WrappedLine<'lend, 'a>> {
if self.max_line_width == 0 {
return None;
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -249,7 +262,7 @@ where
O: Iterator<Item = (I, Alignment)>,
I: Iterator<Item = StyledGrapheme<'a>>,
{
fn next_line(&mut self) -> Option<(&[StyledGrapheme<'a>], u16, Alignment)> {
fn next_line<'lend>(&'lend mut self) -> Option<WrappedLine<'lend, 'a>> {
if self.max_line_width == 0 {
return None;
}
Expand Down Expand Up @@ -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,
})
}
}
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7ced7c0

Please sign in to comment.