-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
95 lines (85 loc) · 2.56 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <regex>
#include <unordered_map>
static const char *Red = "\x1b[31m";
static const char *Green = "\x1b[32m";
static const char *Yellow = "\x1b[33m";
static const char *Blue = "\x1b[34m";
static const char *Magenta = "\x1b[35m";
static const char *Cyan = "\x1b[36m";
bool hasSuffix(std::string const &Str, std::string const &Suffix) {
if (Suffix.empty())
return true;
if (Str.length() >= Suffix.length())
return (Str.compare(Str.length() - Suffix.length(), Suffix.length(),
Suffix) == 0);
else
return false;
}
static std::string addColor(const char *Color, const std::string &Token) {
static const char *Reset = "\x1b[0m";
return Color + Token + Reset;
}
bool colorize(std::string &Str, const std::string &Token, const char *Color) {
size_t start_pos = Str.find(Token);
if (start_pos == std::string::npos)
return false;
Str.replace(start_pos, Token.length(), addColor(Color, Token));
return true;
}
struct Highlight {
const char *Str;
const char *Color;
const char *Suffix;
};
static void handleLine(std::string Line) {
std::regex NinjaStatus("\\[[0-9]+/[0-9]+\\]");
std::smatch Match;
if (std::regex_search(Line, Match, NinjaStatus)) {
colorize(Line, Match[0].str(), Yellow);
} else {
std::cout << Line << std::endl;
return;
}
static const std::vector<Highlight> ColorCodes = {
{"Building C object", Green, ""},
{"Building CXX object", Green, ""},
{"CXX ", Green, ""},
{"Linking CXX static library", Magenta, ""},
{"Linking CXX shared library", Magenta, ""},
{"Updating ", Blue, "..."},
{"Building ", Cyan, "..."},
{"Generating ", Cyan, "..."},
{"Creating export file for ", Cyan, "..."},
{"Linking CXX executable ", Red, ""},
{"Linking CXX shared module", Red, ""},
{"COPY", Magenta, ""},
{"STAMP", Blue, ".stamp"},
{"ACTION", Red, ""},
};
const char *LastUsedColor = nullptr;
for (auto &P : ColorCodes) {
if (hasSuffix(Line, P.Suffix)) {
if (colorize(Line, P.Str, P.Color)) {
LastUsedColor = P.Color;
}
}
}
if (LastUsedColor) {
std::size_t StartOfLastWord = Line.size() - 1;
for (; StartOfLastWord >= 0; StartOfLastWord--) {
if (Line.at(StartOfLastWord) == ' ')
break;
}
std::string LastWord = Line.substr(StartOfLastWord);
colorize(Line, LastWord, LastUsedColor);
}
std::cout << Line << std::endl;
}
int main() {
std::string InputLine;
while (std::cin) {
getline(std::cin, InputLine);
handleLine(InputLine);
};
}