Skip to content

Commit ce554db

Browse files
committed
Uptime / Battery: add formatted duration string in custom format
1 parent a0174e8 commit ce554db

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/detection/battery/battery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef struct FFBatteryResult
1515
double capacity;
1616
double temperature;
1717
uint32_t cycleCount;
18-
int32_t timeRemaining;
18+
int32_t timeRemaining; // in seconds, -1 if unknown
1919
} FFBatteryResult;
2020

2121
const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results);

src/modules/battery/battery.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
101101
ffPercentAppendBar(&capacityBar, result->capacity, options->percent, &options->moduleArgs);
102102
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
103103
ffTempsAppendNum(result->temperature, &tempStr, options->tempConfig, &options->moduleArgs);
104+
FF_STRBUF_AUTO_DESTROY timeStr = ffStrbufCreate();
105+
if (result->timeRemaining > 0)
106+
ffDurationAppendNum((uint32_t) result->timeRemaining, &timeStr);
104107

105108
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
106109
FF_FORMAT_ARG(result->manufacturer, "manufacturer"),
@@ -117,6 +120,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
117120
FF_FORMAT_ARG(hours, "time-hours"),
118121
FF_FORMAT_ARG(minutes, "time-minutes"),
119122
FF_FORMAT_ARG(seconds, "time-seconds"),
123+
FF_FORMAT_ARG(result->timeRemaining, "time-formatted"),
120124
}));
121125
}
122126
}
@@ -254,6 +258,8 @@ void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc,
254258
yyjson_mut_obj_add_uint(doc, obj, "cycleCount", battery->cycleCount);
255259
if (battery->timeRemaining > 0)
256260
yyjson_mut_obj_add_int(doc, obj, "timeRemaining", battery->timeRemaining);
261+
else
262+
yyjson_mut_obj_add_null(doc, obj, "timeRemaining");
257263
}
258264

259265
FF_LIST_FOR_EACH(FFBatteryResult, battery, results)

src/modules/uptime/uptime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ void ffPrintUptime(FFUptimeOptions* options)
1919
}
2020

2121
uint64_t uptime = result.uptime;
22+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
23+
ffDurationAppendNum((uptime + 500) / 1000, &buffer);
2224

2325
if(options->moduleArgs.outputFormat.length == 0)
2426
{
2527
ffPrintLogoAndKey(FF_UPTIME_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
26-
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
27-
ffDurationAppendNum((uptime + 500) / 1000, &buffer);
28-
2928
ffStrbufPutTo(&buffer, stdout);
3029
}
3130
else
@@ -52,6 +51,7 @@ void ffPrintUptime(FFUptimeOptions* options)
5251
FF_FORMAT_ARG(age.years, "years"),
5352
FF_FORMAT_ARG(age.daysOfYear, "days-of-year"),
5453
FF_FORMAT_ARG(age.yearsFraction, "years-fraction"),
54+
FF_FORMAT_ARG(buffer, "formatted")
5555
}));
5656
}
5757
}

0 commit comments

Comments
 (0)