forked from jart/hiptext
-
Notifications
You must be signed in to change notification settings - Fork 2
/
termprinter.h
94 lines (83 loc) · 2.25 KB
/
termprinter.h
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
// hiptext - Image to Text Converter
// By Justine Tunney
#ifndef HIPTEXT_TERMPRINTER_H_
#define HIPTEXT_TERMPRINTER_H_
#include <ostream>
// A wrapper around cout that compresses term color escape codes.
class TermPrinter {
public:
// 'bg' is the native background color of the terminal. If 'bgprint' is set
// to false then TermPrinter will not waste its time printing 256color
// background codes that are nearly identical to your terminal background.
explicit TermPrinter(std::ostream& out);
void Flush();
void Reset(bool force = false);
void SetBold(bool bold);
void SetItalic(bool italic);
void SetUnderline(bool underline);
void SetUnderline2(bool underline2);
void SetStrike(bool strike);
void SetBlink(bool blink);
void SetFlip(bool flip);
void SetForeground256(int code);
void SetBackground256(int code);
template<typename T>
inline TermPrinter& operator<<(const T& val) {
Flush();
out_ << val;
return *this;
}
private:
static const int kReset;
static const int kBoldOn;
static const int kBoldOff;
static const int kItalicOn;
static const int kItalicOff;
static const int kUnderlineOn;
static const int kUnderlineOff;
static const int kUnderline2On;
static const int kUnderline2Off;
static const int kStrikeOn;
static const int kStrikeOff;
static const int kBlinkOn;
static const int kBlinkOff;
static const int kFlipOn;
static const int kFlipOff;
static const int kForegroundOff;
static const int kBackgroundOff;
static const int kForeground256;
static const int kBackground256;
static const char* kEscapeStart;
static const char* kEscapeEnd;
static const char* kEscapeSep;
static const char* kEscapeReset;
struct State {
int fg;
int bg;
bool bold;
bool italic;
bool underline;
bool underline2;
bool strike;
bool blink;
bool flip;
};
bool IsStyled() const;
void PrintSep(bool* first) const;
void PrintCode(int code, bool* first);
bool dirty_;
State cur_;
State new_;
std::ostream& out_;
};
#endif // HIPTEXT_TERMPRINTER_H_
// For Emacs:
// Local Variables:
// mode:c++
// indent-tabs-mode:nil
// tab-width:2
// c-basic-offset:2
// c-file-style: nil
// End:
// For VIM:
// vim:set expandtab softtabstop=2 shiftwidth=2 tabstop=2: