Merge pull request #5153 from Grinchuelo/feat-customizable-icon-thresholds

feat(ALabel): allow custom percentage thresholds in format-icons
This commit is contained in:
Alexis Rouillard
2026-07-04 00:44:34 +02:00
committed by GitHub
+40 -2
View File
@@ -170,7 +170,26 @@ std::string ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_
} }
if (format_icons.isArray()) { if (format_icons.isArray()) {
auto size = format_icons.size(); auto size = format_icons.size();
if (size != 0U) { if (size != 0U && format_icons[0].isObject()) {
std::string last_icon;
for (const auto& threshold : format_icons) {
if (!threshold.isObject() || !threshold["icon"].isString() || !threshold["max"].isUInt()) {
static bool warned = false;
if (!warned) {
spdlog::warn("format-icons: skipping invalid threshold object, expected {\"icon\": \"...\", \"max\": N}");
warned = true;
}
continue;
}
last_icon = threshold["icon"].asString();
if (percentage <= threshold["max"].asUInt()) {
return last_icon;
}
}
if (!last_icon.empty()) {
return last_icon;
}
} else if (size != 0U) {
auto divisor = std::max(1U, (max == 0 ? 100U : static_cast<unsigned>(max)) / size); auto divisor = std::max(1U, (max == 0 ? 100U : static_cast<unsigned>(max)) / size);
auto idx = std::clamp(percentage / divisor, 0U, size - 1); auto idx = std::clamp(percentage / divisor, 0U, size - 1);
format_icons = format_icons[idx]; format_icons = format_icons[idx];
@@ -197,7 +216,26 @@ std::string ALabel::getIcon(uint16_t percentage, const std::vector<std::string>&
} }
if (format_icons.isArray()) { if (format_icons.isArray()) {
auto size = format_icons.size(); auto size = format_icons.size();
if (size != 0U) { if (size != 0U && format_icons[0].isObject()) {
std::string last_icon;
for (const auto& threshold : format_icons) {
if (!threshold.isObject() || !threshold["icon"].isString() || !threshold["max"].isUInt()) {
static bool warned = false;
if (!warned) {
spdlog::warn("format-icons: skipping invalid threshold object, expected {\"icon\": \"...\", \"max\": N}");
warned = true;
}
continue;
}
last_icon = threshold["icon"].asString();
if (percentage <= threshold["max"].asUInt()) {
return last_icon;
}
}
if (!last_icon.empty()) {
return last_icon;
}
} else if (size != 0U) {
auto divisor = std::max(1U, (max == 0 ? 100U : static_cast<unsigned>(max)) / size); auto divisor = std::max(1U, (max == 0 ? 100U : static_cast<unsigned>(max)) / size);
auto idx = std::clamp(percentage / divisor, 0U, size - 1); auto idx = std::clamp(percentage / divisor, 0U, size - 1);
format_icons = format_icons[idx]; format_icons = format_icons[idx];