Add Battery Widget (#1)

Added a battery widget which only shows if the battery folder is set in the config and exists.
This commit is contained in:
Moss 2023-01-28 14:47:55 -08:00 committed by GitHub
parent 6621e3abc2
commit 2c64d6da79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 1 deletions

View file

@ -129,6 +129,18 @@
font-size: 16px; font-size: 16px;
} }
.battery-util-progress {
color: #ff79c6;
background-color: #44475a;
font-size: 16px;
}
.battery-data-text {
color: #ff79c6;
margin-right: 6px;
font-size: 16px;
}
.ws-dead { .ws-dead {
color: #44475a; color: #44475a;
font-size: 16px; font-size: 16px;

View file

@ -1 +1 @@
{"version":3,"sourceRoot":"","sources":["style.scss"],"names":[],"mappings":"AAkBA;EACI;EACA;;;AASJ;EACI,kBA7BC;EA8BD;;;AAGJ;EACI;EACA;;;AAGJ;EACI,WAxBO;;;AA2BX;EACI;EAEA;EAEA,OA5CO;;;AA8CX;EACI;EAGA,OAlDO;;;AAoDX;EACI;EAGA,OAxDO;;;AA2DX;EACI;EAGA,OAzDE;;;AA+DN;EACI;;;AAGJ;EACI;EACA,OAxEK;EAyEL;;;AAGJ;EACI,WApEO;EAqEP,OAvEK;EAwEL;;;AAEJ;EACI;EACA,OA5EK;EA6EL;;;AAEJ;EACI;EACA,OAjFK;EAkFL;;;AAEJ;EACI;EACA,OAtFK;EAuFL;;;AAGJ;EACI,OAhGK;EAiGL,kBAvGO;EAwGP,WA3FO;;;AA6FX;EACI,OArGK;EAsGL;EACA,WAhGO;;;AAmGX;EACI,OA7GK;EA8GL,kBAlHO;;;AAoHX;EACI,OAjHK;EAkHL;EACA,WA1GO;;;AA6GX;EACI,OAnHK;EAoHL,kBA5HO;;;AA8HX;EACI,OAvHK;EAwHL;EACA,WApHO;;;AAuHX;EACI,OAnIG;EAoIH,kBAtIO;;;AAwIX;EACI,OAvIG;EAwIH;EACA,WA9HO;;;AAiIX;EACI,OA5II;EA6IJ,kBAhJO;EAiJP,WApIO;;;AAsIX;EACI,OAjJI;EAkJJ;EACA,WAzIO;;;AA4IX;EACI,OA1JO;EA2JP,WA9IO;;;AAgJX;EACI,OA7JO;EA8JP,WAlJO;;;AAoJX;EACI,OAhKG;EAiKH,WAtJO;;;AAwJX;EACI,OA9JK;EA+JL,WA1JO;;;AA4JX;EACI,OAvKI;EAwKJ,WA9JO;;;AAkKX;EACI;IACI;;EAEJ;IACI;;;AAGR;EACI;IACI;;EAEJ;IACI;;;AAIR;EACI;IACI,OA1LC;;EA4LL;IACI,OAlMA;;;AAqMR;EACI;IACI,OAvMA;;EAyMJ;IACI,OArMC;;;AAyMT;EACI,kBApNC;EAqND;;;AAEJ;EACI;EACA;EACA;EACA;EACA,OA/MK;;;AAiNT;EACI;EACA;;;AAEJ;EAgBI;EACA;EACA;EACA;EACA;EAEA;EACA;EAEA;;AAxBA;EACI;EACA;EACA;EACA;;AAEJ;EACI;EACA;EACA;EACA;;AAEJ;EACI,OAvOF;;;AAqPN;EACI,OAtPE;EAuPF,kBA9PO;EA+PP;EACH;EACA;;;AAED;EAaI,OAxQK;EAyQL,kBAjRO;EAkRP;EACA;EACH;EACG;;AAjBA;EACI;EACA;EACA;EACA;;AAEJ;EACI;EACA;EACA;EACA;;;AAWR;EACI;EACA;EACH;EACG,kBA7RO;EA8RP;EACH;EACA;;;AAGD;EAEC;EACG;EACH;EACA;EACA;EACA;EACA;;;AAGD;EACC;EACG;EACH;EACG;EACH,kBA/SQ","file":"style.css"} {"version":3,"sourceRoot":"","sources":["style.scss"],"names":[],"mappings":"AAmBA;EACI;EACA;;;AASJ;EACI,kBA7BC;EA8BD;;;AAGJ;EACI;EACA;;;AAGJ;EACI,WAxBO;;;AA2BX;EACI;EAEA;EAEA,OA5CO;;;AA8CX;EACI;EAGA,OAlDO;;;AAoDX;EACI;EAGA,OAxDO;;;AA2DX;EACI;EAGA,OAzDE;;;AA+DN;EACI;;;AAGJ;EACI;EACA,OAxEK;EAyEL;;;AAGJ;EACI,WApEO;EAqEP,OAvEK;EAwEL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AnRK;EAoRL,kBA5RO;EA6RP;EACA;EACH;EACG;;AAjBA;EACI;EACA;EACA;EACA;;AAEJ;EACI;EACA;EACA;EACA;;;AAWR;EACI;EACA;EACH;EACG,kBAxSO;EAySP;EACH;EACA;;;AAGD;EAEC;EACG;EACH;EACA;EACA;EACA;EACA;;;AAGD;EACC;EACG;EACH;EACG;EACH,kBA1TQ","file":"style.css"}

View file

@ -155,6 +155,17 @@ $textsize: 16px;
font-size: $textsize; font-size: $textsize;
} }
.battery-util-progress {
color: $pink;
background-color: $inactive;
font-size: $textsize;
}
.battery-data-text {
color: $pink;
margin-right: 6px;
font-size: $textsize;
}
.ws-dead { .ws-dead {
color: $inactive; color: $inactive;
font-size: $textsize; font-size: $textsize;

View file

@ -2,3 +2,4 @@ CPUThermalZone: /sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon2/temp1_input
SuspendCommand: ~/.config/scripts/sys.sh suspend SuspendCommand: ~/.config/scripts/sys.sh suspend
LockCommand: ~/.config/scripts/sys.sh lock LockCommand: ~/.config/scripts/sys.sh lock
ExitCommand: killall Hyprland ExitCommand: killall Hyprland
BatteryFolder: /sys/class/power_supply/BAT1

View file

@ -29,6 +29,16 @@ namespace Bar
return TimerResult::Ok; return TimerResult::Ok;
} }
static Text* batteryText;
static TimerResult UpdateBattery(CairoSensor& sensor)
{
double percentage = System::GetBatteryPercentage();
batteryText->SetText("Battery: " + Utils::ToStringPrecision(percentage * 100, "%0.1f") + "%");
sensor.SetValue(percentage);
return TimerResult::Ok;
}
static Text* ramText; static Text* ramText;
static TimerResult UpdateRAM(CairoSensor& sensor) static TimerResult UpdateRAM(CairoSensor& sensor)
{ {
@ -281,6 +291,10 @@ namespace Bar
#endif #endif
Sensor(parent, DynCtx::UpdateRAM, "ram-util-progress", "ram-data-text", DynCtx::ramText); Sensor(parent, DynCtx::UpdateRAM, "ram-util-progress", "ram-data-text", DynCtx::ramText);
Sensor(parent, DynCtx::UpdateCPU, "cpu-util-progress", "cpu-data-text", DynCtx::cpuText); Sensor(parent, DynCtx::UpdateCPU, "cpu-util-progress", "cpu-data-text", DynCtx::cpuText);
// Only show battery percentage if battery folder is set and exists
if (System::GetBatteryPercentage() >= 0) {
Sensor(parent, DynCtx::UpdateBattery, "battery-util-progress", "battery-data-text", DynCtx::batteryText);
}
} }
void WidgetPower(Widget& parent) void WidgetPower(Widget& parent)

