Skip to content

Commit f3919b2

Browse files
committed
refactor
1 parent 254be27 commit f3919b2

File tree

3 files changed

+50
-36
lines changed

3 files changed

+50
-36
lines changed

src/pcm.cpp

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -170,42 +170,10 @@ void print_output(PCM * m,
170170
const bool show_partial_core_output,
171171
const bool show_socket_output,
172172
const bool show_system_output,
173-
const int metricVersion,
174-
const bool color
173+
const int metricVersion
175174
)
176175
{
177176
cout << "\n";
178-
auto setColor = [&color](const char * colorStr)
179-
{
180-
return color ? colorStr : "";
181-
};
182-
std::vector<const char *> colorTable = {
183-
ASCII_GREEN,
184-
ASCII_YELLOW,
185-
ASCII_MAGENTA,
186-
ASCII_CYAN,
187-
ASCII_BRIGHT_GREEN,
188-
ASCII_BRIGHT_YELLOW,
189-
ASCII_BRIGHT_BLUE,
190-
ASCII_BRIGHT_MAGENTA,
191-
ASCII_BRIGHT_CYAN,
192-
ASCII_BRIGHT_WHITE
193-
};
194-
size_t currentColor = 0;
195-
auto setNextColor = [&setColor,&currentColor,colorTable]()
196-
{
197-
const auto result = setColor(colorTable[currentColor++]);
198-
if (currentColor == colorTable.size())
199-
{
200-
currentColor = 0;
201-
}
202-
return result;
203-
};
204-
auto resetColor = [&setColor, &currentColor]()
205-
{
206-
currentColor = 0;
207-
return setColor(ASCII_RESET_COLOR);
208-
};
209177

210178
switch (metricVersion)
211179
{
@@ -1336,7 +1304,6 @@ int mainThrows(int argc, char * argv[])
13361304
bool disable_JKT_workaround = false; // as per http://software.intel.com/en-us/articles/performance-impact-when-sampling-certain-llc-events-on-snb-ep-with-vtune
13371305
bool enforceFlush = false;
13381306
int metricVersion = 2;
1339-
bool color = false;
13401307

13411308
parsePID(argc, argv, pid);
13421309

@@ -1418,7 +1385,7 @@ int mainThrows(int argc, char * argv[])
14181385
}
14191386
else if (check_argument_equals(*argv, {"--color"}))
14201387
{
1421-
color = true;
1388+
setColorEnabled();
14221389
continue;
14231390
}
14241391
else if (check_argument_equals(*argv, {"-csv", "/csv"}))
@@ -1575,7 +1542,7 @@ int mainThrows(int argc, char * argv[])
15751542
else
15761543
print_output(m, cstates1, cstates2, sktstate1, sktstate2, ycores, sstate1, sstate2,
15771544
cpu_model, show_core_output, show_partial_core_output, show_socket_output, show_system_output,
1578-
metricVersion, color);
1545+
metricVersion);
15791546

15801547
std::swap(sstate1, sstate2);
15811548
std::swap(sktstate1, sktstate2);

src/utils.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,48 @@ void exit_cleanup(void)
4646
}
4747
}
4848

49+
bool colorEnabled = false;
50+
51+
void setColorEnabled(bool value)
52+
{
53+
colorEnabled = value;
54+
}
55+
56+
const char * setColor (const char * colorStr)
57+
{
58+
return colorEnabled ? colorStr : "";
59+
}
60+
61+
std::vector<const char *> colorTable = {
62+
ASCII_GREEN,
63+
ASCII_YELLOW,
64+
ASCII_MAGENTA,
65+
ASCII_CYAN,
66+
ASCII_BRIGHT_GREEN,
67+
ASCII_BRIGHT_YELLOW,
68+
ASCII_BRIGHT_BLUE,
69+
ASCII_BRIGHT_MAGENTA,
70+
ASCII_BRIGHT_CYAN,
71+
ASCII_BRIGHT_WHITE
72+
};
73+
74+
size_t currentColor = 0;
75+
const char * setNextColor()
76+
{
77+
const auto result = setColor(colorTable[currentColor++]);
78+
if (currentColor == colorTable.size())
79+
{
80+
currentColor = 0;
81+
}
82+
return result;
83+
}
84+
85+
const char * resetColor()
86+
{
87+
currentColor = 0;
88+
return setColor(ASCII_RESET_COLOR);
89+
}
90+
4991
void print_cpu_details()
5092
{
5193
const auto m = PCM::getInstance();

src/utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ constexpr const char* ASCII_BRIGHT_CYAN = "\033[1;36m";
163163
constexpr const char* ASCII_BRIGHT_WHITE = "\033[1;37m";
164164
constexpr const char* ASCII_RESET_COLOR = "\033[0m";
165165

166+
void setColorEnabled(bool value = true);
167+
const char * setColor(const char * colorStr);
168+
const char * setNextColor();
169+
const char * resetColor();
170+
166171
template <class IntType>
167172
inline std::string unit_format(IntType n)
168173
{

0 commit comments

Comments
 (0)