From 181dfb5cfc107f51761c3f9d3e9a665f3aac14e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 18 Oct 2021 22:38:00 +0800 Subject: [PATCH] Processes: add new module --- CMakeLists.txt | 1 + completions/bash | 2 ++ src/common/format.c | 3 +++ src/common/init.c | 2 ++ src/fastfetch.c | 15 +++++++++++++++ src/fastfetch.h | 4 ++++ src/flashfetch.c | 1 + src/modules/processes.c | 20 ++++++++++++++++++++ src/modules/uptime.c | 2 +- 9 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/modules/processes.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 83b5e69733..189ae81671 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,6 +98,7 @@ set(SRCS src/modules/host.c src/modules/kernel.c src/modules/uptime.c + src/modules/processes.c src/modules/packages.c src/modules/shell.c src/modules/resolution.c diff --git a/completions/bash b/completions/bash index bd293a18af..fa4818690d 100644 --- a/completions/bash +++ b/completions/bash @@ -145,6 +145,8 @@ __fastfetch_completion() "--kernel-key" "--uptime-format" "--uptime-key" + "--processes-format" + "--processes-key" "--packages-format" "--packages-key" "--shell-format" diff --git a/src/common/format.c b/src/common/format.c index 92a46a7272..b251ca393e 100644 --- a/src/common/format.c +++ b/src/common/format.c @@ -6,6 +6,8 @@ void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg) ffStrbufAppendF(buffer, "%i", *(int*)formatarg->value); else if(formatarg->type == FF_FORMAT_ARG_TYPE_UINT) ffStrbufAppendF(buffer, "%u", *(uint32_t*)formatarg->value); + else if(formatarg->type == FF_FORMAT_ARG_TYPE_UINT16) + ffStrbufAppendF(buffer, "%hu", *(uint16_t*)formatarg->value); else if(formatarg->type == FF_FORMAT_ARG_TYPE_UINT8) ffStrbufAppendF(buffer, "%hhu", *(uint8_t*)formatarg->value); else if(formatarg->type == FF_FORMAT_ARG_TYPE_STRING) @@ -74,6 +76,7 @@ static inline bool formatArgSet(const FFformatarg* arg) (arg->type == FF_FORMAT_ARG_TYPE_STRBUF && ((FFstrbuf*)arg->value)->length > 0) || (arg->type == FF_FORMAT_ARG_TYPE_STRING && *(const char*)arg->value != '\0') || (arg->type == FF_FORMAT_ARG_TYPE_UINT8 && *(uint8_t*)arg->value > 0) || + (arg->type == FF_FORMAT_ARG_TYPE_UINT16 && *(uint16_t*)arg->value > 0) || (arg->type == FF_FORMAT_ARG_TYPE_UINT && *(uint32_t*)arg->value > 0) ); } diff --git a/src/common/init.c b/src/common/init.c index 1fa2bb01ed..5c4c99956d 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -129,6 +129,8 @@ static void defaultConfig(FFinstance* instance) ffStrbufInitA(&instance->config.kernelKey, 1); ffStrbufInitA(&instance->config.uptimeFormat, 1); ffStrbufInitA(&instance->config.uptimeKey, 1); + ffStrbufInitA(&instance->config.processesFormat, 1); + ffStrbufInitA(&instance->config.processesKey, 1); ffStrbufInitA(&instance->config.packagesFormat, 1); ffStrbufInitA(&instance->config.packagesKey, 1); ffStrbufInitA(&instance->config.shellFormat, 1); diff --git a/src/fastfetch.c b/src/fastfetch.c index 2012746f17..008b9ceed7 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -71,6 +71,7 @@ static inline void printHelp() " --host-format \n" " --kernel-format \n" " --uptime-format \n" + " --processes-format \n" " --packages-format \n" " --shell-format \n" " --resolution-format \n" @@ -96,6 +97,7 @@ static inline void printHelp() " --host-key \n" " --kernel-key \n" " --uptime-key \n" + " --processes-key \n" " --packages-key \n" " --shell-key \n" " --resolution-key : takes the resolution index as argument\n" @@ -286,6 +288,12 @@ static inline void printCommandHelp(const char* command) "Seconds" ); } + else if(strcasecmp(command, "processes-format") == 0) + { + constructAndPrintCommandHelpFormat("processes", "{}", 1, + "Count" + ); + } else if(strcasecmp(command, "packages-format") == 0) { constructAndPrintCommandHelpFormat("packages", "{2} (pacman){?3}[{3}]{?}, {4} (dpkg), {5} (rpm), {6} (xps), {7}, (flatpak), {8} (snap)", 8, @@ -501,6 +509,7 @@ static inline void printAvailableModules() "Memory\n" "OS\n" "Packages\n" + "Processes\n" "Resolution\n" "Separator\n" "Shell\n" @@ -848,6 +857,10 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con optionParseString(key, value, &instance->config.uptimeFormat); else if(strcasecmp(key, "--uptime-key") == 0) optionParseString(key, value, &instance->config.uptimeKey); + else if(strcasecmp(key, "--processes-format") == 0) + optionParseString(key, value, &instance->config.processesFormat); + else if(strcasecmp(key, "--processes-key") == 0) + optionParseString(key, value, &instance->config.processesKey); else if(strcasecmp(key, "--packages-format") == 0) optionParseString(key, value, &instance->config.packagesFormat); else if(strcasecmp(key, "--packages-key") == 0) @@ -1043,6 +1056,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char ffPrintKernel(instance); else if(strcasecmp(line, "uptime") == 0) ffPrintUptime(instance); + else if(strcasecmp(line, "processes") == 0) + ffPrintProcesses(instance); else if(strcasecmp(line, "packages") == 0) ffPrintPackages(instance); else if(strcasecmp(line, "shell") == 0) diff --git a/src/fastfetch.h b/src/fastfetch.h index 49231ed07c..ab569074ec 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -55,6 +55,8 @@ typedef struct FFconfig FFstrbuf kernelKey; FFstrbuf uptimeFormat; FFstrbuf uptimeKey; + FFstrbuf processesFormat; + FFstrbuf processesKey; FFstrbuf packagesFormat; FFstrbuf packagesKey; FFstrbuf shellFormat; @@ -202,6 +204,7 @@ typedef enum FFformatargtype { FF_FORMAT_ARG_TYPE_NULL = 0, FF_FORMAT_ARG_TYPE_UINT, + FF_FORMAT_ARG_TYPE_UINT16, FF_FORMAT_ARG_TYPE_UINT8, FF_FORMAT_ARG_TYPE_INT, FF_FORMAT_ARG_TYPE_STRING, @@ -368,6 +371,7 @@ void ffPrintOS(FFinstance* instance); void ffPrintHost(FFinstance* instance); void ffPrintKernel(FFinstance* instance); void ffPrintUptime(FFinstance* instance); +void ffPrintProcesses(FFinstance* instance); void ffPrintPackages(FFinstance* instance); void ffPrintShell(FFinstance* instance); void ffPrintResolution(FFinstance* instance); diff --git a/src/flashfetch.c b/src/flashfetch.c index 0faa014483..9fb38ca18b 100644 --- a/src/flashfetch.c +++ b/src/flashfetch.c @@ -26,6 +26,7 @@ int main(int argc, char** argv) ffPrintHost(&instance); ffPrintKernel(&instance); ffPrintUptime(&instance); + ffPrintProcesses(&instance); ffPrintPackages(&instance); ffPrintShell(&instance); ffPrintResolution(&instance); diff --git a/src/modules/processes.c b/src/modules/processes.c new file mode 100644 index 0000000000..ff697dfee2 --- /dev/null +++ b/src/modules/processes.c @@ -0,0 +1,20 @@ +#include "fastfetch.h" + +#define FF_PROCESSES_MODULE_NAME "Processes" +#define FF_PROCESSES_NUM_FORMAT_ARGS 1 + +void ffPrintProcesses(FFinstance* instance) +{ + if(instance->config.processesFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processesKey); + + printf("%hu\n", instance->state.sysinfo.procs); + } + else + { + ffPrintFormatString(instance, FF_PROCESSES_MODULE_NAME, 0, &instance->config.processesKey, &instance->config.processesFormat, NULL, FF_PROCESSES_NUM_FORMAT_ARGS, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_UINT16, &instance->state.sysinfo.procs} + }); + } +} diff --git a/src/modules/uptime.c b/src/modules/uptime.c index 1018399ec3..fb695c5ad2 100644 --- a/src/modules/uptime.c +++ b/src/modules/uptime.c @@ -6,7 +6,7 @@ void ffPrintUptime(FFinstance* instance) { uint32_t days = instance->state.sysinfo.uptime / 86400; - uint32_t hours = (instance->state.sysinfo.uptime - (days * 86400)) / 3600; + uint32_t hours = (instance->state.sysinfo.uptime - (days * 86400)) / 3600; uint32_t minutes = (instance->state.sysinfo.uptime - (days * 86400) - (hours * 3600)) / 60; uint32_t seconds = instance->state.sysinfo.uptime - (days * 86400) - (hours * 3600) - (minutes * 60);