View file

@ -27,6 +27,7 @@ namespace System
std::string suspendCommand = "systemctl suspend"; std::string suspendCommand = "systemctl suspend";
std::string lockCommand = ""; // idk, no standard way of doing this. std::string lockCommand = ""; // idk, no standard way of doing this.
std::string exitCommand = ""; // idk, no standard way of doing this. std::string exitCommand = ""; // idk, no standard way of doing this.
std::string batteryFolder = ""; // this can be BAT0, BAT1, etc. Usually in /sys/class/power_supply
}; };
static Config config; static Config config;
@ -69,6 +70,10 @@ namespace System
{ {
prop = &config.exitCommand; prop = &config.exitCommand;
} }
else if (line.find("BatteryFolder: ") != std::string::npos)
{
prop = &config.batteryFolder;
}
if (prop == nullptr) if (prop == nullptr)
{ {
LOG("Warning: unknown config var: " << line); LOG("Warning: unknown config var: " << line);
@ -141,6 +146,23 @@ namespace System
return temp; return temp;
} }
double GetBatteryPercentage()
{
std::ifstream fullChargeFile(config.batteryFolder + "/charge_full");
std::ifstream currentChargeFile(config.batteryFolder + "/charge_now");
if (!fullChargeFile.is_open() || !currentChargeFile.is_open())
{
return -1.f;
}
std::string fullChargeStr;
std::string currentChargeStr;
std::getline(fullChargeFile, fullChargeStr);
std::getline(currentChargeFile, currentChargeStr);
uint32_t intFullCharge = atoi(fullChargeStr.c_str());
uint32_t intCurrentCharge = atoi(currentChargeStr.c_str());
return ((double)intCurrentCharge / (double)intFullCharge);
}
RAMInfo GetRAMInfo() RAMInfo GetRAMInfo()
{ {
RAMInfo out{}; RAMInfo out{};

View file

@ -10,6 +10,8 @@ namespace System
// Tctl // Tctl
double GetCPUTemp(); double GetCPUTemp();
double GetBatteryPercentage();
struct RAMInfo struct RAMInfo
{ {
double totalGiB; double totalGiB;