1 |
/* |
2 |
* LSCP Shell |
3 |
* |
4 |
* Copyright (c) 2014 Christian Schoenebeck |
5 |
* |
6 |
* This program is part of LinuxSampler and released under the same terms. |
7 |
*/ |
8 |
|
9 |
#ifndef CFMT_H |
10 |
#define CFMT_H |
11 |
|
12 |
#include <stdio.h> |
13 |
#include <stdlib.h> |
14 |
#include <iostream> |
15 |
#include <string> |
16 |
|
17 |
/** |
18 |
* Writes "ANSI escape codes" to stdout to control color, font weight and |
19 |
* brightness of text written to the command line terminal. |
20 |
* |
21 |
* This is a lite-weight class to prevent a dependency to i.e. libncurses. |
22 |
* |
23 |
* This class is not designed as singleton, because the idea was to reset the |
24 |
* terminals output format as soon as the respective CFmt object is destructed. |
25 |
* This way a developer is not forces to reset the output format each time he |
26 |
* used something special. |
27 |
*/ |
28 |
class CFmt { |
29 |
public: |
30 |
CFmt() : m_bright(false) {} |
31 |
virtual ~CFmt() { reset(); } |
32 |
|
33 |
CFmt& black() { setColorFg("0"); return *this; } |
34 |
CFmt& red() { setColorFg("1"); return *this; } |
35 |
CFmt& green() { setColorFg("2"); return *this; } |
36 |
CFmt& yellow() { setColorFg("3"); return *this; } |
37 |
CFmt& blue() { setColorFg("4"); return *this; } |
38 |
CFmt& magenta() { setColorFg("5"); return *this; } |
39 |
CFmt& cyan() { setColorFg("6"); return *this; } |
40 |
CFmt& white() { setColorFg("7"); return *this; } |
41 |
|
42 |
CFmt& bold() { setCode("1"); return *this; } |
43 |
//CFmt& faint() { setCode("2"); return *this; } // barely supported by any terminal |
44 |
//CFmt& italic() { setCode("3"); return *this; } // barely supported by any terminal |
45 |
CFmt& underline() { setCode("4"); return *this; } |
46 |
//CFmt& frame() { setCode("51"); return *this; } // barely supported by any terminal |
47 |
//CFmt& encircle() { setCode("52"); return *this; } // barely supported by any terminal |
48 |
//CFmt& overline() { setCode("53"); return *this; } // barely supported by any terminal |
49 |
|
50 |
CFmt& bright(bool b = true) { m_bright = b; return *this; } |
51 |
|
52 |
CFmt& reset() { |
53 |
setCode("0"); |
54 |
m_bright = false; |
55 |
return *this; |
56 |
} |
57 |
|
58 |
protected: |
59 |
CFmt& setColorFg(std::string colorCode) { |
60 |
setCode((m_bright ? "9" : "3") + colorCode); |
61 |
return *this; |
62 |
} |
63 |
|
64 |
static void setCode(std::string sCode) { |
65 |
std::cout << modStr(sCode) << std::flush; |
66 |
} |
67 |
|
68 |
static std::string modStr(std::string code) { |
69 |
return "\033[" + code + "m"; |
70 |
} |
71 |
|
72 |
private: |
73 |
bool m_bright; |
74 |
}; |
75 |
|
76 |
#endif // CFMT_H |