Files
fastfetch/src/modules/version/version.c
T

151 lines
5.6 KiB
C
Raw Normal View History

2023-08-21 09:19:16 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "detection/libc/libc.h"
2023-08-21 09:19:16 +08:00
#include "detection/version/version.h"
#include "modules/version/version.h"
#include "util/stringUtils.h"
2024-07-02 10:12:26 +08:00
#define FF_VERSION_NUM_FORMAT_ARGS 10
2023-08-21 09:19:16 +08:00
void ffPrintVersion(FFVersionOptions* options)
{
2024-07-02 10:12:26 +08:00
FFVersionResult* result = &ffVersionResult;
2023-08-21 09:19:16 +08:00
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2024-07-02 10:12:26 +08:00
printf("%s %s%s%s (%s)\n", result->projectName, result->version, result->versionTweak, result->debugMode ? "-debug" : "", result->architecture);
2023-08-21 09:19:16 +08:00
}
else
{
FFLibcResult libcResult;
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
if (!ffDetectLibc(&libcResult))
{
ffStrbufSetS(&buf, libcResult.name);
if (libcResult.version)
{
ffStrbufAppendC(&buf, ' ');
ffStrbufAppendS(&buf, libcResult.version);
}
}
FF_PRINT_FORMAT_CHECKED(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_VERSION_NUM_FORMAT_ARGS, ((FFformatarg[]){
2024-07-02 10:12:26 +08:00
{FF_FORMAT_ARG_TYPE_STRING, result->projectName, "project-name"},
{FF_FORMAT_ARG_TYPE_STRING, result->version, "version"},
{FF_FORMAT_ARG_TYPE_STRING, result->versionTweak, "version-tweak"},
{FF_FORMAT_ARG_TYPE_STRING, result->debugMode ? "debug" : "release", "build-type"},
{FF_FORMAT_ARG_TYPE_STRING, result->sysName, "sysname"},
{FF_FORMAT_ARG_TYPE_STRING, result->architecture, "arch"},
{FF_FORMAT_ARG_TYPE_STRING, result->cmakeBuiltType, "cmake-built-type"},
{FF_FORMAT_ARG_TYPE_STRING, result->compileTime, "compile-time"},
{FF_FORMAT_ARG_TYPE_STRING, result->compiler, "compiler"},
2024-05-27 16:41:37 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &buf, "libc-used"},
}));
2023-08-21 09:19:16 +08:00
}
}
bool ffParseVersionCommandOptions(FFVersionOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_VERSION_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module)
{
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
{
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
ffPrintError(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
2023-08-21 09:19:16 +08:00
}
}
2023-09-12 20:36:00 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateVersionJsonConfig(FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyVersionOptions))) FFVersionOptions defaultOptions;
ffInitVersionOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateVersionJsonResult(FF_MAYBE_UNUSED FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-12 20:36:00 +08:00
{
2024-07-02 10:12:26 +08:00
FFVersionResult* result = &ffVersionResult;
2023-09-12 20:36:00 +08:00
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
2024-07-02 10:12:26 +08:00
yyjson_mut_obj_add_str(doc, obj, "projectName", result->projectName);
yyjson_mut_obj_add_str(doc, obj, "sysName", result->sysName);
yyjson_mut_obj_add_str(doc, obj, "architecture", result->architecture);
yyjson_mut_obj_add_str(doc, obj, "version", result->version);
yyjson_mut_obj_add_str(doc, obj, "versionTweak", result->versionTweak);
yyjson_mut_obj_add_str(doc, obj, "cmakeBuiltType", result->cmakeBuiltType);
yyjson_mut_obj_add_str(doc, obj, "compileTime", result->compileTime);
yyjson_mut_obj_add_str(doc, obj, "compiler", result->compiler);
yyjson_mut_obj_add_bool(doc, obj, "debugMode", result->debugMode);
FFLibcResult libcResult;
if (ffDetectLibc(&libcResult))
{
yyjson_mut_obj_add_null(doc, obj, "libc");
}
else
{
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreateS(libcResult.name);
if (libcResult.version)
{
ffStrbufAppendC(&buf, ' ');
ffStrbufAppendS(&buf, libcResult.version);
}
yyjson_mut_obj_add_strbuf(doc, obj, "libc", &buf);
}
2023-09-12 20:36:00 +08:00
}
2023-10-07 13:40:40 +08:00
void ffPrintVersionHelpFormat(void)
{
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_VERSION_MODULE_NAME, "{1} {2}{3} ({5})", FF_VERSION_NUM_FORMAT_ARGS, ((const char* []) {
2024-05-27 16:41:37 +08:00
"Project name - name",
"Version - version",
"Version tweak - version-tweak",
"Build type (debug or release) - build-type",
2024-07-02 10:12:26 +08:00
"System name - sysname",
2024-05-27 16:41:37 +08:00
"Architecture - arch",
"CMake build type when compiling (Debug, Release, RelWithDebInfo, MinSizeRel) - cmake-built-type",
"Date time when compiling - compile-time",
"Compiler used when compiling - compiler",
"Libc used when compiling - libc",
}));
2023-10-07 13:40:40 +08:00
}
2023-10-23 13:32:18 +08:00
void ffInitVersionOptions(FFVersionOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_VERSION_MODULE_NAME,
"Print Fastfetch version",
ffParseVersionCommandOptions,
ffParseVersionJsonObject,
ffPrintVersion,
ffGenerateVersionJsonResult,
ffPrintVersionHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateVersionJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
void ffDestroyVersionOptions(FFVersionOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}