master ohmu/fastfetch-openbsd / src / modules / processes / processes.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/processes/processes.h"
  5#include "modules/processes/processes.h"
  6
  7bool ffPrintProcesses(FFProcessesOptions* options) {
  8    FFProcessesResult result = {};
  9    const char* error = ffDetectProcesses(options, &result);
 10
 11    if (error) {
 12        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Processes), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 13        return false;
 14    }
 15
 16    if (options->moduleArgs.outputFormat.length == 0) {
 17        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Processes), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 18
 19        printf("%u (%u threads)\n", result.processes, result.threads);
 20    } else {
 21        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Processes), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 22            FF_ARG(result.processes, "result"),
 23            FF_ARG(result.threads, "threads")
 24        }));
 25    }
 26
 27    return true;
 28}
 29
 30void ffParseProcessesJsonObject(FFProcessesOptions* options, yyjson_val* module) {
 31    yyjson_val *key, *val;
 32    size_t idx, max;
 33    yyjson_obj_foreach (module, idx, max, key, val) {
 34        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 35            continue;
 36        }
 37
 38        if (unsafe_yyjson_equals_str(key, "countKprocs")) {
 39            options->countKprocs = yyjson_get_bool(val);
 40            continue;
 41        }
 42
 43        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Processes), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 44    }
 45}
 46
 47[[gnu::cold]]
 48void ffGenerateProcessesJsonConfig(FFProcessesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 49    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 50    yyjson_mut_obj_add_bool(doc, module, "countKprocs", options->countKprocs);
 51}
 52
 53bool ffGenerateProcessesJsonResult([[maybe_unused]] FFProcessesOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 54    FFProcessesResult result = {};
 55    const char* error = ffDetectProcesses(options, &result);
 56
 57    if (error) {
 58        yyjson_mut_obj_add_str(doc, module, "error", error);
 59        return false;
 60    }
 61
 62    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 63    yyjson_mut_obj_add_uint(doc, obj, "processes", result.processes);
 64    yyjson_mut_obj_add_uint(doc, obj, "threads", result.threads);
 65
 66    return true;
 67}
 68
 69void ffInitProcessesOptions(FFProcessesOptions* options) {
 70    ffOptionInitModuleArg(&options->moduleArgs, "");
 71
 72    options->countKprocs = false;
 73}
 74
 75void ffDestroyProcessesOptions(FFProcessesOptions* options) {
 76    ffOptionDestroyModuleArg(&options->moduleArgs);
 77}
 78
 79FFModuleBaseInfo ffProcessesModuleInfo = {
 80    .name = "Processes",
 81    .description = "Print number of running processes and threads",
 82    .displayName = {
 83        .en = "Processes",
 84        .ar = "العمليات",
 85        .cs = "Procesy",
 86        .de = "Prozesse",
 87        .es = "Procesos",
 88        .fi = "Prosessit",
 89        .fr = "Processus",
 90        .gl = "Procesos",
 91        .he = "תהליכים",
 92        .id = "Proses",
 93        .it = "Processi",
 94        .ja = "プロセス",
 95        .ko = "프로세스",
 96        .pl = "Procesy",
 97        .pt = "Processos",
 98        .ru = "Процессы",
 99        .tr = "Süreçler",
100        .uk = "Процеси",
101        .vi = "Tiến trình",
102        .zh_CN = "进程数",
103        .zh_TW = "進程數",
104    },
105    .initOptions = (void*) ffInitProcessesOptions,
106    .destroyOptions = (void*) ffDestroyProcessesOptions,
107    .parseJsonObject = (void*) ffParseProcessesJsonObject,
108    .printModule = (void*) ffPrintProcesses,
109    .generateJsonResult = (void*) ffGenerateProcessesJsonResult,
110    .generateJsonConfig = (void*) ffGenerateProcessesJsonConfig,
111    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
112        { "Process count", "result" },
113        { "Thread count", "threads" },
114    })),
115    .defaultOrder = 13,
116};