1+ #include < iostream>
2+ #include < regex>
3+ #include < unordered_map>
4+
5+ static const char *Red = " \x1b [31m" ;
6+ static const char *Green = " \x1b [32m" ;
7+ static const char *Yellow = " \x1b [33m" ;
8+ static const char *Blue = " \x1b [34m" ;
9+ static const char *Magenta = " \x1b [35m" ;
10+ static const char *Cyan = " \x1b [36m" ;
11+
12+ bool hasSuffix (std::string const &Str, std::string const &Suffix) {
13+ if (Str.length () >= Suffix.length ())
14+ return (Str.compare (Str.length () - Suffix.length (), Suffix.length (),
15+ Suffix) == 0 );
16+ else
17+ return false ;
18+ }
19+
20+ static std::string addColor (const char *Color, const std::string &Token) {
21+ static const char *Reset = " \x1b [0m" ;
22+ return Color + Token + Reset;
23+ }
24+
25+ bool colorize (std::string &Str, const std::string &Token, const char *Color) {
26+ size_t start_pos = Str.find (Token);
27+ if (start_pos == std::string::npos)
28+ return false ;
29+ Str.replace (start_pos, Token.length (), addColor (Color, Token));
30+ return true ;
31+ }
32+
33+ static void handleLine (std::string Line) {
34+ static const std::vector<std::pair<const char *, const char *>> ColorCodes = {
35+ {" Building CXX object" , Green},
36+ {" Linking CXX static library" , Magenta},
37+ {" Updating " , Blue},
38+ };
39+ const char *LastUsedColor = nullptr ;
40+ for (auto &P : ColorCodes) {
41+ if (colorize (Line, P.first , P.second )) {
42+ LastUsedColor = P.second ;
43+ }
44+ }
45+ if (LastUsedColor) {
46+ if (hasSuffix (Line, " .o" ) || hasSuffix (Line, " .a" )) {
47+ std::size_t StartOfLastWord = Line.size () - 1 ;
48+ for (; StartOfLastWord >= 0 ; StartOfLastWord--) {
49+ if (Line.at (StartOfLastWord) == ' ' )
50+ break ;
51+ }
52+ std::string LastWord = Line.substr (StartOfLastWord);
53+ colorize (Line, LastWord, LastUsedColor);
54+ }
55+ }
56+
57+ std::regex NinjaStatus (" \\ [[0-9]+/[0-9]+\\ ]" );
58+ std::smatch Match;
59+ if (std::regex_search (Line, Match, NinjaStatus)) {
60+ colorize (Line, Match[0 ].str (), Yellow);
61+ }
62+
63+ std::cout << Line << std::endl;
64+ }
65+
66+ int main () {
67+ std::string InputLine;
68+ while (std::cin) {
69+ getline (std::cin, InputLine);
70+ handleLine (InputLine);
71+ };
72+ }
0 commit comments