From 45f8a670764043e4df185a99ad4d786a55ef119d Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 23 Aug 2023 23:24:22 -0700 Subject: [PATCH] chore(spans): remove deprecated `Spans` type - `Line` replaces `Spans` - `Buffer::set_line` replaces `Buffer::set_spans` --- src/buffer.rs | 26 +---- src/text.rs | 5 - src/text/line.rs | 20 +--- src/text/spans.rs | 223 ------------------------------------- src/text/text.rs | 51 +-------- tests/widgets_list.rs | 2 - tests/widgets_paragraph.rs | 2 - tests/widgets_tabs.rs | 2 - 8 files changed, 4 insertions(+), 327 deletions(-) delete mode 100644 src/text/spans.rs diff --git a/src/buffer.rs b/src/buffer.rs index 210345432..fa86fba09 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -6,11 +6,10 @@ use std::{ use unicode_segmentation::UnicodeSegmentation; use unicode_width::UnicodeWidthStr; -#[allow(deprecated)] use crate::{ layout::Rect, style::{Color, Modifier, Style}, - text::{Line, Span, Spans}, + text::{Line, Span}, }; /// A buffer cell @@ -334,29 +333,6 @@ impl Buffer { (x_offset as u16, y) } - #[allow(deprecated)] - #[deprecated(note = "Use `Buffer::set_line` instead")] - pub fn set_spans(&mut self, x: u16, y: u16, spans: &Spans<'_>, width: u16) -> (u16, u16) { - let mut remaining_width = width; - let mut x = x; - for span in &spans.0 { - if remaining_width == 0 { - break; - } - let pos = self.set_stringn( - x, - y, - span.content.as_ref(), - remaining_width as usize, - span.style, - ); - let w = pos.0.saturating_sub(x); - x = pos.0; - remaining_width = remaining_width.saturating_sub(w); - } - (x, y) - } - pub fn set_line(&mut self, x: u16, y: u16, line: &Line<'_>, width: u16) -> (u16, u16) { let mut remaining_width = width; let mut x = x; diff --git a/src/text.rs b/src/text.rs index 59fd512b8..941877157 100644 --- a/src/text.rs +++ b/src/text.rs @@ -60,11 +60,6 @@ pub use masked::Masked; mod span; pub use span::Span; -/// We keep this for backward compatibility. -mod spans; -#[allow(deprecated)] -pub use spans::Spans; - #[allow(clippy::module_inception)] mod text; pub use text::Text; diff --git a/src/text/line.rs b/src/text/line.rs index 841095800..1dba1a5c1 100644 --- a/src/text/line.rs +++ b/src/text/line.rs @@ -1,7 +1,6 @@ -#![allow(deprecated)] use std::borrow::Cow; -use super::{Span, Spans, Style, StyledGrapheme}; +use super::{Span, Style, StyledGrapheme}; use crate::layout::Alignment; #[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] @@ -180,18 +179,12 @@ impl<'a> From> for String { } } -impl<'a> From> for Line<'a> { - fn from(value: Spans<'a>) -> Self { - Self::from(value.0) - } -} - #[cfg(test)] mod tests { use crate::{ layout::Alignment, style::{Color, Modifier, Style}, - text::{Line, Span, Spans, StyledGrapheme}, + text::{Line, Span, StyledGrapheme}, }; #[test] @@ -266,15 +259,6 @@ mod tests { assert_eq!(vec![span], line.spans); } - #[test] - fn test_from_spans() { - let spans = vec![ - Span::styled("Hello,", Style::default().fg(Color::Red)), - Span::styled(" world!", Style::default().fg(Color::Green)), - ]; - assert_eq!(Line::from(Spans::from(spans.clone())), Line::from(spans)); - } - #[test] fn test_into_string() { let line = Line::from(vec![ diff --git a/src/text/spans.rs b/src/text/spans.rs deleted file mode 100644 index 1e0b4dd75..000000000 --- a/src/text/spans.rs +++ /dev/null @@ -1,223 +0,0 @@ -#![allow(deprecated)] - -use super::{Span, Style}; -use crate::{layout::Alignment, text::Line}; - -/// A string composed of clusters of graphemes, each with their own style. -/// -/// `Spans` has been deprecated in favor of `Line`, and will be removed in the -/// future. All methods that accept Spans have been replaced with methods that -/// accept Into> (which is implemented on `Spans`) to allow users of -/// this crate to gradually transition to Line. -#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)] -#[deprecated(note = "Use `ratatui::text::Line` instead")] -pub struct Spans<'a>(pub Vec>); - -impl<'a> Spans<'a> { - /// Returns the width of the underlying string. - /// - /// ## Examples - /// - /// ```rust - /// use ratatui::{prelude::*, text::Spans}; - /// - /// let spans = Spans::from(vec![ - /// Span::styled("My", Style::default().fg(Color::Yellow)), - /// Span::raw(" text"), - /// ]); - /// assert_eq!(7, spans.width()); - /// ``` - pub fn width(&self) -> usize { - self.0.iter().map(Span::width).sum() - } - - /// Patches the style of each Span in an existing Spans, adding modifiers from the given style. - /// - /// ## Examples - /// - /// ```rust - /// use ratatui::{prelude::*, text::Spans}; - /// - /// let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC); - /// let mut raw_spans = Spans::from(vec![ - /// Span::raw("My"), - /// Span::raw(" text"), - /// ]); - /// let mut styled_spans = Spans::from(vec![ - /// Span::styled("My", style), - /// Span::styled(" text", style), - /// ]); - /// - /// assert_ne!(raw_spans, styled_spans); - /// - /// raw_spans.patch_style(style); - /// assert_eq!(raw_spans, styled_spans); - /// ``` - pub fn patch_style(&mut self, style: Style) { - for span in &mut self.0 { - span.patch_style(style); - } - } - - /// Resets the style of each Span in the Spans. - /// Equivalent to calling `patch_style(Style::reset())`. - /// - /// ## Examples - /// - /// ```rust - /// use ratatui::{prelude::*, text::Spans}; - /// - /// let mut spans = Spans::from(vec![ - /// Span::styled("My", Style::default().fg(Color::Yellow)), - /// Span::styled(" text", Style::default().add_modifier(Modifier::BOLD)), - /// ]); - /// - /// spans.reset_style(); - /// assert_eq!(Style::reset(), spans.0[0].style); - /// assert_eq!(Style::reset(), spans.0[1].style); - /// ``` - pub fn reset_style(&mut self) { - for span in &mut self.0 { - span.reset_style(); - } - } - - /// Sets the target alignment for this line of text. - /// Defaults to: [`None`], meaning the alignment is determined by the rendering widget. - /// - /// ## Examples - /// - /// ```rust - /// use ratatui::{prelude::*, text::Spans}; - /// - /// let mut line = Spans::from("Hi, what's up?").alignment(Alignment::Right); - /// assert_eq!(Some(Alignment::Right), line.alignment) - /// ``` - pub fn alignment(self, alignment: Alignment) -> Line<'a> { - let line = Line::from(self); - line.alignment(alignment) - } -} - -impl<'a> From for Spans<'a> { - fn from(s: String) -> Spans<'a> { - Spans(vec![Span::from(s)]) - } -} - -impl<'a> From<&'a str> for Spans<'a> { - fn from(s: &'a str) -> Spans<'a> { - Spans(vec![Span::from(s)]) - } -} - -impl<'a> From>> for Spans<'a> { - fn from(spans: Vec>) -> Spans<'a> { - Spans(spans) - } -} - -impl<'a> From> for Spans<'a> { - fn from(span: Span<'a>) -> Spans<'a> { - Spans(vec![span]) - } -} - -impl<'a> From> for String { - fn from(line: Spans<'a>) -> String { - line.0.iter().fold(String::new(), |mut acc, s| { - acc.push_str(s.content.as_ref()); - acc - }) - } -} - -#[cfg(test)] -mod tests { - use crate::{ - style::{Color, Modifier, Style}, - text::{Span, Spans}, - }; - - #[test] - fn test_width() { - let spans = Spans::from(vec![ - Span::styled("My", Style::default().fg(Color::Yellow)), - Span::raw(" text"), - ]); - assert_eq!(7, spans.width()); - - let empty_spans = Spans::default(); - assert_eq!(0, empty_spans.width()); - } - - #[test] - fn test_patch_style() { - let style = Style::default() - .fg(Color::Yellow) - .add_modifier(Modifier::ITALIC); - let mut raw_spans = Spans::from(vec![Span::raw("My"), Span::raw(" text")]); - let styled_spans = Spans::from(vec![ - Span::styled("My", style), - Span::styled(" text", style), - ]); - - assert_ne!(raw_spans, styled_spans); - - raw_spans.patch_style(style); - assert_eq!(raw_spans, styled_spans); - } - - #[test] - fn test_reset_style() { - let mut spans = Spans::from(vec![ - Span::styled("My", Style::default().fg(Color::Yellow)), - Span::styled(" text", Style::default().add_modifier(Modifier::BOLD)), - ]); - - spans.reset_style(); - assert_eq!(Style::reset(), spans.0[0].style); - assert_eq!(Style::reset(), spans.0[1].style); - } - - #[test] - fn test_from_string() { - let s = String::from("Hello, world!"); - let spans = Spans::from(s); - assert_eq!(vec![Span::from("Hello, world!")], spans.0); - } - - #[test] - fn test_from_str() { - let s = "Hello, world!"; - let spans = Spans::from(s); - assert_eq!(vec![Span::from("Hello, world!")], spans.0); - } - - #[test] - fn test_from_vec() { - let spans_vec = vec![ - Span::styled("Hello,", Style::default().fg(Color::Red)), - Span::styled(" world!", Style::default().fg(Color::Green)), - ]; - let spans = Spans::from(spans_vec.clone()); - assert_eq!(spans_vec, spans.0); - } - - #[test] - fn test_from_span() { - let span = Span::styled("Hello, world!", Style::default().fg(Color::Yellow)); - let spans = Spans::from(span.clone()); - assert_eq!(vec![span], spans.0); - } - - #[test] - fn test_into_string() { - let spans = Spans::from(vec![ - Span::styled("Hello,", Style::default().fg(Color::Red)), - Span::styled(" world!", Style::default().fg(Color::Green)), - ]); - let s: String = spans.into(); - assert_eq!("Hello, world!", s); - } -} diff --git a/src/text/text.rs b/src/text/text.rs index 76c7a0ac7..ede3f911a 100644 --- a/src/text/text.rs +++ b/src/text/text.rs @@ -1,7 +1,6 @@ use std::borrow::Cow; -#[allow(deprecated)] -use super::{Line, Span, Spans}; +use super::{Line, Span}; use crate::style::Style; /// A string split over multiple lines where each line is composed of several clusters, each with @@ -172,30 +171,12 @@ impl<'a> From> for Text<'a> { } } -#[allow(deprecated)] -impl<'a> From> for Text<'a> { - fn from(spans: Spans<'a>) -> Text<'a> { - Text { - lines: vec![spans.into()], - } - } -} - impl<'a> From> for Text<'a> { fn from(line: Line<'a>) -> Text<'a> { Text { lines: vec![line] } } } -#[allow(deprecated)] -impl<'a> From>> for Text<'a> { - fn from(lines: Vec>) -> Text<'a> { - Text { - lines: lines.into_iter().map(|l| l.0.into()).collect(), - } - } -} - impl<'a> From>> for Text<'a> { fn from(lines: Vec>) -> Text<'a> { Text { lines } @@ -332,42 +313,12 @@ mod tests { ); } - #[test] - #[allow(deprecated)] - fn from_spans() { - let style = Style::new().yellow().italic(); - let text = Text::from(Spans::from(vec![ - Span::styled("The first line", style), - Span::styled("The second line", style), - ])); - assert_eq!( - text.lines, - vec![Line::from(Spans::from(vec![ - Span::styled("The first line", style), - Span::styled("The second line", style), - ]))] - ); - } - #[test] fn from_line() { let text = Text::from(Line::from("The first line")); assert_eq!(text.lines, vec![Line::from("The first line")]); } - #[test] - #[allow(deprecated)] - fn from_vec_spans() { - let text = Text::from(vec![ - Spans::from("The first line"), - Spans::from("The second line"), - ]); - assert_eq!( - text.lines, - vec![Line::from("The first line"), Line::from("The second line"),] - ); - } - #[test] fn from_vec_line() { let text = Text::from(vec![ diff --git a/tests/widgets_list.rs b/tests/widgets_list.rs index 2224c7993..c918adef0 100644 --- a/tests/widgets_list.rs +++ b/tests/widgets_list.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - use ratatui::{ backend::TestBackend, buffer::Buffer, diff --git a/tests/widgets_paragraph.rs b/tests/widgets_paragraph.rs index 12da85f41..b553da3f0 100644 --- a/tests/widgets_paragraph.rs +++ b/tests/widgets_paragraph.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - use ratatui::{ backend::TestBackend, buffer::Buffer, diff --git a/tests/widgets_tabs.rs b/tests/widgets_tabs.rs index d3fcfae3a..414bba911 100644 --- a/tests/widgets_tabs.rs +++ b/tests/widgets_tabs.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - use ratatui::{ backend::TestBackend, buffer::Buffer, layout::Rect, symbols, text::Line, widgets::Tabs, Terminal,