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
+17
View File
@@ -14,6 +14,23 @@ void ffInitState(FFstate* state)
sysinfo(&state->sysinfo);
}
void ffDefaultConfig(FFconfig* config)
{
config->color[0] = '\0';
config->logo_seperator = 4;
config->offsetx = 0;
config->titleLength = 20; // This is overwritten by ffPrintTitle
config->colorLogo = true;
config->showErrors = false;
config->recache = false;
config->batteryShowManufacturer = true;
config->batteryShowModel = true;
config->batteryShowTechnology = true;
config->batteryShowCapacity = true;
config->batteryShowStatus = true;
}
void ffPrintKey(FFconfig* config, const char* key)
{
printf(FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET": ", config->color, key);
+59 -22
View File
@@ -29,25 +29,38 @@ typedef struct FFdata
static void printHelp()
{
puts(
"usage: fastfetch <options>\n"
"Usage: fastfetch <options>\n"
"\n"
" -h, --help: shows this message and exits\n"
" -h <command>, --help <command>: shows help for a specific command and exits\n"
" -v --version: prints the version of fastfetch and exits\n"
" --list-logos: list available logos and exits\n"
" --print-logos: shows available logos and exits\n"
" --print-default-config: prints the default config and exits\n"
"Informative options:\n"
" -h, --help: shows this message and exits\n"
" -h <command>, --help <command>: shows help for a specific command and exits\n"
" -v --version: prints the version of fastfetch and exits\n"
" --list-logos: list available logos and exits\n"
" --print-logos: shows available logos and exits\n"
" --print-default-config: prints the default config and exits\n"
"\n"
"General options:\n"
" --structure <structure>: sets the structure of the fetch. Must be a colon seperated list of keys\n"
" --set <key=value>: hard set the value of an key\n"
" -l <name>, --logo <name>: sets the shown logo. Also changes the main color accordingly\n"
" -c <color>, --color <color>: sets the color of the keys. Must be a linux console color code\n"
" -c <color>, --color <color>: sets the color of the keys. Must be a linux console color code (+)\n"
" -s <width>, --seperator <width>: sets the distance between logo and text\n"
" -x <offset>, --offsetx <offset>: sets the x offset. Can be negative to cut the logo, but no more than logo width.\n"
" --show-errors <?value>: print occuring errors\n"
" --color-logo <?value>: if set to false, the logo will be black / white\n"
" -r <?value> --recache <?value>: if set to true, no cached values will be used\n"
"\n"
"Logo options:\n"
" -l <name>, --logo <name>: sets the shown logo. Also changes the main color accordingly\n"
" --color-logo <?value>: if set to false, the logo will be black / white\n"
"\n"
"Battery options:\n"
" --battery-manufacturer <?value>: Show the manufacturer of the battery, if possible\n"
" --battery-model <?value>: Show the model of the battery, if possible\n"
" --battery-technology <?value>: Show the technology of the battery, if possible\n"
" --battery-capacity <?value>: Show the capacity of the battery, if possible\n"
" --battery-status <?value>: Show the status of the battery, if possible\n"
"\n"
"If an value starts with an ?, it is optional. \"true\" will be used if not set.\n"
"An (+) at the end indicates that more help can be printed with --help <option>\n"
"All options can be make permanent in $XDG_CONFIG_HOME/fastfetch/config.conf"
);
}
@@ -75,17 +88,6 @@ static void printCommandHelp(const char* command)
printf("No specific help for command %s provided\n", command);
}
static void defaultConfig(FFconfig* config)
{
config->color[0] = '\0';
config->logo_seperator = 4;
config->offsetx = 0;
config->titleLength = 20; // This is overwritten by ffPrintTitle
config->colorLogo = true;
config->showErrors = false;
config->recache = false;
}
static bool parseBoolean(const char* str)
{
return
@@ -277,6 +279,41 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
else
instance->config.recache = parseBoolean(value);
}
else if(strcasecmp(key, "--battery-manufacturer") == 0)
{
if(value == NULL)
instance->config.batteryShowManufacturer = true;
else
instance->config.batteryShowManufacturer = parseBoolean(value);
}
else if(strcasecmp(key, "--battery-model") == 0)
{
if(value == NULL)
instance->config.batteryShowModel = true;
else
instance->config.batteryShowModel = parseBoolean(value);
}
else if(strcasecmp(key, "--battery-technology") == 0)
{
if(value == NULL)
instance->config.batteryShowTechnology = true;
else
instance->config.batteryShowTechnology = parseBoolean(value);
}
else if(strcasecmp(key, "--battery-capacity") == 0)
{
if(value == NULL)
instance->config.batteryShowCapacity = true;
else
instance->config.batteryShowCapacity = parseBoolean(value);
}
else if(strcasecmp(key, "--battery-status") == 0)
{
if(value == NULL)
instance->config.batteryShowStatus = true;
else
instance->config.batteryShowStatus = parseBoolean(value);
}
else
{
printf("Error: unknown option: %s\n", key);
@@ -406,7 +443,7 @@ int main(int argc, const char** argv)
{
FFinstance instance;
ffInitState(&instance.state);
defaultConfig(&instance.config);
ffDefaultConfig(&instance.config);
FFdata data;
ffValuestoreInit(&data.valuestore);
+8
View File
@@ -39,6 +39,13 @@ typedef struct FFconfig
bool colorLogo;
bool showErrors;
bool recache;
bool batteryShowManufacturer;
bool batteryShowModel;
bool batteryShowTechnology;
bool batteryShowCapacity;
bool batteryShowStatus;
} FFconfig;
typedef struct FFstate
@@ -57,6 +64,7 @@ typedef struct FFinstance
//Util functions
void ffInitState(FFstate* state);
void ffDefaultConfig(FFconfig* config);
void ffPrintKey(FFconfig* config, const char* key);
void ffPrintLogoAndKey(FFinstance* instance, const char* key);
void ffGetFileContent(const char* fileName, char* buffer, uint32_t bufferSize);
+2 -7
View File
@@ -6,15 +6,10 @@ int main(int argc, char** argv)
{
FFinstance instance;
ffInitState(&instance.state);
ffDefaultConfig(&instance.config);
//Configuration
instance.config.logo_seperator = 4;
instance.config.offsetx = 0;
instance.config.titleLength = 20; // This is overwritten by ffPrintTitle
instance.config.colorLogo = true;
instance.config.showErrors = false;
instance.config.recache = false;
ffLoadLogoSet(&instance.config, "arch");
strcpy(instance.config.color, instance.config.logo.color); //Use the primary color of the logo as key color
+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);
}
}
}