aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util/LogColor.h
blob: 132fb944e5373405a2db3d5a4b96124159916119 (plain)
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
#pragma once

#include <string>

namespace crepe {

/**
 * \brief Utility class for coloring text using ANSI escape codes
 *
 * \note Most methods in this class return a reference to \c this, which may be
 * used to chain multiple display attributes.
 */
class LogColor {
public:
	/**
	 * \brief Get color code as STL string
	 *
	 * \param content If given, color this string and append a color reset escape sequence.
	 *
	 * \returns Color escape sequence
	 */
	const std::string str(const std::string & content = "") const;

public:
	//! Reset color to default foreground and background color
	LogColor & reset();

public:
	/**
	 * \name Foreground colors
	 *
	 * These functions set the foreground (text) color. The \c bright parameter
	 * makes the color brighter, or bold on some terminals.
	 * \{
	 */
	LogColor & fg_black(bool bright = false);
	LogColor & fg_red(bool bright = false);
	LogColor & fg_green(bool bright = false);
	LogColor & fg_yellow(bool bright = false);
	LogColor & fg_blue(bool bright = false);
	LogColor & fg_magenta(bool bright = false);
	LogColor & fg_cyan(bool bright = false);
	LogColor & fg_white(bool bright = false);
	/// \}

public:
	/**
	 * \name Background colors
	 *
	 * These functions set the background color. The \c bright parameter makes
	 * the color brighter.
	 * \{
	 */
	LogColor & bg_black(bool bright = false);
	LogColor & bg_red(bool bright = false);
	LogColor & bg_green(bool bright = false);
	LogColor & bg_yellow(bool bright = false);
	LogColor & bg_blue(bool bright = false);
	LogColor & bg_magenta(bool bright = false);
	LogColor & bg_cyan(bool bright = false);
	LogColor & bg_white(bool bright = false);
	/// \}

private:
	/**
	 * \brief Append SGR escape sequence to \c this->code
	 *
	 * \param code SGR attribute number
	 *
	 * See <https://en.wikipedia.org/wiki/ANSI_escape_code> for magic number
	 * reference.
	 */
	LogColor & add_code(unsigned int code);

private:
	//! Color escape sequence
	std::string code = "";
};

} // namespace crepe