From 5423a9a0034bc9621a8d98d0aada29d48990e6b3 Mon Sep 17 00:00:00 2001 From: Will Page Date: Wed, 26 Feb 2020 08:43:47 -0800 Subject: [PATCH] Fix for cursive issue #428, "Rendering an empty SelectView..." (#429) This change fixes cursive issue #428, caused by indexing into an empty list. The issue is corrected by using the .get() method to safely handle the case where the requested index doesn't exist, and renders no label in that case. --- src/views/select_view.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 36ca368..66af462 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -892,12 +892,15 @@ impl View for SelectView { printer.print((0, 0), "<"); printer.print((x, 0), ">"); - let label = &self.items[self.focus()].label; + if let Some(label) = + self.items.get(self.focus()).map(|item| &item.label) + { + // And center the text? + let offset = + HAlign::Center.get_offset(label.width(), x + 1); - // And center the text? - let offset = HAlign::Center.get_offset(label.width(), x + 1); - - printer.print_styled((offset, 0), label.into()); + printer.print_styled((offset, 0), label.into()); + } }); } else { // Non-popup mode: we always print the entire list.