From 2c2575a0c9447a61529e6463b9db05f9668f164d Mon Sep 17 00:00:00 2001 From: TheDoctor314 Date: Tue, 21 Sep 2021 19:05:16 +0530 Subject: [PATCH 1/2] feat(ramp) Implement ramp weights *Add test for ramp weights *[drawtypes/ramp] Implement ramp weights Simply clone `label_t` weight no. of times in the icon list This helps us not to change any of the calculations. *Fix silly bug Forgot to add a hyphen for the `weight` parameter. Co-authored-by: Patrick Ziegler *doc: add #1750 to CHANGELOG --- CHANGELOG.md | 2 ++ include/drawtypes/ramp.hpp | 1 + src/drawtypes/ramp.cpp | 16 ++++++++++++++-- tests/unit_tests/drawtypes/ramp.cpp | 26 +++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db65179ce..2e6360e5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,6 +52,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The `POLYBAR_FLAGS` cmake variable can be used to pass extra C++ compiler flags. ### Added +- `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. 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 9ef83cc0b..363076e96 100644 --- a/src/drawtypes/ramp.cpp +++ b/src/drawtypes/ramp.cpp @@ -1,4 +1,5 @@ #include "drawtypes/ramp.hpp" + #include "utils/factory.hpp" #include "utils/math.hpp" @@ -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 factory_util::shared(move(vec)); diff --git a/tests/unit_tests/drawtypes/ramp.cpp b/tests/unit_tests/drawtypes/ramp.cpp index 2db041a73..9c934cbc5 100644 --- a/tests/unit_tests/drawtypes/ramp.cpp +++ b/tests/unit_tests/drawtypes/ramp.cpp @@ -1,5 +1,6 @@ -#include "common/test.hpp" #include "drawtypes/ramp.hpp" + +#include "common/test.hpp" #include "utils/factory.hpp" using namespace polybar::drawtypes; @@ -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(factory_util::shared