From 39af7fa202109c3fdf3b040642ac220df4a8e914 Mon Sep 17 00:00:00 2001 From: Joseph Goulden Date: Tue, 10 Dec 2019 19:10:17 +0000 Subject: [PATCH] fix(TUI): Fixed panic when changing order of items in TableView before the number of items in the table is reduced (#3160) --- src/bin/tui/table.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bin/tui/table.rs b/src/bin/tui/table.rs index 51b56f095..76db2f0a6 100644 --- a/src/bin/tui/table.rs +++ b/src/bin/tui/table.rs @@ -492,7 +492,7 @@ impl, H: Eq + Hash + Copy + Clone + 'static> TableView /// Returns the index of the currently selected item within the underlying /// storage vector. pub fn item(&self) -> Option { - if self.items.is_empty() { + if self.items.is_empty() || self.focus > self.rows_to_items.len() { None } else { Some(self.rows_to_items[self.focus]) @@ -604,7 +604,7 @@ impl, H: Eq + Hash + Copy + Clone + 'static> TableView fn sort_items(&mut self, column: H, order: Ordering) { if !self.is_empty() { - let old_item = self.item().unwrap(); + let old_item = self.item(); let mut rows_to_items = self.rows_to_items.clone(); rows_to_items.sort_by(|a, b| { @@ -616,7 +616,7 @@ impl, H: Eq + Hash + Copy + Clone + 'static> TableView }); self.rows_to_items = rows_to_items; - self.set_selected_item(old_item); + old_item.map(|o| self.set_selected_item(o)); } }