summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2020-03-24 10:52:29 +0000
committerJulian Andres Klode <jak@debian.org>2020-03-24 10:52:29 +0000
commit4eb3cafe5655b48ef9ae8e88f756558423c09985 (patch)
treee14e12e797371eef91279a0ca122d160ccd98408
parente078cfbcd6eef2906b7661fc4822b93126f076d4 (diff)
parent86c5228fb2e0d7cf841beed2f2a3c20785e4a93c (diff)
Merge branch 'pu/colored-error' into 'master'
Add color highlighting to E:/W:/N: prefixes See merge request apt-team/apt!112
-rw-r--r--apt-pkg/contrib/error.cc88
-rw-r--r--apt-pkg/contrib/error.h28
-rw-r--r--doc/examples/configure-index1
3 files changed, 92 insertions, 25 deletions
diff --git a/apt-pkg/contrib/error.cc b/apt-pkg/contrib/error.cc
index 3c397eaf8..ac53b9648 100644
--- a/apt-pkg/contrib/error.cc
+++ b/apt-pkg/contrib/error.cc
@@ -15,6 +15,7 @@
// Include Files /*{{{*/
#include <config.h>
+#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <algorithm>
@@ -242,3 +243,90 @@ void GlobalError::MergeWithStack() {
Stacks.pop_back();
}
/*}}}*/
+
+// GlobalError::Item::operator<< /*{{{*/
+APT_HIDDEN std::ostream &operator<<(std::ostream &out, GlobalError::Item i)
+{
+ static constexpr auto COLOR_RESET = "\033[0m";
+ static constexpr auto COLOR_NOTICE = "\033[33m"; // normal yellow
+ static constexpr auto COLOR_WARN = "\033[1;33m"; // bold yellow
+ static constexpr auto COLOR_ERROR = "\033[1;31m"; // bold red
+
+ bool use_color = _config->FindB("APT::Color", false);
+
+ if (use_color)
+ {
+ switch (i.Type)
+ {
+ case GlobalError::FATAL:
+ case GlobalError::ERROR:
+ out << COLOR_ERROR;
+ break;
+ case GlobalError::WARNING:
+ out << COLOR_WARN;
+ break;
+ case GlobalError::NOTICE:
+ out << COLOR_NOTICE;
+ break;
+ default:
+ break;
+ }
+ }
+
+ switch (i.Type)
+ {
+ case GlobalError::FATAL:
+ case GlobalError::ERROR:
+ out << 'E';
+ break;
+ case GlobalError::WARNING:
+ out << 'W';
+ break;
+ case GlobalError::NOTICE:
+ out << 'N';
+ break;
+ case GlobalError::DEBUG:
+ out << 'D';
+ break;
+ }
+ out << ": ";
+
+ if (use_color)
+ {
+ switch (i.Type)
+ {
+ case GlobalError::FATAL:
+ case GlobalError::ERROR:
+ case GlobalError::WARNING:
+ case GlobalError::NOTICE:
+ out << COLOR_RESET;
+ break;
+ default:
+ break;
+ }
+ }
+
+ std::string::size_type line_start = 0;
+ std::string::size_type line_end;
+ while ((line_end = i.Text.find_first_of("\n\r", line_start)) != std::string::npos)
+ {
+ if (line_start != 0)
+ out << std::endl
+ << " ";
+ out << i.Text.substr(line_start, line_end - line_start);
+ line_start = i.Text.find_first_not_of("\n\r", line_end + 1);
+ if (line_start == std::string::npos)
+ break;
+ }
+ if (line_start == 0)
+ out << i.Text;
+ else if (line_start != std::string::npos)
+ out << std::endl
+ << " " << i.Text.substr(line_start);
+
+ if (use_color)
+ out << COLOR_RESET;
+
+ return out;
+}
+ /*}}}*/
diff --git a/apt-pkg/contrib/error.h b/apt-pkg/contrib/error.h
index 1609b8702..24eead8d5 100644
--- a/apt-pkg/contrib/error.h
+++ b/apt-pkg/contrib/error.h
@@ -315,33 +315,11 @@ private: /*{{{*/
Item(char const *Text, MsgType const &Type) :
Text(Text), Type(Type) {};
- APT_HIDDEN friend std::ostream& operator<< (std::ostream &out, Item i) {
- switch(i.Type) {
- case FATAL:
- case ERROR: out << 'E'; break;
- case WARNING: out << 'W'; break;
- case NOTICE: out << 'N'; break;
- case DEBUG: out << 'D'; break;
- }
- out << ": ";
- std::string::size_type line_start = 0;
- std::string::size_type line_end;
- while ((line_end = i.Text.find_first_of("\n\r", line_start)) != std::string::npos) {
- if (line_start != 0)
- out << std::endl << " ";
- out << i.Text.substr(line_start, line_end - line_start);
- line_start = i.Text.find_first_not_of("\n\r", line_end + 1);
- if (line_start == std::string::npos)
- break;
- }
- if (line_start == 0)
- out << i.Text;
- else if (line_start != std::string::npos)
- out << std::endl << " " << i.Text.substr(line_start);
- return out;
- }
+ APT_HIDDEN friend std::ostream &operator<<(std::ostream &out, Item i);
};
+ APT_HIDDEN friend std::ostream &operator<<(std::ostream &out, Item i);
+
std::list<Item> Messages;
bool PendingFlag;
diff --git a/doc/examples/configure-index b/doc/examples/configure-index
index bf9efc109..7664e0760 100644
--- a/doc/examples/configure-index
+++ b/doc/examples/configure-index
@@ -828,3 +828,4 @@ dir::dpkg::triplettable "<FILE>";
dir::dpkg::cputable "<FILE>";
APT::Internal::OpProgress::Absolute "<BOOL>";
+APT::Color "<BOOL>";