master ohmu/fastfetch-openbsd / src / modules / terminal / terminal.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/terminalshell/terminalshell.h"
  5#include "modules/terminal/terminal.h"
  6
  7bool ffPrintTerminal(FFTerminalOptions* options) {
  8    const FFTerminalResult* result = ffDetectTerminal();
  9
 10    if (result->processName.length == 0) {
 11        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Terminal), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Couldn't detect terminal");
 12        return false;
 13    }
 14
 15    if (options->moduleArgs.outputFormat.length == 0) {
 16        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Terminal), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 17
 18        if (result->version.length) {
 19            printf("%s %s\n", result->prettyName.chars, result->version.chars);
 20        } else {
 21            ffStrbufPutTo(&result->prettyName, stdout);
 22        }
 23    } else {
 24        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Terminal), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 25                                                                                                             FF_ARG(result->processName, "process-name"),
 26                                                                                                             FF_ARG(result->exe, "exe"),
 27                                                                                                             FF_ARG(result->exeName, "exe-name"),
 28                                                                                                             FF_ARG(result->pid, "pid"),
 29                                                                                                             FF_ARG(result->prettyName, "pretty-name"),
 30                                                                                                             FF_ARG(result->version, "version"),
 31                                                                                                             FF_ARG(result->exePath, "exe-path"),
 32                                                                                                             FF_ARG(result->tty, "tty"),
 33                                                                                                         }));
 34    }
 35
 36    return true;
 37}
 38
 39void ffParseTerminalJsonObject(FFTerminalOptions* options, yyjson_val* module) {
 40    yyjson_val *key, *val;
 41    size_t idx, max;
 42    yyjson_obj_foreach (module, idx, max, key, val) {
 43        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 44            continue;
 45        }
 46
 47        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Terminal), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 48    }
 49}
 50
 51[[gnu::cold]]
 52void ffGenerateTerminalJsonConfig(FFTerminalOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 53    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 54}
 55
 56bool ffGenerateTerminalJsonResult([[maybe_unused]] FFTerminalOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 57    const FFTerminalResult* result = ffDetectTerminal();
 58
 59    if (result->processName.length == 0) {
 60        yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect terminal");
 61        return false;
 62    }
 63
 64    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 65    yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->processName);
 66    yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result->exe);
 67    yyjson_mut_obj_add_strcpy(doc, obj, "exeName", result->exeName);
 68    yyjson_mut_obj_add_strbuf(doc, obj, "exePath", &result->exePath);
 69    yyjson_mut_obj_add_uint(doc, obj, "pid", result->pid);
 70    yyjson_mut_obj_add_uint(doc, obj, "ppid", result->ppid);
 71    yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->prettyName);
 72    yyjson_mut_obj_add_strbuf(doc, obj, "version", &result->version);
 73    yyjson_mut_obj_add_strbuf(doc, obj, "tty", &result->tty);
 74
 75    return true;
 76}
 77
 78void ffInitTerminalOptions(FFTerminalOptions* options) {
 79    ffOptionInitModuleArg(&options->moduleArgs, "");
 80}
 81
 82void ffDestroyTerminalOptions(FFTerminalOptions* options) {
 83    ffOptionDestroyModuleArg(&options->moduleArgs);
 84}
 85
 86FFModuleBaseInfo ffTerminalModuleInfo = {
 87    .name = "Terminal",
 88    .description = "Print the current terminal name and version",
 89    .displayName = {
 90        .en = "Terminal",
 91        .ar = "الطرفية",
 92        .cs = "Terminál",
 93        .de = "Terminal",
 94        .es = "Terminal",
 95        .fi = "Terminaali",
 96        .fr = "Terminal",
 97        .gl = "Terminal",
 98        .he = "טרמינל",
 99        .id = "Terminal",
100        .it = "Terminale",
101        .ja = "ターミナル",
102        .ko = "터미널",
103        .pl = "Terminal",
104        .pt = "Terminal",
105        .ru = "Терминал",
106        .tr = "Terminal",
107        .uk = "Термінал",
108        .vi = "Terminal",
109        .zh_CN = "终端",
110        .zh_TW = "終端機",
111    },
112    .initOptions = (void*) ffInitTerminalOptions,
113    .destroyOptions = (void*) ffDestroyTerminalOptions,
114    .parseJsonObject = (void*) ffParseTerminalJsonObject,
115    .printModule = (void*) ffPrintTerminal,
116    .generateJsonResult = (void*) ffGenerateTerminalJsonResult,
117    .generateJsonConfig = (void*) ffGenerateTerminalJsonConfig,
118    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
119        { "Terminal process name", "process-name" },
120        { "The first argument of the command line when running the terminal", "exe" },
121        { "Terminal base name of arg0", "exe-name" },
122        { "Terminal pid", "pid" },
123        { "Terminal pretty name", "pretty-name" },
124        { "Terminal version", "version" },
125        { "Terminal full exe path", "exe-path" },
126        { "Terminal tty / pts used", "tty" },
127    })),
128    .defaultOrder = 29,
129};