diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b6ce48fc..0d183ca92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -172,6 +172,7 @@ set(SRCS src/modules/terminal.c src/modules/terminalfont.c src/modules/cpu.c + src/modules/cpuUsage.c src/modules/gpu.c src/modules/memory.c src/modules/disk.c diff --git a/completions/bash b/completions/bash index 86f3aa960..f96cf3cd7 100644 --- a/completions/bash +++ b/completions/bash @@ -20,6 +20,7 @@ __fastfetch_complete_help() "terminal-format" "terminal-font-format" "cpu-format" + "cpu-usage-format" "gpu-format" "memory-format" "disk-format" @@ -174,6 +175,8 @@ __fastfetch_completion() "--terminal-font-key" "--cpu-format" "--cpu-key" + "--cpu-usage-format" + "--cpu-usage-key" "--gpu-format" "--gpu-key" "--memory-format" diff --git a/presets/verbose b/presets/verbose index 2624667aa..6def292e4 100644 --- a/presets/verbose +++ b/presets/verbose @@ -18,6 +18,7 @@ --terminal-format "Process: {}; Path: {}; Exe: {}" --terminal-font-format "Raw: {}; Name: {}; Size: {}; Styles: {}; Pretty: {}" --cpu-format "Name: {}; Pretty: {}; Vendor: {}; Logical online: {}; Logical configured: {}; Physical: {}; Cores: {}; bios: {}; scaling max: {}; scaling min: {}; info max: {}; info min: {}; cpuinfo: {}; frequency: {}" +--cpu-usage-format "Percentage: {}" --gpu-format "Vendor: {}; Vendor pretty: {}; Name: {}; Name pretty: {}" --memory-format "Used: {}; Total: {}; Percentage: {}" --disk-format "Used: {}; Total: {}; Files: {}; Percentage: {}" diff --git a/src/common/init.c b/src/common/init.c index 8d2d90a9a..236fcc66e 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -153,6 +153,8 @@ static void defaultConfig(FFinstance* instance) ffStrbufInitA(&instance->config.termFontKey, 0); ffStrbufInitA(&instance->config.cpuFormat, 0); ffStrbufInitA(&instance->config.cpuKey, 0); + ffStrbufInitA(&instance->config.cpuUsageFormat, 0); + ffStrbufInitA(&instance->config.cpuUsageKey, 0); ffStrbufInitA(&instance->config.gpuFormat, 0); ffStrbufInitA(&instance->config.gpuKey, 0); ffStrbufInitA(&instance->config.memoryFormat, 0); diff --git a/src/data/config.txt b/src/data/config.txt index 0e4c4b972..08d980640 100644 --- a/src/data/config.txt +++ b/src/data/config.txt @@ -124,6 +124,7 @@ #--terminal-key Terminal #--terminal-font-key Terminal Font #--cpu-key CPU +#--cpu-usage-key CPU Usage #--gpu-key GPU {1} #--memory-key Memory #--disk-key Disk ({1}) @@ -154,6 +155,7 @@ #--terminal-format #--terminal-font-format #--cpu-format +#--cpu-usage-format #--gpu-format #--memory-format #--disk-format diff --git a/src/data/help.txt b/src/data/help.txt index 04abb7467..cf79e2e33 100644 --- a/src/data/help.txt +++ b/src/data/help.txt @@ -52,6 +52,7 @@ Format options: Provide the format string for custom output. Use fastfetch --hel --terminal-format --terminal-font-format --cpu-format + --cpu-usage-format --gpu-format --memory-format --disk-format @@ -78,6 +79,7 @@ Key options: Provide a custom key for an output --terminal-key --terminal-font-key --cpu-key + --cpu-usage-key --gpu-key : takes the gpu index as format argument --memory-key --disk-key : takes the mount path as format argument diff --git a/src/data/modules.txt b/src/data/modules.txt index 36424ce1f..b8286f84d 100644 --- a/src/data/modules.txt +++ b/src/data/modules.txt @@ -2,6 +2,7 @@ Battery Break Colors CPU +CPUUsage Cursor DE Disk diff --git a/src/fastfetch.c b/src/fastfetch.c index dfdb34ec1..e04168924 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -242,6 +242,12 @@ static inline void printCommandHelp(const char* command) "most accurate frequeny" ); } + else if(strcasecmp(command, "cpu-usage-format") == 0) + { + constructAndPrintCommandHelpFormat("cpu-usage", "{0}%", 1, + "CPU usage without percent mark" + ); + } else if(strcasecmp(command, "gpu-format") == 0) { constructAndPrintCommandHelpFormat("gpu", "{2} {4}", 4, @@ -713,8 +719,12 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.termFontKey); else if(strcasecmp(key, "--cpu-format") == 0) optionParseString(key, value, &instance->config.cpuFormat); + else if(strcasecmp(key, "--cpu-usage-format") == 0) + optionParseString(key, value, &instance->config.cpuUsageFormat); else if(strcasecmp(key, "--cpu-key") == 0) optionParseString(key, value, &instance->config.cpuKey); + else if(strcasecmp(key, "--cpu-usage-key") == 0) + optionParseString(key, value, &instance->config.cpuUsageKey); else if(strcasecmp(key, "--gpu-format") == 0) optionParseString(key, value, &instance->config.gpuFormat); else if(strcasecmp(key, "--gpu-key") == 0) @@ -889,6 +899,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintTerminalFont(instance); else if(strcasecmp(line, "cpu") == 0) ffPrintCPU(instance); + else if(strcasecmp(line, "cpuusage") == 0) + ffPrintCPUUsage(instance); else if(strcasecmp(line, "gpu") == 0) ffPrintGPU(instance); else if(strcasecmp(line, "memory") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 4b226a743..2aee3d23b 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -87,6 +87,8 @@ typedef struct FFconfig FFstrbuf termFontKey; FFstrbuf cpuFormat; FFstrbuf cpuKey; + FFstrbuf cpuUsageFormat; + FFstrbuf cpuUsageKey; FFstrbuf gpuFormat; FFstrbuf gpuKey; FFstrbuf memoryFormat; @@ -424,6 +426,7 @@ void ffPrintCursor(FFinstance* instance); void ffPrintTerminal(FFinstance* instance); void ffPrintTerminalFont(FFinstance* instance); void ffPrintCPU(FFinstance* instance); +void ffPrintCPUUsage(FFinstance* instance); void ffPrintGPU(FFinstance* instance); void ffPrintMemory(FFinstance* instance); void ffPrintDisk(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index a89726a6f..94def6138 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -38,6 +38,7 @@ int main(int argc, char** argv) ffPrintTerminal(&instance); ffPrintTerminalFont(&instance); ffPrintCPU(&instance); + ffPrintCPUUsage(&instance); ffPrintGPU(&instance); ffPrintMemory(&instance); ffPrintDisk(&instance); diff --git a/src/modules/cpuUsage.c b/src/modules/cpuUsage.c new file mode 100644 index 000000000..cde2683f8 --- /dev/null +++ b/src/modules/cpuUsage.c @@ -0,0 +1,49 @@ +#include "fastfetch.h" + +#include +#include + +#define FF_CPU_USAGE_MODULE_NAME "CPU Usage" +#define FF_CPU_USAGE_NUM_FORMAT_ARGS 1 + +void ffPrintCPUUsage(FFinstance* instance) +{ + long user, nice, system, idle, iowait, irq, softirq; + FILE* procStat = fopen("/proc/stat", "r"); + if(procStat == NULL) + { + ffPrintError(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsageKey, &instance->config.cpuUsageFormat, FF_CPU_USAGE_NUM_FORMAT_ARGS, "fopen(\"/proc/stat\", \"r\") == NULL"); + return; + } + if (fscanf(procStat, "cpu%ld%ld%ld%ld%ld%ld%ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0) goto exit; + long workJiffies1 = user + nice + system; + long totalJiffies1 = workJiffies1 + idle + iowait + irq + softirq; + + sleep(1); + + rewind(procStat); + if (fscanf(procStat, "cpu%ld%ld%ld%ld%ld%ld%ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq) < 0) goto exit; + long workJiffies2 = user + nice + system; + long totalJiffies2 = workJiffies2 + idle + iowait + irq + softirq; + + // https://stackoverflow.com/questions/3017162/how-to-get-total-cpu-usage-in-linux-using-c#answer-3017438 + long workOverPeriod = workJiffies2 - workJiffies1; + long totalOverPeriod = totalJiffies2 - totalJiffies1; + double cpuPercent = (double)workOverPeriod / (double)totalOverPeriod * 100; + + if(instance->config.cpuUsageFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsageKey); + + printf("%.2lf%%\n", cpuPercent); + } + else + { + ffPrintFormatString(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsageKey, &instance->config.cpuUsageFormat, NULL, FF_CPU_USAGE_NUM_FORMAT_ARGS, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_DOUBLE, &cpuPercent} + }); + } + +exit: + fclose(procStat); +}