mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-12 10:22:12 +02:00
CPU Usage: add new module
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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: {}"
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -52,6 +52,7 @@ Format options: Provide the format string for custom output. Use fastfetch --hel
|
||||
--terminal-format <format>
|
||||
--terminal-font-format <format>
|
||||
--cpu-format <format>
|
||||
--cpu-usage-format <format>
|
||||
--gpu-format <format>
|
||||
--memory-format <format>
|
||||
--disk-format <format>
|
||||
@@ -78,6 +79,7 @@ Key options: Provide a custom key for an output
|
||||
--terminal-key <key>
|
||||
--terminal-font-key <key>
|
||||
--cpu-key <key>
|
||||
--cpu-usage-key <key>
|
||||
--gpu-key <key>: takes the gpu index as format argument
|
||||
--memory-key <key>
|
||||
--disk-key <key>: takes the mount path as format argument
|
||||
|
||||
@@ -2,6 +2,7 @@ Battery
|
||||
Break
|
||||
Colors
|
||||
CPU
|
||||
CPUUsage
|
||||
Cursor
|
||||
DE
|
||||
Disk
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -38,6 +38,7 @@ int main(int argc, char** argv)
|
||||
ffPrintTerminal(&instance);
|
||||
ffPrintTerminalFont(&instance);
|
||||
ffPrintCPU(&instance);
|
||||
ffPrintCPUUsage(&instance);
|
||||
ffPrintGPU(&instance);
|
||||
ffPrintMemory(&instance);
|
||||
ffPrintDisk(&instance);
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user