Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: visitor method to process paragraph wrapped text #956

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rename
  • Loading branch information
december1981 committed Feb 17, 2024
commit 4f3fa75a8176aa69f6d793c4a8b32d04d39945c3
10 changes: 5 additions & 5 deletions src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl Paragraph<'_> {
/// Visits the styled composed lines inside a text area for rendering or other analysis/processing.
/// The visitor function indicates it wants the visitor iteration to terminate if it returns false.
///
pub fn visit_composed<F: FnMut(&'_ WrappedLine) -> bool>(&self, text_area: &Rect, visitor: F) {
pub fn visit_wrapped_text<F: FnMut(&'_ WrappedLine) -> bool>(&self, text_area: &Rect, visitor: F) {
if text_area.is_empty() {
return;
}
Expand All @@ -353,15 +353,15 @@ impl Paragraph<'_> {

if let Some(Wrap { trim }) = self.wrap {
let line_composer = WordWrapper::new(styled, text_area.width, trim);
Self::visit_composed_lines(line_composer, visitor);
Self::visit_with_composer(line_composer, visitor);
} else {
let mut line_composer = LineTruncator::new(styled, text_area.width);
line_composer.set_horizontal_offset(self.scroll.1);
Self::visit_composed_lines(line_composer, visitor);
Self::visit_with_composer(line_composer, visitor);
}
}

fn visit_composed_lines<'a, C: LineComposer<'a>, F: FnMut(&'_ WrappedLine) -> bool>(mut composer: C, mut visitor: F) {
fn visit_with_composer<'a, C: LineComposer<'a>, F: FnMut(&'_ WrappedLine) -> bool>(mut composer: C, mut visitor: F) {
while let Some(line) = composer.next_line() {
if !visitor(&line) {
break;
Expand All @@ -371,7 +371,7 @@ impl Paragraph<'_> {

fn render_paragraph(&self, area: Rect, buf: &mut Buffer) {
let mut y = 0;
self.visit_composed(&area, |wrapped| {
self.visit_wrapped_text(&area, |wrapped| {
if y >= self.scroll.0 {
let mut x = Self::get_line_offset(wrapped.width, area.width, wrapped.alignment);
for StyledGrapheme { symbol, style } in wrapped.line {
Expand Down