From 238e50034bfe7522a061cd724bd7450f19622bb3 Mon Sep 17 00:00:00 2001 From: augustuswm Date: Fri, 2 Feb 2024 12:41:15 -0600 Subject: [PATCH] Fix panicking string index --- rfd-cli/src/printer/tab.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rfd-cli/src/printer/tab.rs b/rfd-cli/src/printer/tab.rs index 00ffcb53..687c6804 100644 --- a/rfd-cli/src/printer/tab.rs +++ b/rfd-cli/src/printer/tab.rs @@ -646,7 +646,13 @@ impl TabDisplay for SearchResultHit { } printer.print_field(tw, level, "location", &heading_path.iter().join(" > ")); - printer.print_field(tw, level, "content", &&self.content[..255]); + + fn get_slice_of_string(s: &str, n: usize) -> &str { + let char_boundary = s.char_indices().nth(n).map_or(s.len(), |(idx, _)| idx); + &s[..char_boundary] + } + + printer.print_field(tw, level, "content", &format!("{}...", get_slice_of_string(&self.content, 255))); } }