master ohmu/fastfetch-openbsd / src / modules / version / version.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/libc/libc.h"
  5#include "detection/version/version.h"
  6#include "modules/version/version.h"
  7
  8bool ffPrintVersion(FFVersionOptions* options) {
  9    FFVersionResult* result = &ffVersionResult;
 10
 11    if (options->moduleArgs.outputFormat.length == 0) {
 12        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Version), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 13        printf("%s %s%s%s (%s)\n", result->projectName, result->version, result->versionTweak, result->debugMode ? "-debug" : "", result->architecture);
 14    } else {
 15        FFLibcResult libcResult;
 16        FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
 17        if (!ffDetectLibc(&libcResult)) {
 18            ffStrbufSetS(&buf, libcResult.name);
 19            if (libcResult.version) {
 20                ffStrbufAppendC(&buf, ' ');
 21                ffStrbufAppendS(&buf, libcResult.version);
 22            }
 23        }
 24
 25        const char* buildType = result->debugMode ? "debug" : "release";
 26        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Version), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 27                                                                                                            FF_ARG(result->projectName, "project-name"),
 28                                                                                                            FF_ARG(result->version, "version"),
 29                                                                                                            FF_ARG(result->versionTweak, "version-tweak"),
 30                                                                                                            FF_ARG(buildType, "build-type"),
 31                                                                                                            FF_ARG(result->sysName, "sysname"),
 32                                                                                                            FF_ARG(result->architecture, "arch"),
 33                                                                                                            FF_ARG(result->cmakeBuiltType, "cmake-built-type"),
 34                                                                                                            FF_ARG(result->compileTime, "compile-time"),
 35                                                                                                            FF_ARG(result->compiler, "compiler"),
 36                                                                                                            FF_ARG(buf, "libc"),
 37                                                                                                        }));
 38    }
 39
 40    return true;
 41}
 42
 43void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module) {
 44    yyjson_val *key, *val;
 45    size_t idx, max;
 46    yyjson_obj_foreach (module, idx, max, key, val) {
 47        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 48            continue;
 49        }
 50
 51        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Version), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 52    }
 53}
 54
 55[[gnu::cold]]
 56void ffGenerateVersionJsonConfig(FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 57    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 58}
 59
 60bool ffGenerateVersionJsonResult([[maybe_unused]] FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 61    FFVersionResult* result = &ffVersionResult;
 62
 63    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 64    yyjson_mut_obj_add_str(doc, obj, "projectName", result->projectName);
 65    yyjson_mut_obj_add_str(doc, obj, "sysName", result->sysName);
 66    yyjson_mut_obj_add_str(doc, obj, "architecture", result->architecture);
 67    yyjson_mut_obj_add_str(doc, obj, "version", result->version);
 68    yyjson_mut_obj_add_str(doc, obj, "versionGit", result->versionGit);
 69    yyjson_mut_obj_add_str(doc, obj, "cmakeBuiltType", result->cmakeBuiltType);
 70    yyjson_mut_obj_add_str(doc, obj, "compileTime", result->compileTime);
 71    yyjson_mut_obj_add_str(doc, obj, "compiler", result->compiler);
 72    yyjson_mut_obj_add_bool(doc, obj, "debugMode", result->debugMode);
 73
 74    FFLibcResult libcResult;
 75    if (ffDetectLibc(&libcResult)) {
 76        yyjson_mut_obj_add_null(doc, obj, "libc");
 77    } else {
 78        FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreateS(libcResult.name);
 79        if (libcResult.version) {
 80            ffStrbufAppendC(&buf, ' ');
 81            ffStrbufAppendS(&buf, libcResult.version);
 82        }
 83        yyjson_mut_obj_add_strbuf(doc, obj, "libc", &buf);
 84    }
 85
 86    return true;
 87}
 88
 89void ffInitVersionOptions(FFVersionOptions* options) {
 90    ffOptionInitModuleArg(&options->moduleArgs, "");
 91}
 92
 93void ffDestroyVersionOptions(FFVersionOptions* options) {
 94    ffOptionDestroyModuleArg(&options->moduleArgs);
 95}
 96
 97FFModuleBaseInfo ffVersionModuleInfo = {
 98    .name = "Version",
 99    .description = "Print the Fastfetch version and build information",
100    .displayName = {
101        .en = "Version",
102        .ar = "الإصدار",
103        .cs = "Verze",
104        .de = "Version",
105        .es = "Versión",
106        .fi = "Versio",
107        .fr = "Version",
108        .gl = "Versión",
109        .he = "גרסה",
110        .id = "Versi",
111        .it = "Versione",
112        .ja = "バージョン",
113        .ko = "버전",
114        .pl = "Wersja",
115        .pt = "Versão",
116        .ru = "Версия",
117        .tr = "Sürüm",
118        .uk = "Версія",
119        .vi = "Phiên bản",
120        .zh_CN = "版本",
121        .zh_TW = "版本",
122    },
123    .initOptions = (void*) ffInitVersionOptions,
124    .destroyOptions = (void*) ffDestroyVersionOptions,
125    .parseJsonObject = (void*) ffParseVersionJsonObject,
126    .printModule = (void*) ffPrintVersion,
127    .generateJsonResult = (void*) ffGenerateVersionJsonResult,
128    .generateJsonConfig = (void*) ffGenerateVersionJsonConfig,
129    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
130        { "Project name", "project-name" },
131        { "Version", "version" },
132        { "Version tweak", "version-tweak" },
133        { "Build type (debug or release)", "build-type" },
134        { "System name", "sysname" },
135        { "Architecture", "arch" },
136        { "CMake build type when compiling (Debug, Release, RelWithDebInfo, MinSizeRel)", "cmake-built-type" },
137        { "Date time when compiling", "compile-time" },
138        { "Compiler used when compiling", "compiler" },
139        { "Libc used when compiling", "libc" },
140    })),
141    .defaultOrder = 71,
142};