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 <dev@kanjala.com>
Co-authored-by: elkowar <5300871+elkowar@users.noreply.github.com>
This commit is contained in:
kawaki-san 2023-03-26 10:25:24 +02:00 committed by GitHub
parent c72b881c3f
commit de232de41b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 7 deletions

View File

@ -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)

View File

@ -819,14 +819,28 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result<gtk::Label> {
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::<String>();
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);