From 37dd06590a6db8754aa3dfa99b52d0a1c36069e4 Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Fri, 12 Mar 2021 12:16:41 +0100 Subject: [PATCH] --host-format --- completions/bash | 6 ++++-- src/common.c | 1 + src/fastfetch.c | 18 ++++++++++++++++++ src/fastfetch.h | 1 + src/modules/host.c | 42 +++++++++++++++++++++++++++++++----------- 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/completions/bash b/completions/bash index 3de5918f..492d2552 100644 --- a/completions/bash +++ b/completions/bash @@ -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" diff --git a/src/common.c b/src/common.c index 60d76d98..f6a233d1 100644 --- a/src/common.c +++ b/src/common.c @@ -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; diff --git a/src/fastfetch.c b/src/fastfetch.c index df4ad2dc..8514806f 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -103,6 +103,7 @@ static inline void printHelp() "\n" "Host options:\n" " --host-version : Show the version of the host platform, if possible. Most likely, this will be the BIOS version\n" + " --host-format : Provide the printf format string for host output (+)\n" "\n" "Kernel options:\n" " --kernel-release : Shows the release of the kernel\n" @@ -166,6 +167,19 @@ static inline void printCommandHelpOsFormat() ); } +static inline void printCommandHelpHostFormat() +{ + puts( + "usage fastfetch --host-format \n" + "\n" + " 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) diff --git a/src/fastfetch.h b/src/fastfetch.h index 72b28ce6..188263e7 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -49,6 +49,7 @@ typedef struct FFconfig //Host bool hostShowVersion; + char hostFormat[32]; //Kernel bool kernelShowRelease; diff --git a/src/modules/host.c b/src/modules/host.c index 5a03cf8e..728db233 100644 --- a/src/modules/host.c +++ b/src/modules/host.c @@ -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);