2021-02-18 22:25:36 +01:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 13:15:01 +02:00
|
|
|
#include "common/printing.h"
|
2022-09-16 11:03:46 +08:00
|
|
|
#include "detection/battery/battery.h"
|
2021-03-03 21:03:12 +01:00
|
|
|
|
2021-04-15 18:59:26 +02:00
|
|
|
#define FF_BATTERY_MODULE_NAME "Battery"
|
|
|
|
|
#define FF_BATTERY_NUM_FORMAT_ARGS 5
|
2021-04-03 21:51:50 +02:00
|
|
|
|
2022-09-25 15:23:00 +08:00
|
|
|
static void printBattery(FFinstance* instance, BatteryResult* result, uint8_t index)
|
2021-12-31 14:12:44 +01:00
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
if(instance->config.battery.outputFormat.length == 0)
|
2021-03-04 20:49:12 +01:00
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
ffPrintLogoAndKey(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.battery.key);
|
2021-03-27 15:39:56 +01:00
|
|
|
|
2021-12-31 14:12:44 +01:00
|
|
|
bool showStatus =
|
|
|
|
|
result->status.length > 0 &&
|
|
|
|
|
ffStrbufIgnCaseCompS(&result->status, "Unknown") != 0;
|
2021-05-30 21:37:27 +02:00
|
|
|
|
2021-12-31 14:12:44 +01:00
|
|
|
if(result->capacity.length > 0)
|
2021-03-19 20:57:07 +01:00
|
|
|
{
|
2021-12-31 14:12:44 +01:00
|
|
|
ffStrbufWriteTo(&result->capacity, stdout);
|
2021-05-30 21:35:04 +02:00
|
|
|
putchar('%');
|
2021-03-27 18:14:19 +01:00
|
|
|
|
2021-05-30 21:37:27 +02:00
|
|
|
if(showStatus)
|
2021-06-26 11:39:42 +02:00
|
|
|
fputs(" [", stdout);
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
2021-05-30 21:35:04 +02:00
|
|
|
|
2021-05-30 21:37:27 +02:00
|
|
|
if(showStatus)
|
2021-06-26 11:39:42 +02:00
|
|
|
{
|
2021-12-31 14:12:44 +01:00
|
|
|
ffStrbufWriteTo(&result->status, stdout);
|
2021-05-30 21:35:04 +02:00
|
|
|
|
2021-12-31 14:12:44 +01:00
|
|
|
if(result->capacity.length > 0)
|
2021-06-26 11:39:42 +02:00
|
|
|
putchar(']');
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-25 15:23:00 +08:00
|
|
|
if(result->temperature == result->temperature) //FF_BATTERY_TEMP_UNSET
|
|
|
|
|
printf(" - %.1f°C", result->temperature);
|
|
|
|
|
|
2021-05-30 21:35:04 +02:00
|
|
|
putchar('\n');
|
2021-02-19 16:23:41 +01:00
|
|
|
}
|
2021-03-01 19:53:20 +01:00
|
|
|
else
|
2021-02-19 16:23:41 +01:00
|
|
|
{
|
2022-06-17 18:15:36 +02:00
|
|
|
ffPrintFormat(instance, FF_BATTERY_MODULE_NAME, index, &instance->config.battery, FF_BATTERY_NUM_FORMAT_ARGS, (FFformatarg[]){
|
2021-12-31 14:12:44 +01:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->manufacturer},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->modelName},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->technology},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->capacity},
|
2022-09-25 15:23:00 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &result->status},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_DOUBLE, &result->temperature},
|
2021-04-15 18:59:26 +02:00
|
|
|
});
|
2021-02-19 16:23:41 +01:00
|
|
|
}
|
2021-03-03 21:03:12 +01:00
|
|
|
}
|
2021-02-18 22:25:36 +01:00
|
|
|
|
2021-03-03 21:03:12 +01:00
|
|
|
void ffPrintBattery(FFinstance* instance)
|
|
|
|
|
{
|
2021-12-31 14:12:44 +01:00
|
|
|
FFlist results;
|
2022-09-16 11:03:46 +08:00
|
|
|
ffListInitA(&results, sizeof(BatteryResult), 0);
|
|
|
|
|
|
|
|
|
|
const char* error = ffDetectBatteryImpl(instance, &results);
|
2021-03-03 21:03:12 +01:00
|
|
|
|
2022-09-16 11:03:46 +08:00
|
|
|
if (error)
|
2021-04-24 13:05:43 +02:00
|
|
|
{
|
2022-09-16 11:03:46 +08:00
|
|
|
ffPrintError(instance, FF_BATTERY_MODULE_NAME, 0, &instance->config.battery, "%s", error);
|
2021-03-03 21:03:12 +01:00
|
|
|
}
|
2022-09-16 11:03:46 +08:00
|
|
|
else
|
2021-04-15 18:59:26 +02:00
|
|
|
{
|
2022-09-16 11:03:46 +08:00
|
|
|
for(uint8_t i = 0; i < (uint8_t) results.length; i++)
|
|
|
|
|
{
|
|
|
|
|
BatteryResult* result = ffListGet(&results, i);
|
|
|
|
|
printBattery(instance, result, i);
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&result->manufacturer);
|
|
|
|
|
ffStrbufDestroy(&result->modelName);
|
|
|
|
|
ffStrbufDestroy(&result->technology);
|
|
|
|
|
ffStrbufDestroy(&result->capacity);
|
|
|
|
|
ffStrbufDestroy(&result->status);
|
|
|
|
|
}
|
|
|
|
|
if(results.length == 0)
|
|
|
|
|
ffPrintError(instance, FF_BATTERY_MODULE_NAME, 0, &instance->config.battery, "No batteries found");
|
2021-04-03 11:39:59 +02:00
|
|
|
}
|
2021-04-24 13:05:43 +02:00
|
|
|
|
2021-12-31 14:12:44 +01:00
|
|
|
ffListDestroy(&results);
|
2021-03-27 18:14:19 +01:00
|
|
|
}
|