From 24cb83dd18e39f9707b498e71a8f3118b3dbf8a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 20 Jan 2024 13:32:17 +0800 Subject: [PATCH] Bluetooth / Gamepad / Sound: support percentage number formatting --- src/modules/bluetooth/bluetooth.c | 23 +++++++++++------- src/modules/gamepad/gamepad.c | 16 +++++++++---- src/modules/sound/sound.c | 39 ++++++++++++++++++++++++------- src/modules/swap/swap.c | 2 ++ 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/modules/bluetooth/bluetooth.c b/src/modules/bluetooth/bluetooth.c index 91fb46724..0d669af44 100644 --- a/src/modules/bluetooth/bluetooth.c +++ b/src/modules/bluetooth/bluetooth.c @@ -1,3 +1,4 @@ +#include "common/percent.h" #include "common/printing.h" #include "common/jsonconfig.h" #include "detection/bluetooth/bluetooth.h" @@ -11,23 +12,29 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - ffStrbufWriteTo(&device->name, stdout); - if(device->battery > 0) - printf(" (%d%%)", device->battery); + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateCopy(&device->name); - if(!device->connected) - puts(" [disconnected]"); - else - putchar('\n'); + if (device->battery > 0 && device->battery <= 100) + { + if (buffer.length) + ffStrbufAppendC(&buffer, ' '); + ffAppendPercentNum(&buffer, device->battery, 50, 20, buffer.length > 0); + } + + if (!device->connected) + ffStrbufAppendS(&buffer, " [disconnected]"); } else { + FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate(); + ffAppendPercentNum(&percentageStr, device->battery, 50, 20, false); + ffPrintFormat(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_BLUETOOTH_NUM_FORMAT_ARGS, (FFformatarg[]) { {FF_FORMAT_ARG_TYPE_STRBUF, &device->name}, {FF_FORMAT_ARG_TYPE_STRBUF, &device->address}, {FF_FORMAT_ARG_TYPE_STRBUF, &device->type}, - {FF_FORMAT_ARG_TYPE_UINT8, &device->battery} + {FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr} }); } } diff --git a/src/modules/gamepad/gamepad.c b/src/modules/gamepad/gamepad.c index 96afdd4d0..2e639b36f 100644 --- a/src/modules/gamepad/gamepad.c +++ b/src/modules/gamepad/gamepad.c @@ -11,21 +11,27 @@ static void printDevice(FFGamepadOptions* options, const FFGamepadDevice* device { if(options->moduleArgs.outputFormat.length == 0) { - FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateCopy(&device->name); ffPrintLogoAndKey(FF_GAMEPAD_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); + + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateCopy(&device->name); + if (device->battery > 0 && device->battery <= 100) { - ffStrbufAppendC(&buffer, ' '); - ffAppendPercentNum(&buffer, device->battery, 51, 21, true); + if (buffer.length) + ffStrbufAppendC(&buffer, ' '); + ffAppendPercentNum(&buffer, device->battery, 50, 20, buffer.length > 0); } ffStrbufPutTo(&buffer, stdout); } else { + FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate(); + ffAppendPercentNum(&percentageStr, device->battery, 50, 20, false); + ffPrintFormat(FF_GAMEPAD_MODULE_NAME, index, &options->moduleArgs, FF_GAMEPAD_NUM_FORMAT_ARGS, (FFformatarg[]) { {FF_FORMAT_ARG_TYPE_STRBUF, &device->name}, {FF_FORMAT_ARG_TYPE_STRBUF, &device->serial}, - {FF_FORMAT_ARG_TYPE_UINT8, &device->battery}, + {FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr}, }); } } @@ -130,7 +136,7 @@ void ffPrintGamepadHelpFormat(void) ffPrintModuleFormatHelp(FF_GAMEPAD_MODULE_NAME, "{1}", FF_GAMEPAD_NUM_FORMAT_ARGS, (const char* []) { "Name", "Serial number", - "Battery", + "Battery percentage", }); } diff --git a/src/modules/sound/sound.c b/src/modules/sound/sound.c index 39411b5d4..54ea63927 100644 --- a/src/modules/sound/sound.c +++ b/src/modules/sound/sound.c @@ -1,3 +1,4 @@ +#include "common/percent.h" #include "common/printing.h" #include "common/jsonconfig.h" #include "detection/sound/sound.h" @@ -11,27 +12,47 @@ static void printDevice(FFSoundOptions* options, const FFSoundDevice* device, ui if(options->moduleArgs.outputFormat.length == 0) { ffPrintLogoAndKey(FF_SOUND_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - ffStrbufWriteTo(&device->name, stdout); + + FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate(); + if (!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) + ffStrbufAppend(&str, &device->name); if(device->volume != FF_SOUND_VOLUME_UNKNOWN) { - if(device->volume > 0) - printf(" (%d%%)", device->volume); - else - fputs(" (muted)", stdout); + if (instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) + { + if (str.length) + ffStrbufAppendC(&str, ' '); + + ffAppendPercentBar(&str, device->volume, 80, 90); + } + + if (instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) + { + if (str.length) + ffStrbufAppendC(&str, ' '); + + ffAppendPercentNum(&str, device->volume, 80, 90, str.length > 0); + } } - if(device->main && index > 0) - fputs(" (*)", stdout); + if (!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) + { + if (device->main && index > 0) + ffStrbufAppendS(&str, " (*)"); + } - putchar('\n'); + ffStrbufPutTo(&str, stdout); } else { + FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate(); + ffAppendPercentNum(&percentageStr, device->volume, 80, 90, false); + ffPrintFormat(FF_SOUND_MODULE_NAME, index, &options->moduleArgs, FF_SOUND_NUM_FORMAT_ARGS, (FFformatarg[]) { {FF_FORMAT_ARG_TYPE_BOOL, &device->main}, {FF_FORMAT_ARG_TYPE_STRBUF, &device->name}, - {FF_FORMAT_ARG_TYPE_UINT8, &device->volume}, + {FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr}, {FF_FORMAT_ARG_TYPE_STRBUF, &device->identifier} }); } diff --git a/src/modules/swap/swap.c b/src/modules/swap/swap.c index 83c0d86cf..2635c8d00 100644 --- a/src/modules/swap/swap.c +++ b/src/modules/swap/swap.c @@ -42,6 +42,8 @@ void ffPrintSwap(FFSwapOptions* options) } if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) ffStrbufAppendS(&str, "Disabled"); + else + ffAppendPercentNum(&str, 0, 50, 80, str.length > 0); } else {