mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 00:37:23 +01:00
CpuUsage: support JSON config
This commit is contained in:
+7
-7
@@ -241,7 +241,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/common/settings.c
|
||||
src/detection/bluetooth/bluetooth.c
|
||||
src/detection/cpu/cpu.c
|
||||
src/detection/cpuUsage/cpuUsage.c
|
||||
src/detection/cpuusage/cpuusage.c
|
||||
src/detection/datetime/datetime.c
|
||||
src/detection/disk/disk.c
|
||||
src/detection/displayserver/displayserver.c
|
||||
@@ -271,7 +271,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/chassis.c
|
||||
src/modules/colors/colors.c
|
||||
src/modules/cpu/cpu.c
|
||||
src/modules/cpuUsage.c
|
||||
src/modules/cpuusage/cpuusage.c
|
||||
src/modules/cursor.c
|
||||
src/modules/custom.c
|
||||
src/modules/command/command.c
|
||||
@@ -333,7 +333,7 @@ if(LINUX)
|
||||
src/detection/brightness/brightness_linux.c
|
||||
src/detection/chassis/chassis_linux.c
|
||||
src/detection/cpu/cpu_linux.c
|
||||
src/detection/cpuUsage/cpuUsage_linux.c
|
||||
src/detection/cpuusage/cpuusage_linux.c
|
||||
src/detection/cursor/cursor_linux.c
|
||||
src/detection/bluetooth/bluetooth_linux.c
|
||||
src/detection/disk/disk_linux.c
|
||||
@@ -383,7 +383,7 @@ elseif(ANDROID)
|
||||
src/detection/chassis/chassis_nosupport.c
|
||||
src/detection/cpu/cpu_linux.c
|
||||
src/detection/cursor/cursor_nosupport.c
|
||||
src/detection/cpuUsage/cpuUsage_linux.c
|
||||
src/detection/cpuusage/cpuusage_linux.c
|
||||
src/detection/disk/disk_linux.c
|
||||
src/detection/displayserver/displayserver_nosupport.c
|
||||
src/detection/font/font_nosupport.c
|
||||
@@ -426,7 +426,7 @@ elseif(BSD)
|
||||
src/detection/brightness/brightness_nosupport.c
|
||||
src/detection/chassis/chassis_nosupport.c
|
||||
src/detection/cpu/cpu_bsd.c
|
||||
src/detection/cpuUsage/cpuUsage_bsd.c
|
||||
src/detection/cpuusage/cpuusage_bsd.c
|
||||
src/detection/cursor/cursor_linux.c
|
||||
src/detection/disk/disk_bsd.c
|
||||
src/detection/displayserver/linux/displayserver_linux.c
|
||||
@@ -475,7 +475,7 @@ elseif(APPLE)
|
||||
src/detection/brightness/brightness_apple.c
|
||||
src/detection/chassis/chassis_nosupport.c
|
||||
src/detection/cpu/cpu_apple.c
|
||||
src/detection/cpuUsage/cpuUsage_apple.c
|
||||
src/detection/cpuusage/cpuUsage_apple.c
|
||||
src/detection/cursor/cursor_apple.m
|
||||
src/detection/disk/disk_apple.m
|
||||
src/detection/disk/disk_bsd.c
|
||||
@@ -520,7 +520,7 @@ elseif(WIN32)
|
||||
src/detection/brightness/brightness_windows.cpp
|
||||
src/detection/chassis/chassis_nosupport.c
|
||||
src/detection/cpu/cpu_windows.c
|
||||
src/detection/cpuUsage/cpuUsage_windows.c
|
||||
src/detection/cpuusage/cpuusage_windows.c
|
||||
src/detection/cursor/cursor_windows.c
|
||||
src/detection/disk/disk_windows.c
|
||||
src/detection/displayserver/displayserver_windows.c
|
||||
|
||||
+2
-2
@@ -81,7 +81,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.terminal);
|
||||
initModuleArg(&instance->config.terminalFont);
|
||||
ffInitCPUOptions(&instance->config.cpu);
|
||||
initModuleArg(&instance->config.cpuUsage);
|
||||
ffInitCPUUsageOptions(&instance->config.cpuUsage);
|
||||
initModuleArg(&instance->config.gpu);
|
||||
initModuleArg(&instance->config.memory);
|
||||
initModuleArg(&instance->config.swap);
|
||||
@@ -316,7 +316,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
destroyModuleArg(&instance->config.terminal);
|
||||
destroyModuleArg(&instance->config.terminalFont);
|
||||
ffDestroyCPUOptions(&instance->config.cpu);
|
||||
destroyModuleArg(&instance->config.cpuUsage);
|
||||
ffDestroyCPUUsageOptions(&instance->config.cpuUsage);
|
||||
destroyModuleArg(&instance->config.gpu);
|
||||
destroyModuleArg(&instance->config.memory);
|
||||
destroyModuleArg(&instance->config.swap);
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FF_INCLUDED_detection_cpu_cpuUsage
|
||||
#define FF_INCLUDED_detection_cpu_cpuUsage
|
||||
|
||||
const char* ffGetCpuUsageResult(double* result);
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "fastfetch.h"
|
||||
#include "cpuUsage.h"
|
||||
|
||||
#include "detection/cpuusage/cpuusage.h"
|
||||
#include "common/time.h"
|
||||
|
||||
#include <stdint.h>
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FF_INCLUDED_detection_cpuusage_cpuusage
|
||||
#define FF_INCLUDED_detection_cpuusage_cpuusage
|
||||
|
||||
const char* ffGetCpuUsageResult(double* result);
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "fastfetch.h"
|
||||
#include "cpuUsage.h"
|
||||
#include "detection/cpuusage/cpuusage.h"
|
||||
|
||||
#include <mach/processor_info.h>
|
||||
#include <mach/mach_host.h>
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "cpuUsage.h"
|
||||
#include "detection/cpuusage/cpuusage.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/user.h>
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "fastfetch.h"
|
||||
#include "cpuUsage.h"
|
||||
#include "detection/cpuusage/cpuusage.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
#include "fastfetch.h"
|
||||
#include "cpuUsage.h"
|
||||
#include "detection/cpuusage/cpuusage.h"
|
||||
|
||||
#include "util/mallocHelper.h"
|
||||
|
||||
+4
-4
@@ -1034,7 +1034,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(optionParseModuleArgs(key, value, "terminal", &instance->config.terminal)) {}
|
||||
else if(optionParseModuleArgs(key, value, "terminal-font", &instance->config.terminalFont)) {}
|
||||
else if(ffParseCPUCommandOptions(&instance->config.cpu, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "cpu-usage", &instance->config.cpuUsage)) {}
|
||||
else if(ffParseCPUUsageCommandOptions(&instance->config.cpuUsage, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "gpu", &instance->config.gpu)) {}
|
||||
else if(optionParseModuleArgs(key, value, "memory", &instance->config.memory)) {}
|
||||
else if(optionParseModuleArgs(key, value, "swap", &instance->config.swap)) {}
|
||||
@@ -1300,8 +1300,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
ffPrintTerminalFont(instance);
|
||||
else if(strcasecmp(line, FF_CPU_MODULE_NAME) == 0)
|
||||
ffPrintCPU(instance, &instance->config.cpu);
|
||||
else if(strcasecmp(line, "cpuusage") == 0)
|
||||
ffPrintCPUUsage(instance);
|
||||
else if(strcasecmp(line, FF_CPUUSAGE_MODULE_NAME) == 0)
|
||||
ffPrintCPUUsage(instance, &instance->config.cpuUsage);
|
||||
else if(strcasecmp(line, "gpu") == 0)
|
||||
ffPrintGPU(instance);
|
||||
else if(strcasecmp(line, "memory") == 0)
|
||||
@@ -1373,7 +1373,7 @@ int main(int argc, const char** argv)
|
||||
if(data.structure.length == 0)
|
||||
ffStrbufAppendS(&data.structure, FASTFETCH_DATATEXT_STRUCTURE);
|
||||
|
||||
if(ffStrbufContainIgnCaseS(&data.structure, "CPUUsage"))
|
||||
if(ffStrbufContainIgnCaseS(&data.structure, FF_CPUUSAGE_MODULE_NAME))
|
||||
ffPrepareCPUUsage();
|
||||
|
||||
if(instance.config.multithreading)
|
||||
|
||||
+1
-3
@@ -117,7 +117,7 @@ typedef struct FFconfig
|
||||
FFModuleArgs terminal;
|
||||
FFModuleArgs terminalFont;
|
||||
FFCPUOptions cpu;
|
||||
FFModuleArgs cpuUsage;
|
||||
FFCPUUsageOptions cpuUsage;
|
||||
FFModuleArgs gpu;
|
||||
FFModuleArgs memory;
|
||||
FFModuleArgs swap;
|
||||
@@ -242,7 +242,6 @@ void ffLogoBuiltinListAutocompletion();
|
||||
|
||||
//Common
|
||||
|
||||
void ffPrepareCPUUsage();
|
||||
void ffPreparePublicIp(FFinstance* instance);
|
||||
void ffPrepareWeather(FFinstance* instance);
|
||||
|
||||
@@ -264,7 +263,6 @@ void ffPrintFont(FFinstance* instance);
|
||||
void ffPrintCursor(FFinstance* instance);
|
||||
void ffPrintTerminal(FFinstance* instance);
|
||||
void ffPrintTerminalFont(FFinstance* instance);
|
||||
void ffPrintCPUUsage(FFinstance* instance);
|
||||
void ffPrintGPU(FFinstance* instance);
|
||||
void ffPrintMemory(FFinstance* instance);
|
||||
void ffPrintSwap(FFinstance* instance);
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ int main(int argc, char** argv)
|
||||
//ffPrintLocalIp(&instance);
|
||||
//ffPrintPublicIp(&instance);
|
||||
//ffPrintWifi(&instance);
|
||||
//ffPrintCPUUsage(&instance);
|
||||
//ffPrintCPUUsage(&instance, &instance.config.cpuUsage);
|
||||
ffPrintLocale(&instance);
|
||||
//ffPrintDateTime(&instance);
|
||||
//ffPrintDate(&instance);
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/bar.h"
|
||||
#include "detection/cpuUsage/cpuUsage.h"
|
||||
|
||||
#define FF_CPU_USAGE_MODULE_NAME "CPU Usage"
|
||||
#define FF_CPU_USAGE_NUM_FORMAT_ARGS 1
|
||||
|
||||
void ffPrintCPUUsage(FFinstance* instance)
|
||||
{
|
||||
double percentage = 0.0/0.0;
|
||||
const char* error = ffGetCpuUsageResult(&percentage);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsage, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.cpuUsage.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsage.key);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY str;
|
||||
ffStrbufInit(&str);
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
ffAppendPercentBar(instance, &str, (uint8_t)percentage, 0, 5, 8);
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
{
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0);
|
||||
}
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_CPU_USAGE_MODULE_NAME, 0, &instance->config.cpuUsage, FF_CPU_USAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_DOUBLE, &percentage}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/bar.h"
|
||||
#include "detection/cpuusage/cpuusage.h"
|
||||
#include "modules/cpuusage/cpuusage.h"
|
||||
|
||||
#define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage"
|
||||
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 1
|
||||
|
||||
void ffPrintCPUUsage(FFinstance* instance, FFCPUUsageOptions* options)
|
||||
{
|
||||
double percentage = 0.0/0.0;
|
||||
const char* error = ffGetCpuUsageResult(&percentage);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs.key);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY str;
|
||||
ffStrbufInit(&str);
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
ffAppendPercentBar(instance, &str, (uint8_t)percentage, 0, 5, 8);
|
||||
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
{
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0);
|
||||
}
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_CPUUSAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_DOUBLE, &percentage}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
|
||||
{
|
||||
options->moduleName = FF_CPUUSAGE_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_CPUUSAGE_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseCPUUsageJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFCPUUsageOptions __attribute__((__cleanup__(ffDestroyCPUUsageOptions))) options;
|
||||
ffInitCPUUsageOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_CPUUSAGE_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintCPUUsage(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#define FF_CPUUSAGE_MODULE_NAME "CPUUsage"
|
||||
|
||||
void ffPrepareCPUUsage();
|
||||
|
||||
void ffPrintCPUUsage(FFinstance* instance, FFCPUUsageOptions* options);
|
||||
void ffInitCPUUsageOptions(FFCPUUsageOptions* options);
|
||||
bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key, const char* value);
|
||||
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseCPUUsageJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFCPUUsageOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFCPUUsageOptions;
|
||||
@@ -40,6 +40,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'C': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_CPU_MODULE_NAME, ffParseCPUJsonObject) ||
|
||||
tryModule(instance, type, module, FF_CPUUSAGE_MODULE_NAME, ffParseCPUUsageJsonObject) ||
|
||||
tryModule(instance, type, module, FF_COMMAND_MODULE_NAME, ffParseCommandJsonObject) ||
|
||||
tryModule(instance, type, module, FF_COLORS_MODULE_NAME, ffParseColorsJsonObject) ||
|
||||
false;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "modules/board/board.h"
|
||||
#include "modules/break/break.h"
|
||||
#include "modules/cpu/cpu.h"
|
||||
#include "modules/cpuusage/cpuusage.h"
|
||||
#include "modules/command/command.h"
|
||||
#include "modules/colors/colors.h"
|
||||
#include "modules/datetime/datetime.h"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "modules/board/option.h"
|
||||
#include "modules/brightness/option.h"
|
||||
#include "modules/cpu/option.h"
|
||||
#include "modules/cpuusage/option.h"
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/datetime/option.h"
|
||||
#include "modules/display/option.h"
|
||||
|
||||
Reference in New Issue
Block a user