Skip to content

Commit

Permalink
config: add --color as a config setting (alias-colors)
Browse files Browse the repository at this point in the history
and also fix ascii art being fucked up if couldn't parse the colors
  • Loading branch information
Toni500github committed Sep 28, 2024
1 parent 5a2bece commit a120a87
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
10 changes: 8 additions & 2 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Config
// config file
std::vector<std::string> layout;
std::vector<std::string> percentage_colors;
std::vector<std::string> colors_name, colors_value;
std::string source_path;
std::string font;
std::string data_dir;
Expand Down Expand Up @@ -70,12 +71,11 @@ class Config
bool m_disable_source = false;
bool m_display_distro = true;
bool m_print_logo_only = false;
std::vector<std::string> m_arg_colors_name, m_arg_colors_value;

void loadConfigFile(const std::string_view filename, colors_t& colors);
std::string getThemeValue(const std::string_view value, const std::string_view fallback) const;
void generateConfig(const std::string_view filename);

void addAliasColors(const std::string_view value);
std::vector<std::string> getValueArrayStr(const std::string_view value, const std::vector<std::string>& fallback);

template <typename T>
Expand Down Expand Up @@ -235,6 +235,12 @@ magenta = "\e[1;35m"
cyan = "\e[1;36m"
white = "\e[1;37m"
# Alias colors.
# They can be used as like as the color tag.
# This is as like as using the --color argument
# Syntax must be "name=value", e.g "purple=magenta" or "orange=!#F08000"
alias-colors = ["purple=magenta"]
# 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"
Expand Down
14 changes: 14 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ void Config::loadConfigFile(const std::string_view filename, colors_t& colors)
"backing up to green, yellow and red");
this->percentage_colors = {"green", "yellow", "red"};
}

for (const std::string_view str : this->getValueArrayStr("config.alias-colors", {}))
this->addAliasColors(str);
}

std::string Config::getThemeValue(const std::string_view value, const std::string_view fallback) const
Expand Down Expand Up @@ -126,6 +129,17 @@ std::vector<std::string> Config::getValueArrayStr(const std::string_view
return ret;
}

void Config::addAliasColors(const std::string_view value)
{
const size_t pos = value.find('=');
if (pos == std::string::npos)
die("alias color '{}' does NOT have an equal sign '=' for separiting color name and value.\n"
"for more check with --help", value);

this->colors_name.push_back(value.substr(0, pos).data());
this->colors_value.push_back(value.substr(pos + 1).data());
}

void Config::generateConfig(const std::string_view filename)
{
if (std::filesystem::exists(filename))
Expand Down
16 changes: 1 addition & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ static std::string parse_config_path(int argc, char* argv[], const std::string&
case 'C':
if (!std::filesystem::exists(optarg))
die("config file '{}' doesn't exist", optarg);

return optarg;
break;
}
}

Expand Down Expand Up @@ -351,19 +349,7 @@ static bool parseargs(int argc, char* argv[], Config& config, const std::string_
config.gui_bg_image = optarg; break;

case "color"_fnv1a16:
{
const std::string& optarg_str = optarg;
const size_t& pos = optarg_str.find('=');
if (pos == std::string::npos)
die("argument color '{}' does NOT have an equal sign '=' for separiting color name and value.\n"
"for more check with --help", optarg_str);

const std::string& name = optarg_str.substr(0, pos);
const std::string& value = optarg_str.substr(pos + 1);
config.m_arg_colors_name.push_back(name);
config.m_arg_colors_value.push_back(value);
}
break;
config.addAliasColors(optarg); break;

case "gen-config"_fnv1a16:
if (OPTIONAL_ARGUMENT_IS_PRESENT)
Expand Down
35 changes: 19 additions & 16 deletions src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,10 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s

if (!parsingLayout && tagpos != std::string::npos)
{
if (!removetag)
pureOutput.replace(tagpos, taglen, cmd_output);
else
if (removetag)
pureOutput.erase(tagpos, taglen);
else
pureOutput.replace(tagpos, taglen, cmd_output);
}
}
break;
Expand Down Expand Up @@ -335,8 +335,8 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s

output.replace(dollarSignIndex, taglen,
get_and_color_percentage(n1, n2, systemInfo, config, colors, parsingLayout, invert));
break;
}
break;

case ']':
{
Expand Down Expand Up @@ -377,37 +377,35 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
case '}': // please pay very attention when reading this unreadable and godawful code

// if at end there a '$', it will make the end output "$</span>" and so it will confuse
// addValueFromModule() and so let's make it "$ </span>". this geniunenly stupid
// addValueFromModule() and so let's make it "$ </span>". this is geniunenly stupid
if (config.gui && output.back() == '$')
output += ' ';

if (!config.m_arg_colors_name.empty())
if (!config.colors_name.empty())
{
const auto& it_name =
std::find(config.m_arg_colors_name.begin(), config.m_arg_colors_name.end(), command);
if (it_name != config.m_arg_colors_name.end())
const auto& it_name = std::find(config.colors_name.begin(), config.colors_name.end(), command);
if (it_name != config.colors_name.end())
{
const auto& it_value = std::distance(config.m_arg_colors_name.begin(), it_name);
const auto& it_value = std::distance(config.colors_name.begin(), it_name);

if (hasStart(command, "auto"))
{
// "ehhmmm why goto and double code? that's ugly and unconvienient :nerd:"
// I don't care, it does the work and well
if (command == *it_name)
command = config.m_arg_colors_value.at(it_value);
command = config.colors_value.at(it_value);
goto jump;
}

if (command == *it_name)
command = config.m_arg_colors_value.at(it_value);
command = config.colors_value.at(it_value);
}
}

if (hasStart(command, "auto"))
{
std::uint16_t ver =
static_cast<std::uint16_t>(command.length() > 4 ? std::stoi(command.substr(4)) - 1 : 0);
if (ver >= auto_colors.size() || ver < 1)
int ver = command.length() > 4 ? std::stoi(command.substr(4)) - 1 : 0;
if (ver < 1 || static_cast<size_t>(ver) >= auto_colors.size())
ver = 0;

if (auto_colors.empty())
Expand Down Expand Up @@ -493,7 +491,10 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
}

else
{
error("PARSER: failed to parse line with color '{}'", str_clr);
break;
}

firstrun_noclr = false;
}
Expand Down Expand Up @@ -549,8 +550,10 @@ std::string parse(const std::string_view input, systemInfo_t& systemInfo, std::s
}

else
{
error("PARSER: failed to parse line with color '{}'", str_clr);

break;
}
output.replace(dollarSignIndex, output.length() - dollarSignIndex,
formatted_replacement_string);
}
Expand Down

0 comments on commit a120a87

Please sign in to comment.