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

200 lines
6.5 KiB
C
Raw Normal View History

#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-07 14:56:13 +08:00
#include "common/option.h"
2022-09-04 13:33:18 +02:00
#include "detection/os/os.h"
2023-03-07 14:56:13 +08:00
#include "modules/os/os.h"
#include "util/stringUtils.h"
2021-02-18 22:25:36 +01:00
2022-07-21 11:33:19 +02:00
#include <ctype.h>
2021-04-15 18:59:26 +02:00
#define FF_OS_NUM_FORMAT_ARGS 12
static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result)
2021-02-18 22:25:36 +01:00
{
2021-07-12 19:01:31 +02:00
//Create the basic output
2022-07-21 11:33:19 +02:00
if(os->name.length > 0)
ffStrbufAppend(result, &os->name);
else if(os->prettyName.length > 0)
2022-09-04 13:56:14 +02:00
ffStrbufAppend(result, &os->prettyName);
2022-07-21 11:33:19 +02:00
else if(os->id.length > 0)
ffStrbufAppend(result, &os->id);
2021-07-12 19:01:31 +02:00
else
ffStrbufAppend(result, &instance.state.platform.systemName);
2021-07-12 19:01:31 +02:00
2022-09-21 14:52:07 +08:00
#ifdef __APPLE__
if(os->codename.length > 0)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->codename);
}
#endif
2021-07-12 19:01:31 +02:00
//Append version if it is missing
2022-07-21 11:33:19 +02:00
if(os->versionID.length > 0 && ffStrbufFirstIndex(result, &os->versionID) == result->length)
2021-05-05 21:12:42 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->versionID);
2021-04-15 18:59:26 +02:00
}
2022-07-21 11:33:19 +02:00
else if(os->versionID.length == 0 && os->version.length > 0 && ffStrbufFirstIndex(result, &os->version) == result->length)
2021-04-15 18:59:26 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->version);
2021-04-15 18:59:26 +02:00
}
2021-05-05 21:12:42 +02:00
#ifdef __APPLE__
if(os->buildID.length > 0)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->buildID);
}
#endif
2021-07-12 19:01:31 +02:00
//Append variant if it is missing
2022-07-21 11:33:19 +02:00
if(os->variant.length > 0 && ffStrbufFirstIndex(result, &os->variant) == result->length)
2021-05-05 21:12:42 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendS(result, " (");
ffStrbufAppend(result, &os->variant);
ffStrbufAppendC(result, ')');
2021-05-05 21:12:42 +02:00
}
2022-07-21 11:33:19 +02:00
else if(os->variant.length == 0 && os->variantID.length > 0 && ffStrbufFirstIndex(result, &os->variantID) == result->length)
2021-04-15 18:59:26 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendS(result, " (");
ffStrbufAppend(result, &os->variantID);
ffStrbufAppendC(result, ')');
2021-03-19 20:57:07 +01:00
}
2021-02-22 16:52:51 +01:00
2021-07-12 19:01:31 +02:00
//Append architecture if it is missing
if(ffStrbufFirstIndex(result, &instance.state.platform.systemArchitecture) == result->length)
2022-07-21 11:33:19 +02:00
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &instance.state.platform.systemArchitecture);
2022-07-21 11:33:19 +02:00
}
}
static void buildOutputNixOS(const FFOSResult* os, FFstrbuf* result)
2022-07-21 11:33:19 +02:00
{
ffStrbufAppendS(result, "NixOS");
2021-07-12 19:01:31 +02:00
2022-07-21 11:33:19 +02:00
if(os->buildID.length > 0)
2021-07-12 19:01:31 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->buildID);
}
if(os->codename.length > 0)
{
ffStrbufAppendS(result, " (");
ffStrbufAppendC(result, (char) toupper(os->codename.chars[0]));
ffStrbufAppendS(result, os->codename.chars + 1);
ffStrbufAppendC(result, ')');
}
if(instance.state.platform.systemArchitecture.length > 0)
2022-07-21 11:33:19 +02:00
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &instance.state.platform.systemArchitecture);
2022-07-21 11:33:19 +02:00
}
}
void ffPrintOS(FFOSOptions* options)
2022-07-21 11:33:19 +02:00
{
const FFOSResult* os = ffDetectOS();
2022-07-21 11:33:19 +02:00
2023-01-15 15:57:26 +01:00
if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0)
2022-07-21 11:33:19 +02:00
{
ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, "Could not detect OS");
2022-07-21 11:33:19 +02:00
return;
2021-07-12 19:01:31 +02:00
}
2021-04-15 18:59:26 +02:00
2023-03-07 14:56:13 +08:00
if(options->moduleArgs.outputFormat.length == 0)
2022-06-04 22:43:57 +02:00
{
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
2022-07-21 11:33:19 +02:00
if(ffStrbufIgnCaseCompS(&os->id, "nixos") == 0)
buildOutputNixOS(os, &result);
2022-07-21 11:33:19 +02:00
else
buildOutputDefault(os, &result);
2022-07-21 11:33:19 +02:00
ffPrintLogoAndKey(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2022-07-21 11:33:19 +02:00
ffStrbufPutTo(&result, stdout);
2022-06-04 22:43:57 +02:00
}
else
{
ffPrintFormat(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_OS_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.systemName},
2022-07-21 11:33:19 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &os->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->prettyName},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->id},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->idLike},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->variant},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->variantID},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->version},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->versionID},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->codename},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->buildID},
{FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.systemArchitecture}
2022-06-04 22:43:57 +02:00
});
}
2021-03-27 18:14:19 +01:00
}
2023-03-07 14:56:13 +08:00
void ffInitOSOptions(FFOSOptions* options)
{
2023-09-07 09:36:04 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_OS_MODULE_NAME, ffParseOSCommandOptions, ffParseOSJsonObject, ffPrintOS, ffGenerateOSJson);
2023-03-07 14:56:13 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseOSCommandOptions(FFOSOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_OS_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyOSOptions(FFOSOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2023-03-09 16:14:08 +08:00
2023-08-17 10:56:54 +08:00
void ffParseOSJsonObject(FFOSOptions* options, yyjson_val* module)
2023-03-09 16:14:08 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-03-09 16:14:08 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-03-09 16:14:08 +08:00
}
}
2023-09-07 09:36:04 +08:00
void ffGenerateOSJson(FF_MAYBE_UNUSED FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
const FFOSResult* os = ffDetectOS();
if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "Could not detect OS");
return;
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "buildID", &os->buildID);
yyjson_mut_obj_add_strbuf(doc, obj, "codename", &os->codename);
yyjson_mut_obj_add_strbuf(doc, obj, "id", &os->id);
yyjson_mut_obj_add_strbuf(doc, obj, "idLike", &os->idLike);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &os->name);
yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &os->prettyName);
yyjson_mut_obj_add_strbuf(doc, obj, "variant", &os->variant);
yyjson_mut_obj_add_strbuf(doc, obj, "variantID", &os->variantID);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &os->version);
yyjson_mut_obj_add_strbuf(doc, obj, "versionID", &os->versionID);
}