Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-tui/src/widgets/paragraph.rs')
| -rw-r--r-- | helix-tui/src/widgets/paragraph.rs | 55 |
1 files changed, 13 insertions, 42 deletions
diff --git a/helix-tui/src/widgets/paragraph.rs b/helix-tui/src/widgets/paragraph.rs index 2b4ccfbd..4e839162 100644 --- a/helix-tui/src/widgets/paragraph.rs +++ b/helix-tui/src/widgets/paragraph.rs @@ -28,16 +28,16 @@ fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment) /// # use helix_tui::widgets::{Block, Borders, Paragraph, Wrap}; /// # use helix_tui::layout::{Alignment}; /// # use helix_view::graphics::{Style, Color, Modifier}; -/// let text = Text::from(vec![ +/// let text = vec![ /// Spans::from(vec![ /// Span::raw("First"), /// Span::styled("line",Style::default().add_modifier(Modifier::ITALIC)), /// Span::raw("."), /// ]), /// Spans::from(Span::styled("Second line", Style::default().fg(Color::Red))), -/// ]); -/// Paragraph::new(&text) -/// .block(Block::bordered().title("Paragraph")) +/// ]; +/// Paragraph::new(text) +/// .block(Block::default().title("Paragraph").borders(Borders::ALL)) /// .style(Style::default().fg(Color::White).bg(Color::Black)) /// .alignment(Alignment::Center) /// .wrap(Wrap { trim: true }); @@ -51,7 +51,7 @@ pub struct Paragraph<'a> { /// How to wrap the text wrap: Option<Wrap>, /// The text to display - text: &'a Text<'a>, + text: Text<'a>, /// Scroll scroll: (u16, u16), /// Alignment of the text @@ -70,7 +70,7 @@ pub struct Paragraph<'a> { /// - Here is another point that is long enough to wrap"#); /// /// // With leading spaces trimmed (window width of 30 chars): -/// Paragraph::new(&bullet_points).wrap(Wrap { trim: true }); +/// Paragraph::new(bullet_points.clone()).wrap(Wrap { trim: true }); /// // Some indented points: /// // - First thing goes here and is /// // long so that it wraps @@ -78,7 +78,7 @@ pub struct Paragraph<'a> { /// // is long enough to wrap /// /// // But without trimming, indentation is preserved: -/// Paragraph::new(&bullet_points).wrap(Wrap { trim: false }); +/// Paragraph::new(bullet_points).wrap(Wrap { trim: false }); /// // Some indented points: /// // - First thing goes here /// // and is long so that it wraps @@ -92,12 +92,15 @@ pub struct Wrap { } impl<'a> Paragraph<'a> { - pub fn new(text: &'a Text) -> Paragraph<'a> { + pub fn new<T>(text: T) -> Paragraph<'a> + where + T: Into<Text<'a>>, + { Paragraph { block: None, style: Default::default(), wrap: None, - text, + text: text.into(), scroll: (0, 0), alignment: Alignment::Left, } @@ -127,41 +130,9 @@ impl<'a> Paragraph<'a> { self.alignment = alignment; self } - - pub fn required_size(&self, max_text_width: u16) -> (u16, u16) { - let style = self.style; - let mut styled = self.text.lines.iter().flat_map(|spans| { - spans - .0 - .iter() - .flat_map(|span| span.styled_graphemes(style)) - // Required given the way composers work but might be refactored out if we change - // composers to operate on lines instead of a stream of graphemes. - .chain(iter::once(StyledGrapheme { - symbol: "\n", - style: self.style, - })) - }); - let mut line_composer: Box<dyn LineComposer> = if let Some(Wrap { trim }) = self.wrap { - Box::new(WordWrapper::new(&mut styled, max_text_width, trim)) - } else { - let mut line_composer = Box::new(LineTruncator::new(&mut styled, max_text_width)); - if self.alignment == Alignment::Left { - line_composer.set_horizontal_offset(self.scroll.1); - } - line_composer - }; - let mut text_width = 0; - let mut text_height = 0; - while let Some((_, line_width)) = line_composer.next_line() { - text_width = line_width.max(text_width); - text_height += 1; - } - (text_width, text_height) - } } -impl Widget for Paragraph<'_> { +impl<'a> Widget for Paragraph<'a> { fn render(mut self, area: Rect, buf: &mut Buffer) { buf.set_style(area, self.style); let text_area = match self.block.take() { |