battery options

This commit is contained in:
Linus Dierheimer
2021-03-03 21:03:12 +01:00
parent b95dd4ce17
commit c1fb9525b8
5 changed files with 161 additions and 45 deletions
+75 -16
View File
@@ -1,34 +1,78 @@
#include "fastfetch.h"
void ffPrintBattery(FFinstance* instance)
#include <dirent.h>
static void printBattery(FFinstance* instance, uint8_t index)
{
char model[256];
ffGetFileContent("/sys/class/power_supply/BAT0/model_name", model, sizeof(model));
char manufactor[128];
if(instance->config.batteryShowManufacturer)
{
char manufactorPath[42];
sprintf(manufactorPath, "/sys/class/power_supply/BAT%i/manufacturer", index);
ffGetFileContent(manufactorPath, manufactor, sizeof(manufactor));
}
else
manufactor[0] = '\0';
char model[128];
if(instance->config.batteryShowModel)
{
char modelPath[40];
sprintf(modelPath, "/sys/class/power_supply/BAT%i/model_name", index);
ffGetFileContent(modelPath, model, sizeof(model));
}
else
model[0] = '\0';
char technology[32];
ffGetFileContent("/sys/class/power_supply/BAT0/technology", technology, sizeof(technology));
if(instance->config.batteryShowTechnology)
{
char technologyPath[40];
sprintf(technologyPath, "/sys/class/power_supply/BAT%i/technology", index);
ffGetFileContent(technologyPath, technology, sizeof(technology));
}
else
technology[0] = '\0';
char capacity[32];
ffGetFileContent("/sys/class/power_supply/BAT0/capacity", capacity, sizeof(capacity));
if(instance->config.batteryShowCapacity)
{
char capacityPath[38];
sprintf(capacityPath, "/sys/class/power_supply/BAT%i/capacity", index);
ffGetFileContent(capacityPath, capacity, sizeof(capacity));
}
else
capacity[0] = '\0';
char status[32];
ffGetFileContent("/sys/class/power_supply/BAT0/status", status, sizeof(status));
if(model[0] == '\0' && capacity[0] == '\0' && status[0] == '\0')
if(instance->config.batteryShowStatus)
{
ffPrintError(instance, "Battery", "No file in /sys/class/power_supply/BAT0/ could be read");
char statusPath[36];
sprintf(statusPath, "/sys/class/power_supply/BAT%i/status", index);
ffGetFileContent(statusPath, status, sizeof(status));
}
else
status[0] = '\0';
char key[10];
sprintf(key, "Battery %i", index);
if(manufactor[0] == '\0' && model[0] == '\0' && technology[0] == '\0' && capacity[0] == '\0' && status[0] == '\0')
{
ffPrintError(instance, key, "No file in /sys/class/power_supply/BAT0/ could be read or all battery options are disabled");
return;
}
ffPrintLogoAndKey(instance, "Battery");
ffPrintLogoAndKey(instance, key);
if(manufactor[0] != '\0')
printf("%s ", manufactor);
if(model[0] != '\0')
{
printf("%s ", model);
if(technology[0] != '\0')
printf("(%s) ", technology);
}
if(technology[0] != '\0')
printf("(%s) ", technology);
if(capacity[0] != '\0')
{
@@ -46,5 +90,20 @@ void ffPrintBattery(FFinstance* instance)
else
putchar('\n');
}
}
void ffPrintBattery(FFinstance* instance)
{
for(uint8_t i = 0; i < 5; i++)
{
char path[30];
sprintf(path, "/sys/class/power_supply/BAT%i", i);
DIR* dir = opendir(path);
if(dir != NULL)
{
printBattery(instance, i);
closedir(dir);
}
}
}