From d1f89dad896fd2ac6ca133f013ce8f6bfb827d0e Mon Sep 17 00:00:00 2001 From: zignis Date: Tue, 16 Apr 2024 15:56:16 +0530 Subject: [PATCH] fix division by zero --- src/utils/truncate_text.rs | 2 +- src/utils/wrap_text.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/truncate_text.rs b/src/utils/truncate_text.rs index df43a16..2650a22 100644 --- a/src/utils/truncate_text.rs +++ b/src/utils/truncate_text.rs @@ -14,7 +14,7 @@ where let mut text = text.to_owned(); let text_width = compute_text_width(&text); let text_chars = text.graphemes(true).count().max(1); // Total number of characters. - let char_width = text_width / text_chars; // Average width of each character in pixels. + let char_width = (text_width / text_chars).max(1); // Average width of each character in pixels. // Maximum number of characters that can fit inside the given `max_width` value. let mut fitting_chars = ((max_width / char_width as u32) as f32).floor() as usize; diff --git a/src/utils/wrap_text.rs b/src/utils/wrap_text.rs index 2b7ba24..9e4f5f5 100644 --- a/src/utils/wrap_text.rs +++ b/src/utils/wrap_text.rs @@ -19,8 +19,8 @@ where F: Fn(&str) -> usize, { let text_width = compute_text_width(text); - let text_chars = text.graphemes(true).count(); - let char_width = text_width / text_chars.max(1); // Average width of each character in pixels. + let text_chars = text.graphemes(true).count().max(1); + let char_width = (text_width / text_chars).max(1); // Average width of each character in pixels. let columns = max_width / char_width as u32; let lines = wrap(text, columns as usize);