Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ramp): Allow specifying ramp weights #2505

Merged
merged 4 commits into from Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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))
Expand Down
1 change: 1 addition & 0 deletions include/drawtypes/ramp.hpp
Expand Up @@ -14,6 +14,7 @@ namespace drawtypes {
explicit ramp(vector<label_t>&& icons) : m_icons(forward<decltype(icons)>(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);
Expand Down
16 changes: 14 additions & 2 deletions src/drawtypes/ramp.cpp
@@ -1,5 +1,6 @@
#include "drawtypes/ramp.hpp"

#include "utils/factory.hpp"
#include "utils/math.hpp"

POLYBAR_NS
Expand All @@ -9,6 +10,12 @@ namespace drawtypes {
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) {
return m_icons[index];
}
Expand Down Expand Up @@ -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<drawtypes::ramp>(move(vec));
Expand Down
24 changes: 24 additions & 0 deletions 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;
Expand All @@ -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<label>("test1", 0), 1);
r.add(factory_util::shared<label>("test2", 0), 2);
r.add(factory_util::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(factory_util::shared<label>("test4", 0));
r.add(factory_util::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());
}