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

107 lines
4.1 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/duration.h"
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/time.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2023-04-25 19:57:27 +08:00
#include "detection/uptime/uptime.h"
#include "modules/uptime/uptime.h"
2026-03-29 09:28:49 +08:00
bool ffPrintUptime(FFUptimeOptions* options) {
2025-04-29 10:20:23 +08:00
FFUptimeResult result = {};
2023-04-25 19:57:27 +08:00
2023-09-08 11:08:29 +08:00
const char* error = ffDetectUptime(&result);
2023-04-25 19:57:27 +08:00
2026-03-29 09:28:49 +08:00
if (error) {
ffPrintError(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2023-04-25 19:57:27 +08:00
}
2023-09-08 11:08:29 +08:00
uint64_t uptime = result.uptime;
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
ffDurationAppendNum((uptime + 500) / 1000, &buffer);
2023-09-08 11:08:29 +08:00
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2024-10-10 10:33:07 +08:00
ffStrbufPutTo(&buffer, stdout);
2026-03-29 09:28:49 +08:00
} else {
2025-04-29 10:20:23 +08:00
uint32_t milliseconds = (uint32_t) (uptime % 1000);
uptime /= 1000;
uint32_t seconds = (uint32_t) (uptime % 60);
uptime /= 60;
uint32_t minutes = (uint32_t) (uptime % 60);
uptime /= 60;
uint32_t hours = (uint32_t) (uptime % 24);
uptime /= 24;
uint32_t days = (uint32_t) uptime;
FFTimeGetAgeResult age = ffTimeGetAge(result.bootTime, ffTimeGetNow());
2026-03-30 10:27:23 +08:00
FF_PRINT_FORMAT_CHECKED(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) { FF_ARG(days, "days"), FF_ARG(hours, "hours"), FF_ARG(minutes, "minutes"), FF_ARG(seconds, "seconds"), FF_ARG(milliseconds, "milliseconds"), { FF_ARG_TYPE_STRING, ffTimeToShortStr(result.bootTime), "boot-time" }, FF_ARG(age.years, "years"), FF_ARG(age.daysOfYear, "days-of-year"), FF_ARG(age.yearsFraction, "years-fraction"), FF_ARG(buffer, "formatted") }));
2023-04-25 19:57:27 +08:00
}
return true;
2023-04-25 19:57:27 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseUptimeJsonObject(FFUptimeOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2023-08-17 16:09:41 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2023-04-25 19:57:27 +08:00
ffPrintError(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-04-25 19:57:27 +08:00
}
}
2023-09-08 14:25:17 +08:00
2026-03-29 09:28:49 +08:00
void ffGenerateUptimeJsonConfig(FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-24 14:41:29 +08:00
}
2026-04-09 16:20:59 +08:00
bool ffGenerateUptimeJsonResult(FF_A_UNUSED FFUptimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2023-09-08 14:25:17 +08:00
FFUptimeResult result;
const char* error = ffDetectUptime(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2023-09-08 14:25:17 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2023-09-08 14:25:17 +08:00
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_uint(doc, obj, "uptime", result.uptime);
yyjson_mut_obj_add_strcpy(doc, obj, "bootTime", ffTimeToFullStr(result.bootTime));
return true;
2023-09-08 14:25:17 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitUptimeOptions(FFUptimeOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
}
2026-03-29 09:28:49 +08:00
void ffDestroyUptimeOptions(FFUptimeOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffUptimeModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_UPTIME_MODULE_NAME,
2026-04-16 18:27:11 +08:00
.description = "Print how long the system has been running",
.initOptions = (void*) ffInitUptimeOptions,
.destroyOptions = (void*) ffDestroyUptimeOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseUptimeJsonObject,
.printModule = (void*) ffPrintUptime,
.generateJsonResult = (void*) ffGenerateUptimeJsonResult,
.generateJsonConfig = (void*) ffGenerateUptimeJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Days after boot", "days" },
{ "Hours after boot", "hours" },
{ "Minutes after boot", "minutes" },
{ "Seconds after boot", "seconds" },
{ "Milliseconds after boot", "milliseconds" },
{ "Boot time in local timezone", "boot-time" },
{ "Years integer after boot", "years" },
{ "Days of year after boot", "days-of-year" },
{ "Years fraction after boot", "years-fraction" },
{ "Formatted uptime", "formatted" },
}))
};