mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 00:03:58 +01:00
Uptime: support JSON config
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFUptimeOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFUptimeOptions;
|
||||
@@ -0,0 +1,121 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/uptime/uptime.h"
|
||||
#include "modules/uptime/uptime.h"
|
||||
|
||||
#define FF_UPTIME_NUM_FORMAT_ARGS 4
|
||||
|
||||
void ffPrintUptime(FFinstance* instance, FFUptimeOptions* options)
|
||||
{
|
||||
uint64_t uptime;
|
||||
|
||||
const char* error = ffDetectUptime(&uptime);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t days = (uint32_t) uptime / 86400;
|
||||
uint32_t hours = (uint32_t) (uptime - (days * 86400)) / 3600;
|
||||
uint32_t minutes = (uint32_t) (uptime - (days * 86400) - (hours * 3600)) / 60;
|
||||
uint32_t seconds = (uint32_t) uptime - (days * 86400) - (hours * 3600) - (minutes * 60);
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
|
||||
if(days == 0 && hours == 0 && minutes == 0)
|
||||
{
|
||||
printf("%u seconds\n", seconds);
|
||||
return;
|
||||
}
|
||||
|
||||
if(days > 0)
|
||||
{
|
||||
printf("%u day", days);
|
||||
|
||||
if(days > 1)
|
||||
putchar('s');
|
||||
|
||||
if(days >= 100)
|
||||
fputs("(!)", stdout);
|
||||
|
||||
if(hours > 0 || minutes > 0)
|
||||
fputs(", ", stdout);
|
||||
}
|
||||
|
||||
if(hours > 0)
|
||||
{
|
||||
printf("%u hour", hours);
|
||||
|
||||
if(hours > 1)
|
||||
putchar('s');
|
||||
|
||||
if(minutes > 0)
|
||||
fputs(", ", stdout);
|
||||
}
|
||||
|
||||
if(minutes > 0)
|
||||
{
|
||||
printf("%u min", minutes);
|
||||
|
||||
if(minutes > 1)
|
||||
putchar('s');
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_UPTIME_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &days},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &hours},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &minutes},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &seconds}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitUptimeOptions(FFUptimeOptions* options)
|
||||
{
|
||||
options->moduleName = FF_UPTIME_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseUptimeCommandOptions(FFUptimeOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_UPTIME_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyUptimeOptions(FFUptimeOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseUptimeJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFUptimeOptions __attribute__((__cleanup__(ffDestroyUptimeOptions))) options;
|
||||
ffInitUptimeOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_UPTIME_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintUptime(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include "modules/title/option.h"
|
||||
|
||||
#define FF_UPTIME_MODULE_NAME "Uptime"
|
||||
|
||||
void ffPrintUptime(FFinstance* instance, FFUptimeOptions* options);
|
||||
void ffInitUptimeOptions(FFUptimeOptions* options);
|
||||
bool ffParseUptimeCommandOptions(FFUptimeOptions* options, const char* key, const char* value);
|
||||
void ffDestroyUptimeOptions(FFUptimeOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseUptimeJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
Reference in New Issue
Block a user