diff --git a/CHANGELOG.md b/CHANGELOG.md index 288203c8f..eab170126 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 bar update. ### Added -- `internal/memory`: New tokens `%used%`, `%free%`, `%total%`, `%swap_total%`, +- `drawtypes/ramp`: Add support for ramp weights. + ([1750](https://github.com/polybar/polybar/issues/1750)) +- `internal/memory`: New tokens `%used%`, `%free%`, `%total%`, `%swap_total%`, `%swap_free%`, and `%swap_used%` that automatically switch between MiB and GiB when below or above 1GiB. ([`2472`](https://github.com/polybar/polybar/issues/2472)) diff --git a/include/drawtypes/ramp.hpp b/include/drawtypes/ramp.hpp index f2ba09742..b960effec 100644 --- a/include/drawtypes/ramp.hpp +++ b/include/drawtypes/ramp.hpp @@ -14,6 +14,7 @@ namespace drawtypes { explicit ramp(vector&& icons) : m_icons(forward(icons)) {} void add(label_t&& icon); + void add(label_t&& icon, unsigned weight); label_t get(size_t index); label_t get_by_percentage(float percentage); label_t get_by_percentage_with_borders(float percentage, float min, float max); diff --git a/src/drawtypes/ramp.cpp b/src/drawtypes/ramp.cpp index b8c026baf..7eb8d724e 100644 --- a/src/drawtypes/ramp.cpp +++ b/src/drawtypes/ramp.cpp @@ -1,5 +1,6 @@ #include "drawtypes/ramp.hpp" +#include "utils/factory.hpp" #include "utils/math.hpp" POLYBAR_NS @@ -9,6 +10,12 @@ namespace drawtypes { m_icons.emplace_back(forward(icon)); } + void ramp::add(label_t&& icon, unsigned weight) { + while (weight--) { + m_icons.emplace_back(icon); + } + } + label_t ramp::get(size_t index) { return m_icons[index]; } @@ -59,9 +66,14 @@ namespace drawtypes { } for (size_t i = 0; i < icons.size(); i++) { - auto icon = load_optional_label(conf, section, name + "-" + to_string(i), icons[i]); + auto ramp_name = name + "-" + to_string(i); + auto icon = load_optional_label(conf, section, ramp_name, icons[i]); icon->copy_undefined(ramp_defaults); - vec.emplace_back(move(icon)); + + auto weight = conf.get(section, ramp_name + "-weight", 1U); + while (weight--) { + vec.emplace_back(icon); + } } return std::make_shared(move(vec)); diff --git a/tests/unit_tests/drawtypes/ramp.cpp b/tests/unit_tests/drawtypes/ramp.cpp index f68b26946..b775c95c2 100644 --- a/tests/unit_tests/drawtypes/ramp.cpp +++ b/tests/unit_tests/drawtypes/ramp.cpp @@ -1,6 +1,7 @@ #include "drawtypes/ramp.hpp" #include "common/test.hpp" +#include "utils/factory.hpp" using namespace polybar::drawtypes; using namespace polybar; @@ -23,3 +24,26 @@ TEST(Ramp, perc) { EXPECT_EQ("test2", r.get_by_percentage_with_borders(29, 20, 40)->get()); EXPECT_EQ("test3", r.get_by_percentage_with_borders(31, 20, 40)->get()); } + +TEST(Ramp, weights) { + ramp r; + r.add(std::make_shared