From 4a1cf45795e954e3c1d90d065af8a8608367c223 Mon Sep 17 00:00:00 2001 From: su8 Date: Sat, 4 Aug 2018 22:35:04 +0200 Subject: [PATCH] re-apply intra-aus batteries patch --- src/conky.cc | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index e09a40c4..0174e660 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -39,6 +39,7 @@ #include #include #include +#include #include "common.h" #include "config.h" #include "conky.h" @@ -1989,21 +1990,28 @@ static void update_text() { int inotify_fd = -1; #endif +template +void split(const std::string &s, char delim, Out result) { + std::stringstream ss(s); + std::string item; + while (std::getline(ss, item, delim)) { + *(result++) = item; + } +} + std::vector split(const std::string &s, char delim) { + std::vector elems; + split(s, delim, std::back_inserter(elems)); + return elems; +} + bool is_on_battery() { // checks if at least one battery specified in // "detect_battery" is discharging char buf[64]; - std::string detect_battery_str; - std::string str_buf = str_buf; - detect_battery_str.assign(detect_battery.get(*state)); - detect_battery_str += ','; + std::vector b_items = split(detect_battery.get(*state), ','); - for (char i : detect_battery_str) { // parse using ',' as delimiter - if ((i != ',') && (i != ' ')) { str_buf += i; } - if ((i == ',') && !str_buf.empty()) { - get_battery_short_status(buf, 64, str_buf.c_str()); - if (buf[0] == 'D') { return true; } - str_buf = ""; - } + for(auto const& value: b_items) { + get_battery_short_status(buf, 64, value.c_str()); + if (buf[0] == 'D') { return true; } } return false; }