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.
This commit is contained in:
Will Page 2020-02-26 08:43:47 -08:00 committed by GitHub
parent 833ad6889e
commit 5423a9a003
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -892,12 +892,15 @@ impl<T: 'static> View for SelectView<T> {
printer.print((0, 0), "<"); printer.print((0, 0), "<");
printer.print((x, 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? printer.print_styled((offset, 0), label.into());
let offset = HAlign::Center.get_offset(label.width(), x + 1); }
printer.print_styled((offset, 0), label.into());
}); });
} else { } else {
// Non-popup mode: we always print the entire list. // Non-popup mode: we always print the entire list.