Skip to content

Commit c9e3f8d

Browse files
committed
Global: support percent type config in module level
1 parent 97e1b98 commit c9e3f8d

File tree

16 files changed

+109
-54
lines changed

16 files changed

+109
-54
lines changed

src/common/percent.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ void ffPercentAppendBar(FFstrbuf* buffer, double percent, FFPercentageModuleConf
4141
}
4242
else
4343
{
44-
const char* colorGreen = instance.config.display.percentColorGreen.chars;
45-
const char* colorYellow = instance.config.display.percentColorYellow.chars;
46-
const char* colorRed = instance.config.display.percentColorRed.chars;
44+
const char* colorGreen = options->percentColorGreen.chars;
45+
const char* colorYellow = options->percentColorYellow.chars;
46+
const char* colorRed = options->percentColorRed.chars;
47+
48+
FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type;
49+
bool monochrome = !!(percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT);
4750

4851
for (uint32_t i = 0; i < blocksPercent; ++i)
4952
{
5053
if(!options->pipe)
5154
{
52-
if (options->percentType & FF_PERCENTAGE_TYPE_BAR_MONOCHROME_BIT)
55+
if (monochrome)
5356
{
5457
const char* color = NULL;
5558
if (green <= yellow)
@@ -110,8 +113,9 @@ void ffPercentAppendNum(FFstrbuf* buffer, double percent, FFPercentageModuleConf
110113
assert(green <= 100 && yellow <= 100);
111114

112115
const FFOptionsDisplay* options = &instance.config.display;
116+
FFPercentageTypeFlags percentType = config.type == 0 ? options->percentType : config.type;
113117

114-
bool colored = !!(options->percentType & FF_PERCENTAGE_TYPE_NUM_COLOR_BIT);
118+
bool colored = !!(percentType & FF_PERCENTAGE_TYPE_NUM_COLOR_BIT);
115119

116120
if (parentheses)
117121
ffStrbufAppendC(buffer, '(');
@@ -187,6 +191,12 @@ bool ffPercentParseCommandOptions(const char* key, const char* subkey, const cha
187191
return true;
188192
}
189193

194+
if (ffStrEqualsIgnCase(subkey, "type"))
195+
{
196+
config->type = (FFPercentageTypeFlags) ffOptionParseUInt32(key, value);
197+
return true;
198+
}
199+
190200
return false;
191201
}
192202

@@ -225,6 +235,12 @@ bool ffPercentParseJsonObject(const char* key, yyjson_val* value, FFPercentageMo
225235
config->yellow = (uint8_t) num;
226236
}
227237

238+
yyjson_val* typeVal = yyjson_obj_get(value, "type");
239+
if (typeVal)
240+
{
241+
config->type = (FFPercentageTypeFlags) yyjson_get_int(typeVal);
242+
}
243+
228244
return true;
229245
}
230246

@@ -238,4 +254,6 @@ void ffPercentGenerateJsonConfig(yyjson_mut_doc* doc, yyjson_mut_val* module, FF
238254
yyjson_mut_obj_add_uint(doc, percent, "green", config.green);
239255
if (config.yellow != defaultConfig.yellow)
240256
yyjson_mut_obj_add_uint(doc, percent, "yellow", config.yellow);
257+
if (config.type != defaultConfig.type)
258+
yyjson_mut_obj_add_uint(doc, percent, "type", config.type);
241259
}

src/common/percent.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ typedef struct FFPercentageModuleConfig
2020
{
2121
uint8_t green;
2222
uint8_t yellow;
23+
FFPercentageTypeFlags type;
2324
} FFPercentageModuleConfig;
2425

2526
// if (green <= yellow)

src/modules/battery/battery.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,26 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
3939
timeRemaining /= 24;
4040
uint32_t days = timeRemaining;
4141

42+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
43+
4244
if(options->moduleArgs.outputFormat.length == 0)
4345
{
4446
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
4547

4648
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
4749
bool showStatus =
48-
!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT) &&
50+
!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT) &&
4951
result->status.length > 0 &&
5052
ffStrbufIgnCaseCompS(&result->status, "Unknown") != 0;
5153

5254
if(result->capacity >= 0)
5355
{
54-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
56+
if(percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
5557
{
5658
ffPercentAppendBar(&str, result->capacity, options->percent, &options->moduleArgs);
5759
}
5860

59-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
61+
if(percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
6062
{
6163
if(str.length > 0)
6264
ffStrbufAppendC(&str, ' ');
@@ -301,7 +303,7 @@ void ffInitBatteryOptions(FFBatteryOptions* options)
301303
ffOptionInitModuleArg(&options->moduleArgs, "");
302304
options->temp = false;
303305
options->tempConfig = (FFColorRangeConfig) { 60, 80 };
304-
options->percent = (FFPercentageModuleConfig) { 50, 20 };
306+
options->percent = (FFPercentageModuleConfig) { 50, 20, 0 };
305307

306308
#ifdef _WIN32
307309
options->useSetupApi = false;

src/modules/bluetooth/bluetooth.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de
1313
{
1414
ffPrintLogoAndKey(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
1515

16-
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateCopy(&device->name);
16+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
17+
bool showBatteryLevel = device->battery > 0 && device->battery <= 100;
18+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
1719

18-
if (device->battery > 0 && device->battery <= 100)
20+
if (showBatteryLevel && (percentType & FF_PERCENTAGE_TYPE_BAR_BIT))
21+
{
22+
ffPercentAppendBar(&buffer, device->battery, options->percent, &options->moduleArgs);
23+
ffStrbufAppendC(&buffer, ' ');
24+
}
25+
26+
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
27+
ffStrbufAppend(&buffer, &device->name);
28+
29+
if (showBatteryLevel && (percentType & FF_PERCENTAGE_TYPE_NUM_BIT))
1930
{
2031
if (buffer.length)
2132
ffStrbufAppendC(&buffer, ' ');
@@ -202,7 +213,7 @@ void ffInitBluetoothOptions(FFBluetoothOptions* options)
202213
);
203214
ffOptionInitModuleArg(&options->moduleArgs, "");
204215
options->showDisconnected = false;
205-
options->percent = (FFPercentageModuleConfig) { 50, 20 };
216+
options->percent = (FFPercentageModuleConfig) { 50, 20, 0 };
206217
}
207218

208219
void ffDestroyBluetoothOptions(FFBluetoothOptions* options)

src/modules/brightness/brightness.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ void ffPrintBrightness(FFBrightnessOptions* options)
2525
return;
2626
}
2727

28+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
29+
2830
if (options->compact)
2931
{
3032
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
@@ -48,7 +50,7 @@ void ffPrintBrightness(FFBrightnessOptions* options)
4850
uint32_t index = 0;
4951
FF_LIST_FOR_EACH(FFBrightnessResult, item, result)
5052
{
51-
if(options->moduleArgs.key.length == 0)
53+
if (options->moduleArgs.key.length == 0)
5254
{
5355
ffStrbufAppendF(&key, "%s (%s)", FF_BRIGHTNESS_MODULE_NAME, item->name.chars);
5456
}
@@ -64,17 +66,17 @@ void ffPrintBrightness(FFBrightnessOptions* options)
6466

6567
const double percent = (item->current - item->min) / (item->max - item->min) * 100;
6668

67-
if(options->moduleArgs.outputFormat.length == 0)
69+
if (options->moduleArgs.outputFormat.length == 0)
6870
{
6971
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
7072
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
7173

72-
if (instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
74+
if (percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
7375
{
7476
ffPercentAppendBar(&str, percent, options->percent, &options->moduleArgs);
7577
}
7678

77-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
79+
if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
7880
{
7981
if(str.length > 0)
8082
ffStrbufAppendC(&str, ' ');
@@ -237,7 +239,7 @@ void ffInitBrightnessOptions(FFBrightnessOptions* options)
237239
ffOptionInitModuleArg(&options->moduleArgs, "󰯪");
238240

239241
options->ddcciSleep = 10;
240-
options->percent = (FFPercentageModuleConfig) { 100, 100 };
242+
options->percent = (FFPercentageModuleConfig) { 100, 100, 0 };
241243
options->compact = false;
242244
}
243245

src/modules/btrfs/btrfs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void ffInitBtrfsOptions(FFBtrfsOptions* options)
245245
ffGenerateBtrfsJsonConfig
246246
);
247247
ffOptionInitModuleArg(&options->moduleArgs, "󱑛");
248-
options->percent = (FFPercentageModuleConfig) { 50, 80 };
248+
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
249249
}
250250

251251
void ffDestroyBtrfsOptions(FFBtrfsOptions* options)

src/modules/cpuusage/cpuusage.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options)
4444
}
4545
double avgValue = sumValue / (double) valueCount;
4646

47+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
48+
4749
if(options->moduleArgs.outputFormat.length == 0)
4850
{
4951
ffPrintLogoAndKey(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
5052

5153
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
5254
if (!options->separate)
5355
{
54-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
56+
if(percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
5557
ffPercentAppendBar(&str, avgValue, options->percent, &options->moduleArgs);
56-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
58+
if(percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
5759
{
5860
if(str.length > 0)
5961
ffStrbufAppendC(&str, ' ');
@@ -214,7 +216,7 @@ void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
214216
);
215217
ffOptionInitModuleArg(&options->moduleArgs, "󰓅");
216218
options->separate = false;
217-
options->percent = (FFPercentageModuleConfig) { 50, 80 };
219+
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
218220
options->waitTime = 200;
219221
}
220222

src/modules/disk/disk.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
5151
ffParseSize(disk->bytesTotal, &totalPretty);
5252

5353
double bytesPercentage = disk->bytesTotal > 0 ? (double) disk->bytesUsed / (double) disk->bytesTotal * 100.0 : 0;
54+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
5455

5556
if(options->moduleArgs.outputFormat.length == 0)
5657
{
@@ -60,16 +61,16 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
6061

6162
if(disk->bytesTotal > 0)
6263
{
63-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
64+
if(percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
6465
{
6566
ffPercentAppendBar(&str, bytesPercentage, options->percent, &options->moduleArgs);
6667
ffStrbufAppendC(&str, ' ');
6768
}
6869

69-
if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
70+
if(!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
7071
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
7172

72-
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
73+
if(percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
7374
{
7475
ffPercentAppendNum(&str, bytesPercentage, options->percent, str.length > 0, &options->moduleArgs);
7576
ffStrbufAppendC(&str, ' ');
@@ -78,7 +79,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
7879
else
7980
ffStrbufAppendS(&str, "Unknown ");
8081

81-
if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
82+
if(!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
8283
{
8384
if(disk->filesystem.length)
8485
ffStrbufAppendF(&str, "- %s ", disk->filesystem.chars);
@@ -473,7 +474,7 @@ void ffInitDiskOptions(FFDiskOptions* options)
473474
ffStrbufInit(&options->folders);
474475
options->showTypes = FF_DISK_VOLUME_TYPE_REGULAR_BIT | FF_DISK_VOLUME_TYPE_EXTERNAL_BIT | FF_DISK_VOLUME_TYPE_READONLY_BIT;
475476
options->calcType = FF_DISK_CALC_TYPE_FREE;
476-
options->percent = (FFPercentageModuleConfig) { 50, 80 };
477+
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
477478
}
478479

479480
void ffDestroyDiskOptions(FFDiskOptions* options)

src/modules/gamepad/gamepad.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ static void printDevice(FFGamepadOptions* options, const FFGamepadDevice* device
1313
{
1414
ffPrintLogoAndKey(FF_GAMEPAD_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
1515

16-
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateCopy(&device->name);
16+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
17+
bool showBatteryLevel = device->battery > 0 && device->battery <= 100;
18+
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
1719

18-
if (device->battery > 0 && device->battery <= 100)
20+
if (showBatteryLevel && (percentType & FF_PERCENTAGE_TYPE_BAR_BIT))
21+
{
22+
ffPercentAppendBar(&buffer, device->battery, options->percent, &options->moduleArgs);
23+
ffStrbufAppendC(&buffer, ' ');
24+
}
25+
26+
if (!(percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
27+
ffStrbufAppend(&buffer, &device->name);
28+
29+
if (showBatteryLevel && (percentType & FF_PERCENTAGE_TYPE_NUM_BIT))
1930
{
2031
if (buffer.length)
2132
ffStrbufAppendC(&buffer, ' ');
@@ -160,7 +171,7 @@ void ffInitGamepadOptions(FFGamepadOptions* options)
160171
ffGenerateGamepadJsonConfig
161172
);
162173
ffOptionInitModuleArg(&options->moduleArgs, "󰺵");
163-
options->percent = (FFPercentageModuleConfig) { 50, 20 };
174+
options->percent = (FFPercentageModuleConfig) { 50, 20, 0 };
164175
}
165176

166177
void ffDestroyGamepadOptions(FFGamepadOptions* options)

src/modules/gpu/gpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ void ffInitGPUOptions(FFGPUOptions* options)
439439
options->temp = false;
440440
options->hideType = FF_GPU_TYPE_UNKNOWN;
441441
options->tempConfig = (FFColorRangeConfig) { 60, 80 };
442-
options->percent = (FFPercentageModuleConfig) { 50, 80 };
442+
options->percent = (FFPercentageModuleConfig) { 50, 80, 0 };
443443
}
444444

445445
void ffDestroyGPUOptions(FFGPUOptions* options)

0 commit comments

Comments
 (0)