Files
fastfetch/src/modules/os.c
T

139 lines
4.2 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include "common/printing.h"
2022-09-04 13:33:18 +02:00
#include "detection/os/os.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_MODULE_NAME "OS"
#define FF_OS_NUM_FORMAT_ARGS 12
2022-07-21 11:33:19 +02:00
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
2022-09-04 13:56:14 +02:00
ffStrbufAppend(result, &os->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
2022-07-21 11:33:19 +02:00
if(ffStrbufFirstIndex(result, &os->architecture) == result->length)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->architecture);
}
}
static void buildOutputNixOS(const FFOSResult* os, FFstrbuf* result)
{
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(os->architecture.length > 0)
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->architecture);
}
}
void ffPrintOS(FFinstance* instance)
{
const FFOSResult* os = ffDetectOS(instance);
2022-09-04 13:56:14 +02:00
if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0 && os->systemName.length == 0)
2022-07-21 11:33:19 +02:00
{
ffPrintError(instance, FF_OS_MODULE_NAME, 0, &instance->config.os, "Could not detect OS");
return;
2021-07-12 19:01:31 +02:00
}
2021-04-15 18:59:26 +02:00
2022-06-17 18:15:36 +02:00
if(instance->config.os.outputFormat.length == 0)
2022-06-04 22:43:57 +02:00
{
2022-07-21 11:33:19 +02:00
FFstrbuf result;
ffStrbufInit(&result);
if(ffStrbufIgnCaseCompS(&os->id, "nixos") == 0)
buildOutputNixOS(os, &result);
else
buildOutputDefault(os, &result);
2022-06-17 18:15:36 +02:00
ffPrintLogoAndKey(instance, FF_OS_MODULE_NAME, 0, &instance->config.os.key);
2022-07-21 11:33:19 +02:00
ffStrbufPutTo(&result, stdout);
ffStrbufDestroy(&result);
2022-06-04 22:43:57 +02:00
}
else
{
2022-06-17 18:15:36 +02:00
ffPrintFormat(instance, FF_OS_MODULE_NAME, 0, &instance->config.os, FF_OS_NUM_FORMAT_ARGS, (FFformatarg[]){
2022-07-21 11:33:19 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &os->systemName},
{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, &os->architecture}
2022-06-04 22:43:57 +02:00
});
}
2021-03-27 18:14:19 +01:00
}