Skip to content

Commit 2c08f86

Browse files
committed
added a message color API
This enables colorful formatting of any message printed through the messaget API. Server-side implementation is provided for Windows and Unix consoles.
1 parent 422b46c commit 2c08f86

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

src/util/cout_message.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Author: Daniel Kroening, kroening@kroening.com
2121
#include <io.h>
2222
#include <cstdio>
2323
#include <util/pragma_pop.def>
24+
#else
25+
#include <unistd.h>
2426
#endif
2527

2628
#include "unicode.h"
@@ -35,6 +37,35 @@ cerr_message_handlert::cerr_message_handlert():
3537
{
3638
}
3739

40+
console_message_handlert::console_message_handlert(bool _always_flush)
41+
: always_flush(_always_flush), is_a_tty(false), use_SGR(false)
42+
{
43+
#ifdef _WIN32
44+
HANDLE out_handle=GetStdHandle(STD_OUTPUT_HANDLE);
45+
46+
DWORD consoleMode;
47+
if(GetConsoleMode(out_handle, &consoleMode))
48+
{
49+
is_a_tty = true;
50+
51+
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
52+
if(SetConsoleMode(out_handle, consoleMode))
53+
use_SGR = true;
54+
}
55+
#else
56+
is_a_tty = isatty(STDOUT_FILENO);
57+
use_SGR = is_a_tty;
58+
#endif
59+
}
60+
61+
std::string console_message_handlert::command(unsigned c) const
62+
{
63+
if(!use_SGR)
64+
return std::string();
65+
66+
return "\x1b[" + std::to_string(c) + 'm';
67+
}
68+
3869
void console_message_handlert::print(
3970
unsigned level,
4071
const std::string &message)

src/util/cout_message.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ class console_message_handlert : public message_handlert
3636

3737
virtual void flush(unsigned level) override;
3838

39-
console_message_handlert() : always_flush(false)
39+
console_message_handlert() : console_message_handlert(false)
4040
{
4141
}
4242

43-
explicit console_message_handlert(bool always_flush)
44-
: always_flush(always_flush)
45-
{
46-
}
43+
explicit console_message_handlert(bool always_flush);
44+
45+
std::string command(unsigned c) const override;
4746

4847
protected:
4948
const bool always_flush;
49+
bool is_a_tty;
50+
bool use_SGR;
5051
};
5152

5253
class gcc_message_handlert : public message_handlert

src/util/message.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class message_handlert
6565
return message_count[level];
6666
}
6767

68+
virtual std::string command(unsigned) const
69+
{
70+
return std::string();
71+
}
72+
6873
protected:
6974
unsigned verbosity;
7075
std::vector<std::size_t> message_count;
@@ -275,6 +280,50 @@ class messaget
275280
return m;
276281
}
277282

283+
std::string command(unsigned c) const
284+
{
285+
if(message_handler)
286+
return message_handler->command(c);
287+
else
288+
return std::string();
289+
}
290+
291+
// return to default
292+
std::string reset() const
293+
{
294+
return command(0);
295+
}
296+
297+
// set red foreground color
298+
std::string red() const
299+
{
300+
return command(31);
301+
}
302+
303+
// set green foreground color
304+
std::string green() const
305+
{
306+
return command(32);
307+
}
308+
309+
// set yellow foreground color
310+
std::string yellow() const
311+
{
312+
return command(33);
313+
}
314+
315+
// set blue foreground color
316+
std::string blue() const
317+
{
318+
return command(34);
319+
}
320+
321+
// set bold font
322+
std::string bold() const
323+
{
324+
return command(1);
325+
}
326+
278327
mstreamt &get_mstream(unsigned message_level) const
279328
{
280329
mstream.message_level=message_level;

src/util/ui_message.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ class ui_message_handlert : public message_handlert
8989
const source_locationt &location);
9090

9191
const char *level_string(unsigned level);
92+
93+
std::string command(unsigned c) const override
94+
{
95+
return message_handler->command(c);
96+
}
9297
};
9398

9499
#define OPT_FLUSH "(flush)"

0 commit comments

Comments
 (0)