diff options
Diffstat (limited to 'src/jo_lowbat.cpp')
-rw-r--r-- | src/jo_lowbat.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/jo_lowbat.cpp b/src/jo_lowbat.cpp index 56fe655..ab74eaf 100644 --- a/src/jo_lowbat.cpp +++ b/src/jo_lowbat.cpp @@ -1,5 +1,8 @@ #include <jo_lowbat.hpp> #include <iostream> +#include <array> +#include <memory> +#include <algorithm> using namespace std; @@ -11,3 +14,30 @@ Lowbat::jo_testAcpi(void) { } return 0; } + +string +jo_exec(const char* cmd) { + array<char, 128> buffer; + string result; + unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose); + if (!pipe) { + throw runtime_error("popen() failed!"); + } + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { + result += buffer.data(); + } + return result; +} + +string +Lowbat::jo_fetchBatlvl(void) { + string batlvl; + uint8_t batlvlint; + + cout << "Fetching batlvl: "; + batlvl = this->jo_exec("acpi | awk '{print $4}' | rev | cut -c 3- | rev"); + batlvl.erase(remove(batlvl.begin(), batlvl.end(), '\n'), batlvl.end()); + batlvlint = stoi(batlvl); + cout << batlvlint << "%" << endl; + return (batlvl); +} |