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:
parent
c72b881c3f
commit
de232de41b
2 changed files with 22 additions and 7 deletions
|
@ -6,6 +6,7 @@ All notable changes to eww will be listed here, starting at changes since versio
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
- Add `truncate-left` property on `label` widgets (By: kawaki-san)
|
||||||
- Add support for safe access (`?.`) in simplexpr (By: oldwomanjosiah)
|
- Add support for safe access (`?.`) in simplexpr (By: oldwomanjosiah)
|
||||||
- Allow floating-point numbers in percentages for window-geometry
|
- Allow floating-point numbers in percentages for window-geometry
|
||||||
- Add support for safe access with index (`?.[n]`) (By: ModProg)
|
- Add support for safe access with index (`?.[n]`) (By: ModProg)
|
||||||
|
|
|
@ -819,14 +819,28 @@ fn build_gtk_label(bargs: &mut BuilderArgs) -> Result<gtk::Label> {
|
||||||
def_widget!(bargs, _g, gtk_widget, {
|
def_widget!(bargs, _g, gtk_widget, {
|
||||||
// @prop text - the text to display
|
// @prop text - the text to display
|
||||||
// @prop limit-width - maximum count of characters 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 show_truncated - show whether the text was truncated
|
||||||
prop(text: as_string, limit_width: as_i32 = i32::MAX, show_truncated: as_bool = true) {
|
prop(text: as_string, limit_width: as_i32 = i32::MAX, truncate_left: as_bool = false, show_truncated: as_bool = true) {
|
||||||
let truncated = text.chars().count() > limit_width as usize;
|
let limit_width = limit_width as usize;
|
||||||
let mut text = text.chars().take(limit_width as usize).collect::<String>();
|
let char_count = text.chars().count();
|
||||||
|
let text = if char_count > limit_width {
|
||||||
if show_truncated && truncated {
|
let mut truncated: String = if truncate_left {
|
||||||
text.push_str("...");
|
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 = unescape::unescape(&text).context(format!("Failed to unescape label text {}", &text))?;
|
||||||
let text = unindent(&text);
|
let text = unindent(&text);
|
||||||
|
|
Loading…
Add table
Reference in a new issue