From de232de41b428b2caf1f41e021e67c0b86e65908 Mon Sep 17 00:00:00 2001 From: kawaki-san <70331483+kawaki-san@users.noreply.github.com> Date: Sun, 26 Mar 2023 10:25:24 +0200 Subject: [PATCH] feat: `truncate-left` for labels (#721) feat: `truncate-left` for labels Adds a flag that allows truncation to the left of text --------- Co-authored-by: kawaki-san Co-authored-by: elkowar <5300871+elkowar@users.noreply.github.com> --- CHANGELOG.md | 1 + crates/eww/src/widgets/widget_definitions.rs | 28 +++++++++++++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24ef038..1e38bdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to eww will be listed here, starting at changes since versio ## [Unreleased] ### Features +- Add `truncate-left` property on `label` widgets (By: kawaki-san) - Add support for safe access (`?.`) in simplexpr (By: oldwomanjosiah) - Allow floating-point numbers in percentages for window-geometry - Add support for safe access with index (`?.[n]`) (By: ModProg) diff --git a/crates/eww/src/widgets/widget_definitions.rs b/crates/eww/src/widgets/widget_definitions.rs index 91662ff..ecc8f13 100644 --- a/crates/eww/src/widgets/widget_definitions.rs +++ b/crates/eww/src/widgets/widget_definitions.rs @@ -819,14 +819,28 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result { def_widget!(bargs, _g, gtk_widget, { // @prop text - the text to display // @prop limit-width - maximum count of characters to display + // @prop truncate_left - whether to truncate on the left side // @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("..."); - } + prop(text: as_string, limit_width: as_i32 = i32::MAX, truncate_left: as_bool = false, show_truncated: as_bool = true) { + let limit_width = limit_width as usize; + let char_count = text.chars().count(); + let text = if char_count > limit_width { + let mut truncated: String = if truncate_left { + text.chars().skip(char_count - limit_width).collect() + } else { + text.chars().take(limit_width).collect() + }; + if show_truncated { + if truncate_left { + truncated.insert_str(0, "..."); + } else { + truncated.push_str("..."); + } + } + truncated + } else { + text + }; let text = unescape::unescape(&text).context(format!("Failed to unescape label text {}", &text))?; let text = unindent(&text);