From 778bec70306f6793ab6d9356bf924d712e357d56 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 17 Jun 2024 12:37:59 -0400 Subject: [PATCH] Add SelectView::add_item_styled --- cursive-core/src/views/select_view.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cursive-core/src/views/select_view.rs b/cursive-core/src/views/select_view.rs index 7c5886c9..b143d437 100644 --- a/cursive-core/src/views/select_view.rs +++ b/cursive-core/src/views/select_view.rs @@ -838,7 +838,20 @@ impl SelectView { self.add_item(label.clone(), label); } - /// Chainable variant of add_item_str + /// Convenient method to use the label unstyled text as value. + pub fn add_item_styled>(&mut self, label: S) { + let label = label.into(); + + // Accumulate the content of each span. + let mut content = String::new(); + for span in label.spans() { + content.push_str(span.content); + } + + self.add_item(label, content); + } + + /// Chainable variant of `add_item_str`. /// /// # Examples /// @@ -855,6 +868,12 @@ impl SelectView { self.with(|s| s.add_item_str(label)) } + /// Chainable variant of `add_item_styled`. + #[must_use] + pub fn item_styled>(self, label: S) -> Self { + self.with(|s| s.add_item_styled(label)) + } + /// Convenient method to use the label as value. pub fn insert_item_str(&mut self, index: usize, label: S) where