From 7838241a77e0c9677066883853ae602ef8db0453 Mon Sep 17 00:00:00 2001 From: Ashwin Rajesh <46510831+VanillaViking@users.noreply.github.com> Date: Thu, 25 Aug 2022 08:36:38 +1000 Subject: [PATCH] feat(tokens): Negative minimum length adds right padding (#2801) * negative minimum length adds right padding * missing else statement * updated changelog --- CHANGELOG.md | 1 + include/drawtypes/label.hpp | 1 + src/drawtypes/label.cpp | 10 +++++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a0deabd..df5c71e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - `internal/fs`: Use `/` as a fallback if no mountpoints are specified ([`#2572`](https://github.com/polybar/polybar/issues/2572), [`#2705`](https://github.com/polybar/polybar/pull/2705)) - `internal/backlight`: Detect backlight if none specified ([`#2572`](https://github.com/polybar/polybar/issues/2572), [`#2728`](https://github.com/polybar/polybar/pull/2728)) +- Providing a negative min-width to a token adds right-padding ([`#2789`](https://github.com/polybar/polybar/issues/2789), [`#2801`](https://github.com/polybar/polybar/pull/2801)) by [@VanillaViking](https://github.com/VanillaViking). ### Fixed - Waiting for double click interval on modules that don't have a double click action ([`#2663`](https://github.com/polybar/polybar/issues/2663), [`#2695`](https://github.com/polybar/polybar/pull/2695)) diff --git a/include/drawtypes/label.hpp b/include/drawtypes/label.hpp index 3e178fdf..58635efa 100644 --- a/include/drawtypes/label.hpp +++ b/include/drawtypes/label.hpp @@ -16,6 +16,7 @@ namespace drawtypes { size_t max{0_z}; string suffix{""s}; bool zpad{false}; + bool rpadding{false}; }; class label : public non_copyable_mixin { diff --git a/src/drawtypes/label.cpp b/src/drawtypes/label.cpp index e4317d9b..e5ba8911 100644 --- a/src/drawtypes/label.cpp +++ b/src/drawtypes/label.cpp @@ -86,7 +86,11 @@ namespace drawtypes { if (tok.max != 0_z && string_util::char_len(repl) > tok.max) { repl = string_util::utf8_truncate(std::move(repl), tok.max) + tok.suffix; } else if (tok.min != 0_z && repl.length() < tok.min) { - repl.insert(0_z, tok.min - repl.length(), tok.zpad ? '0' : ' '); + if (tok.rpadding) { + repl.append(tok.min - repl.length(), ' '); + } else { + repl.insert(0_z, tok.min - repl.length(), tok.zpad ? '0' : ' '); + } } /* @@ -231,6 +235,10 @@ namespace drawtypes { text = string_util::replace(text, token_str, token.token); try { + if (token_str[pos + 1] == '-') { + token.rpadding = true; + pos++; + } token.min = std::stoul(&token_str[pos + 1], nullptr, 10); // When the number starts with 0 the string is 0-padded token.zpad = token_str[pos + 1] == '0';