From 86a5cc1a85195e8b90b2e5283e5e524e2e5fa466 Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Sat, 17 Apr 2021 13:25:12 -0400 Subject: [PATCH 1/2] feat(Text): Add a From> impl for Text --- src/text.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/text.rs b/src/text.rs index eca59afc..70a71d0c 100644 --- a/src/text.rs +++ b/src/text.rs @@ -398,6 +398,12 @@ impl<'a> From<&'a str> for Text<'a> { } } +impl<'a> From> for Text<'a> { + fn from(s: Cow<'a, str>) -> Text<'a> { + Text::raw(s) + } +} + impl<'a> From> for Text<'a> { fn from(span: Span<'a>) -> Text<'a> { Text { From a9972343bd9e56db6431373b442a354a82d0b42a Mon Sep 17 00:00:00 2001 From: Andrew Chin Date: Wed, 5 May 2021 00:03:03 -0400 Subject: [PATCH 2/2] doc: Add doctests that shows how Text can be created from Cow --- src/widgets/table.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/widgets/table.rs b/src/widgets/table.rs index 261bc186..04b8437d 100644 --- a/src/widgets/table.rs +++ b/src/widgets/table.rs @@ -23,6 +23,7 @@ use unicode_width::UnicodeWidthStr; /// # use tui::widgets::Cell; /// # use tui::style::{Style, Modifier}; /// # use tui::text::{Span, Spans, Text}; +/// # use std::borrow::Cow; /// Cell::from("simple string"); /// /// Cell::from(Span::from("span")); @@ -33,6 +34,8 @@ use unicode_width::UnicodeWidthStr; /// ])); /// /// Cell::from(Text::from("a text")); +/// +/// Cell::from(Text::from(Cow::Borrowed("hello"))); /// ``` /// /// You can apply a [`Style`] on the entire [`Cell`] using [`Cell::style`] or rely on the styling @@ -81,6 +84,16 @@ where /// ]); /// ``` /// +/// You can also construct a row from any type that can be converted into [`Text`]: +/// ```rust +/// # use std::borrow::Cow; +/// # use tui::widgets::Row; +/// Row::new(vec![ +/// Cow::Borrowed("hello"), +/// Cow::Owned("world".to_uppercase()), +/// ]); +/// ``` +/// /// By default, a row has a height of 1 but you can change this using [`Row::height`]. #[derive(Debug, Clone, PartialEq, Default)] pub struct Row<'a> {