Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-tui/src/text.rs')
| -rw-r--r-- | helix-tui/src/text.rs | 64 |
1 files changed, 6 insertions, 58 deletions
diff --git a/helix-tui/src/text.rs b/helix-tui/src/text.rs index a9c36503..ccdafad5 100644 --- a/helix-tui/src/text.rs +++ b/helix-tui/src/text.rs @@ -5,12 +5,12 @@ //! - A single line string where all graphemes have the same style is represented by a [`Span`]. //! - A single line string where each grapheme may have its own style is represented by [`Spans`]. //! - A multiple line string where each grapheme may have its own style is represented by a -//! [`Text`]. +//! [`Text`]. //! //! These types form a hierarchy: [`Spans`] is a collection of [`Span`] and each line of [`Text`] //! is a [`Spans`]. //! -//! Keep in mind that a lot of widgets will use those types to advertise what kind of string is +//! Keep it mind that a lot of widgets will use those types to advertise what kind of string is //! supported for their properties. Moreover, `tui` provides convenient `From` implementations so //! that you can start by using simple `String` or `&str` and then promote them to the previous //! primitives when you need additional styling capabilities. @@ -212,7 +212,7 @@ impl<'a> From<Cow<'a, str>> for Span<'a> { #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Spans<'a>(pub Vec<Span<'a>>); -impl Spans<'_> { +impl<'a> Spans<'a> { /// Returns the width of the underlying string. /// /// ## Examples @@ -374,30 +374,6 @@ impl<'a> Text<'a> { self.lines.len() } - /// Patch text with a new style. Only updates fields that are in the new style. - /// - /// # Examples - /// - /// ```rust - /// # use helix_tui::text::Text; - /// # use helix_view::graphics::{Color, Style}; - /// let style1 = Style::default().fg(Color::Yellow); - /// let style2 = Style::default().fg(Color::Yellow).bg(Color::Black); - /// let mut half_styled_text = Text::styled(String::from("The first line\nThe second line"), style1); - /// let full_styled_text = Text::styled(String::from("The first line\nThe second line"), style2); - /// assert_ne!(half_styled_text, full_styled_text); - /// - /// half_styled_text.patch_style(Style::default().bg(Color::Black)); - /// assert_eq!(half_styled_text, full_styled_text); - /// ``` - pub fn patch_style(&mut self, style: Style) { - for line in &mut self.lines { - for span in &mut line.0 { - span.style = span.style.patch(style); - } - } - } - /// Apply a new style to existing text. /// /// # Examples @@ -410,13 +386,13 @@ impl<'a> Text<'a> { /// let styled_text = Text::styled(String::from("The first line\nThe second line"), style); /// assert_ne!(raw_text, styled_text); /// - /// raw_text.set_style(style); + /// raw_text.patch_style(style); /// assert_eq!(raw_text, styled_text); /// ``` - pub fn set_style(&mut self, style: Style) { + pub fn patch_style(&mut self, style: Style) { for line in &mut self.lines { for span in &mut line.0 { - span.style = style; + span.style = span.style.patch(style); } } } @@ -460,34 +436,6 @@ impl<'a> From<Vec<Spans<'a>>> for Text<'a> { } } -impl<'a> From<Text<'a>> for String { - fn from(text: Text<'a>) -> String { - String::from(&text) - } -} - -impl<'a> From<&Text<'a>> for String { - fn from(text: &Text<'a>) -> String { - let size = text - .lines - .iter() - .flat_map(|spans| spans.0.iter().map(|span| span.content.len())) - .sum::<usize>() - + text.lines.len().saturating_sub(1); // for newline after each line - let mut output = String::with_capacity(size); - - for spans in &text.lines { - if !output.is_empty() { - output.push('\n'); - } - for span in &spans.0 { - output.push_str(&span.content); - } - } - output - } -} - impl<'a> IntoIterator for Text<'a> { type Item = Spans<'a>; type IntoIter = std::vec::IntoIter<Self::Item>; |