feat(temperature): format-icons

This commit is contained in:
Alex 2019-05-13 11:31:05 +02:00
parent 62f8af8a39
commit fcb23d3104
4 changed files with 14 additions and 7 deletions

View File

@ -13,7 +13,7 @@ class ALabel : public IModule {
ALabel(const Json::Value &, const std::string &format, uint16_t interval = 0); ALabel(const Json::Value &, const std::string &format, uint16_t interval = 0);
virtual ~ALabel(); virtual ~ALabel();
virtual auto update() -> void; 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 &(); virtual operator Gtk::Widget &();
protected: protected:

View File

@ -78,8 +78,9 @@
// "thermal-zone": 2, // "thermal-zone": 2,
// "hwmon-path": "/sys/class/hwmon/hwmon2/temp1_input", // "hwmon-path": "/sys/class/hwmon/hwmon2/temp1_input",
"critical-threshold": 80, "critical-threshold": 80,
// "format-critical": "{temperatureC}°C ", // "format-critical": "{temperatureC}°C {icon}",
"format": "{temperatureC}°C " "format": "{temperatureC}°C {icon}",
"format-icons": ["", "", ""]
}, },
"backlight": { "backlight": {
// "device": "acpi_video1", // "device": "acpi_video1",

View File

@ -109,7 +109,7 @@ bool waybar::ALabel::handleScroll(GdkEventScroll* e) {
return true; 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"]; auto format_icons = config_["format-icons"];
if (format_icons.isObject()) { if (format_icons.isObject()) {
if (!alt.empty() && (format_icons[alt].isString() || format_icons[alt].isArray())) { 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()) { if (format_icons.isArray()) {
auto size = format_icons.size(); 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]; format_icons = format_icons[idx];
} }
if (format_icons.isString()) { 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) { std::string waybar::ALabel::getState(uint8_t value) {
if (!config_["states"].isObject()) {
return "";
}
// Get current state // Get current state
std::vector<std::pair<std::string, uint8_t>> states; std::vector<std::pair<std::string, uint8_t>> states;
if (config_["states"].isObject()) { if (config_["states"].isObject()) {

View File

@ -32,8 +32,11 @@ auto waybar::modules::Temperature::update() -> void {
} else { } else {
label_.get_style_context()->remove_class("critical"); label_.get_style_context()->remove_class("critical");
} }
label_.set_markup(fmt::format( auto max_temp = config_["critical-threshold"].isInt() ? config_["critical-threshold"].asInt() : 0;
format, fmt::arg("temperatureC", temperature_c), fmt::arg("temperatureF", temperature_f))); 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<uint16_t, uint16_t> waybar::modules::Temperature::getTemperature() { std::tuple<uint16_t, uint16_t> waybar::modules::Temperature::getTemperature() {