From 58ee0fabc9028fcdf86faab3bb9c1db2b27e3644 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 19 Apr 2024 15:57:23 +0200 Subject: Add APT::Configuration::color helper to colorize things --- apt-pkg/aptconfiguration.cc | 35 +++++++++++++++++++++++++++++++++++ apt-pkg/aptconfiguration.h | 2 ++ 2 files changed, 37 insertions(+) (limited to 'apt-pkg') diff --git a/apt-pkg/aptconfiguration.cc b/apt-pkg/aptconfiguration.cc index 982e68bb1..f462b6eda 100644 --- a/apt-pkg/aptconfiguration.cc +++ b/apt-pkg/aptconfiguration.cc @@ -563,4 +563,39 @@ bool Configuration::checkUsrMerged() return true; } /*}}}*/ +// isUsrMerged - whether usr is merged t /*{{{*/ +// --------------------------------------------------------------------- +/* */ +std::string Configuration::color(std::string const &colorName, std::string const &content) +{ + if (not _config->FindB("APT::Color")) + return content; + + auto colors = ::Configuration(_config->Tree("APT::Color")); + auto color = colors.Find(colorName); + + // Resolve the color recursively. A color string has the following format + // := \x1B ; fully resolved color + // | \\x1B ; color escaped. + // | ; a simple color name + // | ; a sequence of colors + if (color.find(" ") != color.npos) + { + std::string res; + for (auto &&colorPart : VectorizeString(color, ' ')) + res += Configuration::color(colorPart); + color = res; + } + else if (not color.empty() && color[0] != '\x1B') + { + if (APT::String::Startswith(color, "\\x1B")) + color = "\x1B" + color.substr(4); + else + color = Configuration::color(color); + } + if (content.empty()) + return color; + return color + content + Configuration::color("Neutral"); +} + /*}}}*/ } diff --git a/apt-pkg/aptconfiguration.h b/apt-pkg/aptconfiguration.h index 3e2636edc..58c925b7f 100644 --- a/apt-pkg/aptconfiguration.h +++ b/apt-pkg/aptconfiguration.h @@ -130,7 +130,9 @@ namespace Configuration { /*{{{*/ APT_PUBLIC bool isChroot(); /** \return Check usr is merged or produce error. */ APT_PUBLIC bool checkUsrMerged(); + APT_PUBLIC std::string color(std::string const &colorName, std::string const &content = ""); #endif + /*}}}*/ } /*}}}*/ -- cgit v1.2.3-70-g09d2