From 55eb19fdc791b7ab5bfae9c5ceea9ef552f04113 Mon Sep 17 00:00:00 2001 From: TheDoctor314 <64731940+TheDoctor314@users.noreply.github.com> Date: Thu, 23 Sep 2021 01:16:20 +0530 Subject: [PATCH] feat(ramp): Allow specifying ramp weights (#2505) * 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 * Fix compile error in ramp test Use std::make_shared. --- CHANGELOG.md | 4 +++- include/drawtypes/ramp.hpp | 1 + src/drawtypes/ramp.cpp | 16 ++++++++++++++-- tests/unit_tests/drawtypes/ramp.cpp | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 288203c8..eab17012 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 f2ba0974..b960effe 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 b8c026ba..7eb8d724 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 f68b2694..b775c95c 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