summaryrefslogtreecommitdiffstats
path: root/src/modules/version/version.c
blob: dcc4bbef12400784cf8084db01ac4794b2977001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/strutil.h"
#include "detection/libc/libc.h"
#include "detection/version/version.h"
#include "modules/version/version.h"

bool ffPrintVersion(FFVersionOptions* options) {
    FFVersionResult* result = &ffVersionResult;

    if (options->moduleArgs.outputFormat.length == 0) {
        ffPrintLogoAndKey(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
        printf("%s %s%s%s (%s)\n", result->projectName, result->version, result->versionTweak, result->debugMode ? "-debug" : "", result->architecture);
    } 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);
            }
        }

        const char* buildType = result->debugMode ? "debug" : "release";
        FF_PRINT_FORMAT_CHECKED(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
                                                                                                            FF_ARG(result->projectName, "project-name"),
                                                                                                            FF_ARG(result->version, "version"),
                                                                                                            FF_ARG(result->versionTweak, "version-tweak"),
                                                                                                            FF_ARG(buildType, "build-type"),
                                                                                                            FF_ARG(result->sysName, "sysname"),
                                                                                                            FF_ARG(result->architecture, "arch"),
                                                                                                            FF_ARG(result->cmakeBuiltType, "cmake-built-type"),
                                                                                                            FF_ARG(result->compileTime, "compile-time"),
                                                                                                            FF_ARG(result->compiler, "compiler"),
                                                                                                            FF_ARG(buf, "libc"),
                                                                                                        }));
    }

    return true;
}

void ffParseVersionJsonObject(FFVersionOptions* options, yyjson_val* module) {
    yyjson_val *key, *val;
    size_t idx, max;
    yyjson_obj_foreach (module, idx, max, key, val) {
        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
            continue;
        }

        ffPrintError(FF_VERSION_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
    }
}

void ffGenerateVersionJsonConfig(FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
}

bool ffGenerateVersionJsonResult(FF_A_UNUSED FFVersionOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    FFVersionResult* result = &ffVersionResult;

    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
    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, "versionGit", result->versionGit);
    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);
    }

    return true;
}

void ffInitVersionOptions(FFVersionOptions* options) {
    ffOptionInitModuleArg(&options->moduleArgs, "");
}

void ffDestroyVersionOptions(FFVersionOptions* options) {
    ffOptionDestroyModuleArg(&options->moduleArgs);
}

FFModuleBaseInfo ffVersionModuleInfo = {
    .name = FF_VERSION_MODULE_NAME,
    .description = "Print the Fastfetch version and build information",
    .initOptions = (void*) ffInitVersionOptions,
    .destroyOptions = (void*) ffDestroyVersionOptions,
    .parseJsonObject = (void*) ffParseVersionJsonObject,
    .printModule = (void*) ffPrintVersion,
    .generateJsonResult = (void*) ffGenerateVersionJsonResult,
    .generateJsonConfig = (void*) ffGenerateVersionJsonConfig,
    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
        { "Project name", "project-name" },
        { "Version", "version" },
        { "Version tweak", "version-tweak" },
        { "Build type (debug or release)", "build-type" },
        { "System name", "sysname" },
        { "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" },
    }))
};