From 90dd26fa0236953e6a8704dcae6d19fec733e638 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Mon, 11 Mar 2019 21:35:36 +0100 Subject: [PATCH] fix panic caused by strings that where cut outside of char boundary --- src/ui/splitbutton.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ui/splitbutton.rs b/src/ui/splitbutton.rs index 9f627bf..fd0d3dc 100644 --- a/src/ui/splitbutton.rs +++ b/src/ui/splitbutton.rs @@ -56,19 +56,25 @@ impl View for SplitButton { ColorStyle::highlight() }; - // shorten titles that are too long and append ".." to indicate this - let mut left_shortened = self.left.clone(); - left_shortened.truncate(printer.size.x - self.right.width() - 1); - if left_shortened.width() < self.left.width() { - let offset = left_shortened.width() - 2; - left_shortened.replace_range(offset.., ".."); - } - + // draw left string printer.with_color(style, |printer| { - printer.print((0, 0), &left_shortened); + printer.print((0, 0), &self.left); }); - // track duration goes to the end of the line + // draw ".." to indicate a cut off string + let max_length = printer + .size + .x + .checked_sub(self.right.width() + 1) + .unwrap_or(0); + if max_length < self.left.width() { + let offset = max_length.checked_sub(1).unwrap_or(0); + printer.with_color(style, |printer| { + printer.print((offset, 0), ".."); + }); + } + + // draw right string let offset = HAlign::Right.get_offset(self.right.width(), printer.size.x); printer.with_color(style, |printer| {