mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
Percentage: simplify usage of ffAppendPercentBar
This commit is contained in:
+21
-29
@@ -1,17 +1,15 @@
|
||||
#include "common/color.h"
|
||||
#include "fastfetch.h"
|
||||
#include "common/percent.h"
|
||||
#include "common/color.h"
|
||||
#include "util/textModifier.h"
|
||||
|
||||
void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red)
|
||||
void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow)
|
||||
{
|
||||
assert(green <= 100 && yellow <= 100 && red <= 100);
|
||||
assert(green <= 100 && yellow <= 100);
|
||||
|
||||
const FFOptionsDisplay* options = &instance.config.display;
|
||||
|
||||
uint32_t blocksPercent = (uint32_t) (percent / 100.0 * options->barWidth + 0.5);
|
||||
uint32_t blocksGreen = (uint32_t) (green / 100.0 * options->barWidth + 0.5);
|
||||
uint32_t blocksYellow = (uint32_t) (yellow / 100.0 * options->barWidth + 0.5);
|
||||
uint32_t blocksRed = (uint32_t) (red / 100.0 * options->barWidth + 0.5);
|
||||
assert(blocksPercent <= options->barWidth);
|
||||
|
||||
if(options->barBorder)
|
||||
@@ -26,12 +24,14 @@ void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t
|
||||
{
|
||||
if(!options->pipe)
|
||||
{
|
||||
if (i == blocksGreen)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
|
||||
else if (i == blocksYellow)
|
||||
uint32_t section1Begin = (uint32_t) ((green <= yellow ? green : yellow) / 100.0 * options->barWidth + 0.5);
|
||||
uint32_t section2Begin = (uint32_t) ((green > yellow ? green : yellow) / 100.0 * options->barWidth + 0.5);
|
||||
if (i == section2Begin)
|
||||
ffStrbufAppendF(buffer, "\e[%sm", (green > yellow ? FF_COLOR_FG_LIGHT_GREEN : FF_COLOR_FG_LIGHT_RED));
|
||||
else if (i == section1Begin)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
|
||||
else if (i == blocksRed)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
|
||||
else if (i == 0)
|
||||
ffStrbufAppendF(buffer, "\e[%sm", (green <= yellow ? FF_COLOR_FG_GREEN : FF_COLOR_FG_LIGHT_RED));
|
||||
}
|
||||
ffStrbufAppend(buffer, &options->barCharElapsed);
|
||||
}
|
||||
@@ -56,15 +56,6 @@ void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t
|
||||
ffStrbufAppendS(buffer, FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
}
|
||||
|
||||
// if (green < yellow)
|
||||
// [0, green]: print green
|
||||
// (green, yellow]: print yellow
|
||||
// (yellow, 100]: print red
|
||||
//
|
||||
// if (green > yellow)
|
||||
// [green, 100]: print green
|
||||
// [yellow, green): print yellow
|
||||
// [0, yellow): PRINT RED
|
||||
void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses)
|
||||
{
|
||||
assert(green <= 100 && yellow <= 100);
|
||||
@@ -80,23 +71,24 @@ void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t
|
||||
{
|
||||
if(percent != percent)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_BLACK "m");
|
||||
else if(green < yellow)
|
||||
else if(green <= yellow)
|
||||
{
|
||||
if (percent <= green)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
|
||||
else if (percent <= yellow)
|
||||
if (percent > yellow)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
|
||||
else if (percent > green)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
|
||||
else
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (percent >= green)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
|
||||
else if (percent >= yellow)
|
||||
if (percent < yellow)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
|
||||
else if (percent < green)
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_YELLOW "m");
|
||||
else
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_LIGHT_RED "m");
|
||||
ffStrbufAppendS(buffer, "\e[" FF_COLOR_FG_GREEN "m");
|
||||
}
|
||||
}
|
||||
ffStrbufAppendF(buffer, "%.*f%%", options->percentNdigits, percent);
|
||||
|
||||
+11
-1
@@ -10,5 +10,15 @@ enum
|
||||
FF_PERCENTAGE_TYPE_NUM_COLOR_BIT = 1 << 3,
|
||||
};
|
||||
|
||||
void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, uint8_t red);
|
||||
// if (green <= yellow)
|
||||
// [0, green]: print green
|
||||
// (green, yellow]: print yellow
|
||||
// (yellow, 100]: print red
|
||||
//
|
||||
// if (green > yellow)
|
||||
// [green, 100]: print green
|
||||
// [yellow, green): print yellow
|
||||
// [0, yellow): print red
|
||||
|
||||
void ffAppendPercentBar(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow);
|
||||
void ffAppendPercentNum(FFstrbuf* buffer, double percent, uint8_t green, uint8_t yellow, bool parentheses);
|
||||
|
||||
@@ -24,12 +24,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
|
||||
{
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
if(result->capacity <= 20)
|
||||
ffAppendPercentBar(&str, result->capacity, 100, 100, 0);
|
||||
else if(result->capacity <= 50)
|
||||
ffAppendPercentBar(&str, result->capacity, 100, 0, 100);
|
||||
else
|
||||
ffAppendPercentBar(&str, result->capacity, 0, 100, 100);
|
||||
ffAppendPercentBar(&str, result->capacity, 50, 20);
|
||||
}
|
||||
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
@@ -37,7 +32,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
|
||||
ffAppendPercentNum(&str, result->capacity, 51, 21, str.length > 0);
|
||||
ffAppendPercentNum(&str, result->capacity, 50, 20, str.length > 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ void ffPrintBrightness(FFBrightnessOptions* options)
|
||||
|
||||
if (instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(&str, percent, 0, 100, 100);
|
||||
ffAppendPercentBar(&str, percent, 100, 100);
|
||||
}
|
||||
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
@@ -60,7 +60,7 @@ void ffPrintBrightness(FFBrightnessOptions* options)
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
|
||||
ffAppendPercentNum(&str, percent, 10, 10, str.length > 0);
|
||||
ffAppendPercentNum(&str, percent, 100, 100, str.length > 0);
|
||||
}
|
||||
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
|
||||
@@ -52,7 +52,7 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options)
|
||||
if (!options->separate)
|
||||
{
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
ffAppendPercentBar(&str, avgValue, 0, 50, 80);
|
||||
ffAppendPercentBar(&str, avgValue, 50, 80);
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
{
|
||||
if(str.length > 0)
|
||||
|
||||
@@ -59,7 +59,7 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
|
||||
{
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(&str, bytesPercentage, 0, 50, 80);
|
||||
ffAppendPercentBar(&str, bytesPercentage, 50, 80);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ void ffPrintMemory(FFMemoryOptions* options)
|
||||
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(&str, percentage, 0, 50, 80);
|
||||
ffAppendPercentBar(&str, percentage, 50, 80);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ void ffPrintSwap(FFSwapOptions* options)
|
||||
{
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(&str, 0, 0, 50, 80);
|
||||
ffAppendPercentBar(&str, 0, 50, 80);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
|
||||
@@ -47,7 +47,7 @@ void ffPrintSwap(FFSwapOptions* options)
|
||||
{
|
||||
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
{
|
||||
ffAppendPercentBar(&str, percentage, 0, 50, 80);
|
||||
ffAppendPercentBar(&str, percentage, 50, 80);
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user