--host-format

This commit is contained in:
Linus Dierheimer 2021-03-12 12:16:41 +01:00
parent 8e9cb079b0
commit 37dd06590a
5 changed files with 55 additions and 13 deletions

View File

@ -4,9 +4,10 @@ __fastfetch_complete_help()
{
local __ff_helps=(
"color"
"battery-format"
"packages-format"
"os-format"
"host-format"
"packages-format"
"battery-format"
)
COMPREPLY=($(compgen -W "${__ff_helps[*]}" -- "$CURRENT_WORD"))
}
@ -120,6 +121,7 @@ __fastfetch_completion()
"-x"
"--offsetx"
"--os-format"
"--host-format"
"--packages-format"
"--resolution-format"
"--battery-format"

View File

@ -32,6 +32,7 @@ void ffDefaultConfig(FFconfig* config)
config->osFormat[0] = '\0';
config->hostShowVersion = true;
config->hostFormat[0] = '\0';
config->kernelShowRelease = true;
config->kernelShowVersion = false;

View File

@ -103,6 +103,7 @@ static inline void printHelp()
"\n"
"Host options:\n"
" --host-version <?value>: Show the version of the host platform, if possible. Most likely, this will be the BIOS version\n"
" --host-format <format>: Provide the printf format string for host output (+)\n"
"\n"
"Kernel options:\n"
" --kernel-release <?value>: Shows the release of the kernel\n"
@ -166,6 +167,19 @@ static inline void printCommandHelpOsFormat()
);
}
static inline void printCommandHelpHostFormat()
{
puts(
"usage fastfetch --host-format <format>\n"
"\n"
"<format> is a string of maximum length 32, which is passed to printf as the format string.\n"
"The arguments passed to printf are 3 strings in following order:\n"
"family, name, version\n"
"If an value could not be determined, it will be an zero length string.\n"
"The default value is something like \"%s %s %s\"."
);
}
static inline void printCommandHelpBatteryFormat()
{
puts(
@ -211,6 +225,8 @@ static inline void printCommandHelp(const char* command)
printCommandHelpColor();
else if(strcasecmp(command, "os-format") == 0)
printCommandHelpOsFormat();
else if(strcasecmp(command, "host-format") == 0)
printCommandHelpHostFormat();
else if(strcasecmp(command, "battery-format") == 0)
printCommandHelpBatteryFormat();
else if(strcasecmp(command, "packages-format") == 0)
@ -424,6 +440,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
optionParseString(key, value, instance->config.osFormat, sizeof(instance->config.osFormat));
else if(strcasecmp(key, "--host-version") == 0)
instance->config.hostShowVersion = optionParseBoolean(value);
else if(strcasecmp(key, "--host-format") == 0)
optionParseString(key, value, instance->config.hostFormat, sizeof(instance->config.hostFormat));
else if(strcasecmp(key, "--kernel-release") == 0)
instance->config.kernelShowRelease = optionParseBoolean(value);
else if(strcasecmp(key, "--kernel-version") == 0)

View File

@ -49,6 +49,7 @@ typedef struct FFconfig
//Host
bool hostShowVersion;
char hostFormat[32];
//Kernel
bool kernelShowRelease;

View File

@ -5,19 +5,39 @@ void ffPrintHost(FFinstance* instance)
if(ffPrintCachedValue(instance, "Host"))
return;
char host[256];
ffGetFileContent("/sys/devices/virtual/dmi/id/product_name", host, sizeof(host) - 32); //We subtract 32 to have at least this padding for the version
if(host[0] == '\0')
{
ffPrintError(instance, "Host", "ffGetFileContent(\"/sys/devices/virtual/dmi/id/product_name\", host, sizeof(host)) failed");
return;
}
char family[256];
ffGetFileContent("/sys/devices/virtual/dmi/id/product_family", family, sizeof(family));
if(instance->config.hostShowVersion)
char name[256];
ffGetFileContent("/sys/devices/virtual/dmi/id/product_name", name, sizeof(name));
char version[256];
if(instance->config.hostShowVersion || instance->config.hostFormat[0] != '\0')
ffGetFileContent("/sys/devices/virtual/dmi/id/product_version", version, sizeof(version));
else
version[0] = '\0';
char host[1024];
if(instance->config.hostFormat[0] != '\0')
{
size_t len = strlen(host);
host[len++] = ' ';
ffGetFileContent("/sys/devices/virtual/dmi/id/product_version", host + len, sizeof(host) - len);
snprintf(host, sizeof(host), instance->config.hostFormat, family, name, version);
}
else if(family[0] == '\0' && name[0] == '\0')
{
ffPrintError(instance, "Host", "neither family nor name could be determined");
return;
}else if(family[0] != '\0' && name[0] != '\0')
{
snprintf(host, sizeof(host), "%s %s %s", family, name, instance->config.hostShowVersion ? version : "");
}
else if(family[0] != '\0')
{
snprintf(host, sizeof(host), "%s %s", family, instance->config.hostShowVersion ? version : "");
}
else
{
snprintf(host, sizeof(host), "%s %s", name, instance->config.hostShowVersion ? version : "");
}
ffPrintAndSaveCachedValue(instance, "Host", host);