diff --git a/include/ALabel.hpp b/include/ALabel.hpp index d4d10878..728bbc49 100644 --- a/include/ALabel.hpp +++ b/include/ALabel.hpp @@ -13,7 +13,7 @@ class ALabel : public IModule { ALabel(const Json::Value &, const std::string &format, uint16_t interval = 0); virtual ~ALabel(); virtual auto update() -> void; - virtual std::string getIcon(uint16_t, const std::string &alt = ""); + virtual std::string getIcon(uint16_t, const std::string &alt = "", uint16_t max = 0); virtual operator Gtk::Widget &(); protected: diff --git a/resources/config b/resources/config index 2c1f69c3..d3f5fbae 100644 --- a/resources/config +++ b/resources/config @@ -78,8 +78,9 @@ // "thermal-zone": 2, // "hwmon-path": "/sys/class/hwmon/hwmon2/temp1_input", "critical-threshold": 80, - // "format-critical": "{temperatureC}°C ", - "format": "{temperatureC}°C " + // "format-critical": "{temperatureC}°C {icon}", + "format": "{temperatureC}°C {icon}", + "format-icons": ["", "", ""] }, "backlight": { // "device": "acpi_video1", diff --git a/src/ALabel.cpp b/src/ALabel.cpp index 9435e03b..ffe06bf3 100644 --- a/src/ALabel.cpp +++ b/src/ALabel.cpp @@ -109,7 +109,7 @@ bool waybar::ALabel::handleScroll(GdkEventScroll* e) { return true; } -std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt) { +std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt, uint16_t max) { auto format_icons = config_["format-icons"]; if (format_icons.isObject()) { if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) { @@ -120,7 +120,7 @@ std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt) } if (format_icons.isArray()) { auto size = format_icons.size(); - auto idx = std::clamp(percentage / (100 / size), 0U, size - 1); + auto idx = std::clamp(percentage / ((max == 0 ? 100 : max) / size), 0U, size - 1); format_icons = format_icons[idx]; } if (format_icons.isString()) { @@ -130,6 +130,9 @@ std::string waybar::ALabel::getIcon(uint16_t percentage, const std::string& alt) } std::string waybar::ALabel::getState(uint8_t value) { + if (!config_["states"].isObject()) { + return ""; + } // Get current state std::vector> states; if (config_["states"].isObject()) { diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index c41a498f..9e6bff79 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -32,8 +32,11 @@ auto waybar::modules::Temperature::update() -> void { } else { label_.get_style_context()->remove_class("critical"); } - label_.set_markup(fmt::format( - format, fmt::arg("temperatureC", temperature_c), fmt::arg("temperatureF", temperature_f))); + auto max_temp = config_["critical-threshold"].isInt() ? config_["critical-threshold"].asInt() : 0; + label_.set_markup(fmt::format(format, + fmt::arg("temperatureC", temperature_c), + fmt::arg("temperatureF", temperature_f), + fmt::arg("icon", getIcon(temperature_c, "", max_temp)))); } std::tuple waybar::modules::Temperature::getTemperature() {