Skip to content

Commit

Permalink
config: add percentage-colors + fix invert of percentage tag colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Toni500github committed Sep 20, 2024
1 parent 5f9d922 commit 5418c8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
9 changes: 9 additions & 0 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Config

// config file
std::vector<std::string> layout;
std::vector<std::string> percentage_colors;
std::string source_path;
std::string font;
std::string data_dir;
Expand Down Expand Up @@ -234,6 +235,14 @@ magenta = "\e[1;35m"
cyan = "\e[1;36m"
white = "\e[1;37m"
# Colors to be used in percentage tag and modules members.
# They are used as if you're using the color tag.
# It's an array just for "convinience"
# 1st color for good
# 2nd color for normal
# 3rd color for bad
percentage-colors = ["green", "yellow", "red"]
# $<os.uptime> config
[os.uptime]
# how to display the name of the uptime
Expand Down
7 changes: 7 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
// clang-format off
// Idk but with `this->` looks more readable
this->layout = this->getValueArrayStr("config.layout", {});
this->percentage_colors = this->getValueArrayStr("config.percentage-colors", {"green", "yellow", "red"});
this->gui = this->getValue<bool>("gui.enable", false);
this->slow_query_warnings= this->getValue<bool>("config.slow-query-warnings", false);
this->sep_reset_after = this->getValue<bool>("config.sep-reset-after", false);
Expand Down Expand Up @@ -84,6 +85,12 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
colors.gui_magenta = this->getThemeValue("gui.magenta", "!#ff11cc");
colors.gui_white = this->getThemeValue("gui.white", "!#ffffff");

if (this->percentage_colors.size() < 3)
{
warn("the config array percentage-colors doesn't have 3 colors for being used in percentage tag and modules\n"
"backing up to green, yellow and red");
this->percentage_colors = {"green", "yellow", "red"};
}
}

std::string Config::getThemeValue(const std::string_view value, const std::string_view fallback) const
Expand Down
22 changes: 12 additions & 10 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ static std::string get_and_color_percentage(const float& n1, const float& n2, sy
std::string_view color;
if (!invert)
{
if (result <= 35)
color = "${green}";
if (result <= 45)
color = "${" + config.percentage_colors.at(0) + "}";
else if (result <= 80)
color = "${yellow}";
color = "${" + config.percentage_colors.at(1) + "}";
else
color = "${red}";
color = "${" + config.percentage_colors.at(2) + "}";
}
else
{
if (result <= 35)
color = "${red}";
if (result <= 45)
color = "${" + config.percentage_colors.at(2) + "}";
else if (result <= 80)
color = "${yellow}";
color = "${" + config.percentage_colors.at(1) + "}";
else
color = "${green}";
color = "${" + config.percentage_colors.at(0) + "}";
}

std::string _;
Expand Down Expand Up @@ -306,11 +306,13 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
if (comma_pos == std::string::npos)
die("percentage tag '{}' doesn't have a comma for separating the 2 numbers", command);

const bool invert = (command.front() == '!');

std::string _;
const float& n1 = std::stof(parse(command.substr(0, comma_pos), systemInfo, _, config, colors, parsingLayout));
const float& n1 = std::stof(parse(command.substr(invert ? 1 : 0, comma_pos), systemInfo, _, config, colors, parsingLayout));
const float& n2 = std::stof(parse(command.substr(comma_pos + 1), systemInfo, _, config, colors, parsingLayout));

output.replace(dollarSignIndex, taglen, get_and_color_percentage(n1, n2, systemInfo, config, colors, parsingLayout, (command.front() == '!')));
output.replace(dollarSignIndex, taglen, get_and_color_percentage(n1, n2, systemInfo, config, colors, parsingLayout, invert));
break;
}

Expand Down

0 comments on commit 5418c8b

Please sign in to comment.