diff --git a/src/ALabel.cpp b/src/ALabel.cpp index 795f87f1..46eeac2f 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -150,7 +150,26 @@ std::string ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_ } if (format_icons.isArray()) { 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(max)) / size); auto idx = std::clamp(percentage / divisor, 0U, size - 1); format_icons = format_icons[idx]; @@ -177,7 +196,26 @@ std::string ALabel::getIcon(uint16_t percentage, const std::vector& } if (format_icons.isArray()) { 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(max)) / size); auto idx = std::clamp(percentage / divisor, 0U, size - 1); format_icons = format_icons[idx];