From 0e5410eb36aca4c1ce1430ce81923f3b8ab03865 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Apr 2025 03:01:10 +0800 Subject: [PATCH] add more convenience apis --- libs/ux-api/src/widgets/scroll.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/libs/ux-api/src/widgets/scroll.rs b/libs/ux-api/src/widgets/scroll.rs index d9c4b9f8a..9b2e1823a 100644 --- a/libs/ux-api/src/widgets/scroll.rs +++ b/libs/ux-api/src/widgets/scroll.rs @@ -214,6 +214,18 @@ impl ScrollableList { } } + pub fn delete_selected(&mut self) -> String { + if self.items[self.select_index.0].len() > self.select_index.1 { + let removed = self.items[self.select_index.0].remove(self.select_index.1); + if self.select_index.1 >= self.items[self.select_index.0].len() { + self.select_index.1 = self.items.len().saturating_sub(1); + } + removed + } else { + "".to_owned() + } + } + /// Set the scroll offset. The selected element is the start of list rendering, and /// it designates the top left element on the screen. /// @@ -298,6 +310,23 @@ impl ScrollableList { self.items[self.select_index.0][self.select_index.1] = contents.to_owned(); } + /// Replaces every instance of a `original` string with `replacement` string in a given column + /// Returns the number of instances replaced. + pub fn replace_with(&mut self, column: usize, original: &str, replacement: &str) -> usize { + if column >= self.items.len() { + 0 + } else { + let mut count = 0; + for item in self.items[column].iter_mut() { + if item == original { + *item = replacement.to_string(); + count += 1; + } + } + count + } + } + /// Returns all the items as a nested iterator. pub fn get_all(&self) -> impl Iterator> { self.items.iter().map(|inner_vec| inner_vec.iter().map(|s| s.as_str()))