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 <p.ziegler96@gmail.com> *doc: add #1750 to CHANGELOG * Fix compile error in ramp test Use std::make_shared.
This commit is contained in:
parent
98d9a882ea
commit
55eb19fdc7
@ -60,7 +60,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
bar update.
|
bar update.
|
||||||
|
|
||||||
### Added
|
### 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
|
`%swap_free%`, and `%swap_used%` that automatically switch between MiB and GiB
|
||||||
when below or above 1GiB.
|
when below or above 1GiB.
|
||||||
([`2472`](https://github.com/polybar/polybar/issues/2472))
|
([`2472`](https://github.com/polybar/polybar/issues/2472))
|
||||||
|
@ -14,6 +14,7 @@ namespace drawtypes {
|
|||||||
explicit ramp(vector<label_t>&& icons) : m_icons(forward<decltype(icons)>(icons)) {}
|
explicit ramp(vector<label_t>&& icons) : m_icons(forward<decltype(icons)>(icons)) {}
|
||||||
|
|
||||||
void add(label_t&& icon);
|
void add(label_t&& icon);
|
||||||
|
void add(label_t&& icon, unsigned weight);
|
||||||
label_t get(size_t index);
|
label_t get(size_t index);
|
||||||
label_t get_by_percentage(float percentage);
|
label_t get_by_percentage(float percentage);
|
||||||
label_t get_by_percentage_with_borders(float percentage, float min, float max);
|
label_t get_by_percentage_with_borders(float percentage, float min, float max);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "drawtypes/ramp.hpp"
|
#include "drawtypes/ramp.hpp"
|
||||||
|
|
||||||
|
#include "utils/factory.hpp"
|
||||||
#include "utils/math.hpp"
|
#include "utils/math.hpp"
|
||||||
|
|
||||||
POLYBAR_NS
|
POLYBAR_NS
|
||||||
@ -9,6 +10,12 @@ namespace drawtypes {
|
|||||||
m_icons.emplace_back(forward<decltype(icon)>(icon));
|
m_icons.emplace_back(forward<decltype(icon)>(icon));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ramp::add(label_t&& icon, unsigned weight) {
|
||||||
|
while (weight--) {
|
||||||
|
m_icons.emplace_back(icon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
label_t ramp::get(size_t index) {
|
label_t ramp::get(size_t index) {
|
||||||
return m_icons[index];
|
return m_icons[index];
|
||||||
}
|
}
|
||||||
@ -59,9 +66,14 @@ namespace drawtypes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < icons.size(); i++) {
|
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);
|
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<drawtypes::ramp>(move(vec));
|
return std::make_shared<drawtypes::ramp>(move(vec));
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "drawtypes/ramp.hpp"
|
#include "drawtypes/ramp.hpp"
|
||||||
|
|
||||||
#include "common/test.hpp"
|
#include "common/test.hpp"
|
||||||
|
#include "utils/factory.hpp"
|
||||||
|
|
||||||
using namespace polybar::drawtypes;
|
using namespace polybar::drawtypes;
|
||||||
using namespace polybar;
|
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("test2", r.get_by_percentage_with_borders(29, 20, 40)->get());
|
||||||
EXPECT_EQ("test3", r.get_by_percentage_with_borders(31, 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<label>("test1", 0), 1);
|
||||||
|
r.add(std::make_shared<label>("test2", 0), 2);
|
||||||
|
r.add(std::make_shared<label>("test3", 0), 5);
|
||||||
|
|
||||||
|
EXPECT_EQ("test1", r.get_by_percentage(12)->get());
|
||||||
|
EXPECT_EQ("test2", r.get_by_percentage(13)->get());
|
||||||
|
EXPECT_EQ("test2", r.get_by_percentage(37)->get());
|
||||||
|
EXPECT_EQ("test3", r.get_by_percentage(38)->get());
|
||||||
|
|
||||||
|
EXPECT_EQ("test1", r.get_by_percentage_with_borders(19, 20, 40)->get());
|
||||||
|
EXPECT_EQ("test2", r.get_by_percentage_with_borders(21, 20, 40)->get());
|
||||||
|
EXPECT_EQ("test3", r.get_by_percentage_with_borders(39, 20, 40)->get());
|
||||||
|
EXPECT_EQ("test3", r.get_by_percentage_with_borders(41, 20, 40)->get());
|
||||||
|
EXPECT_EQ("test1", r.get_by_percentage_with_borders(20, 20, 40)->get());
|
||||||
|
EXPECT_EQ("test3", r.get_by_percentage_with_borders(40, 20, 40)->get());
|
||||||
|
r.add(std::make_shared<label>("test4", 0));
|
||||||
|
r.add(std::make_shared<label>("test5", 0));
|
||||||
|
EXPECT_EQ("test2", r.get_by_percentage_with_borders(24, 20, 40)->get());
|
||||||
|
EXPECT_EQ("test3", r.get_by_percentage_with_borders(25, 20, 40)->get());
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user