From 089f617393a9f96d5f2d171f1e82764eff93a208 Mon Sep 17 00:00:00 2001 From: Ben Fiedler Date: Sat, 11 Sep 2021 12:44:25 +0200 Subject: [PATCH] Indicate label truncation (#259) Closes #257 --- crates/eww/src/widgets/widget_definitions.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/eww/src/widgets/widget_definitions.rs b/crates/eww/src/widgets/widget_definitions.rs index cf49939..a12349c 100644 --- a/crates/eww/src/widgets/widget_definitions.rs +++ b/crates/eww/src/widgets/widget_definitions.rs @@ -544,8 +544,15 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result { resolve_block!(bargs, gtk_widget, { // @prop text - the text to display // @prop limit-width - maximum count of characters to display - prop(text: as_string, limit_width: as_i32 = i32::MAX) { - let text = text.chars().take(limit_width as usize).collect::(); + // @prop show_truncated - show whether the text was truncated + prop(text: as_string, limit_width: as_i32 = i32::MAX, show_truncated: as_bool = true) { + let truncated = text.chars().count() > limit_width as usize; + let mut text = text.chars().take(limit_width as usize).collect::(); + + if show_truncated && truncated { + text.push_str("..."); + } + let text = unescape::unescape(&text).context(format!("Failed to unescape label text {}", &text))?; let text = unindent::unindent(&text); gtk_widget.set_text(&text);