-
Notifications
You must be signed in to change notification settings - Fork 0
/
SColor.cpp
206 lines (169 loc) · 3.09 KB
/
SColor.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "SColor.h"
bool SColor::b_allEnabled=true;
void SColor::setAllEnabled(bool enabled)
{
b_allEnabled=enabled;
}
bool SColor::allEnabled()
{
return b_allEnabled;
}
void SColor::setEnabled(bool enabled)
{
b_enabled=enabled;
}
bool SColor::enabled()const
{
return b_enabled;
}
void SColor::move(int x,int y,std::ostream& os)
{
if(allEnabled())os<<"\e["<<x-1<<";"<<y-1<<"H";
}
void SColor::moveUP(int n,std::ostream& os)
{
if(allEnabled())os<<"\e["<<n<<"A";
}
void SColor::moveDOWN(int n,std::ostream& os)
{
if(allEnabled())os<<"\e["<<n<<"B";
}
void SColor::moveRIGHT(int n,std::ostream& os)
{
if(allEnabled())os<<"\e["<<n<<"C";
}
void SColor::moveLEFT(int n,std::ostream& os)
{
if(allEnabled())os<<"\e["<<n<<"D";
}
void SColor::clear(std::ostream& os)
{
if(allEnabled())os<<"\e[2J\e[1;1H";
}
void SColor::clearLine(std::ostream& os)
{
if(allEnabled())os<<"\e[K";
}
void SColor::setCursor(int x,int y,std::ostream& os)
{
if(allEnabled())os<<"\e["<<x<<";"<<y<<"H";
}
void SColor::saveCursor(std::ostream& os)
{
if(allEnabled())os<<"\e[s";
}
void SColor::unsetCursor(std::ostream& os)
{
if(allEnabled())os<<"\e[u";
}
void SColor::hideCursor(std::ostream& os)
{
if(allEnabled())os<<"\e[?25l";
}
void SColor::echoCursor(std::ostream& os)
{
if(allEnabled())os<<"\e[?25h";
}
SColor::SColor(int fg,int bg,int style)
{
this->foreground=(fg==DEFAULT)?0:fg+30;
this->background=(bg==DEFAULT)?0:bg+40;
this->style=style;
}
std::ostream& operator<<(std::ostream& os,const SColor& sColor)
{
if(SColor::allEnabled()&&sColor.enabled())
{
os<<"\e["<<sColor.foreground;
if(sColor.background)os<<';'<<sColor.background;
for(int i=1;i<=9;i++)os<<';'<<((sColor.style&(1<<i))?i:i+20);
os<<"m";
}
return os;
}
SColor SColor::operator~()
{
SColor res(*this);
res.style=~res.style;
return res;
}
const SColor& SColor::operator+=(int style)
{
this->style|=style;
return *this;
}
const SColor& SColor::operator-=(int style)
{
this->style&=~style;
return *this;
}
const SColor& SColor::operator|=(int style)
{
this->style|=style;
return *this;
}
const SColor& SColor::operator&=(int style)
{
this->style&=style;
return *this;
}
const SColor& SColor::operator^=(int style)
{
this->style^=style;
return *this;
}
SColor operator+(const SColor &a,int b)
{
return SColor(a)+=b;
}
SColor operator-(const SColor &a,int b)
{
return SColor(a)-=b;
}
SColor operator|(const SColor &a,int b)
{
return SColor(a)|=b;
}
SColor operator&(const SColor &a,int b)
{
return SColor(a)&=b;
}
SColor operator^(const SColor &a,int b)
{
return SColor(a)^=b;
}
SColor& SColor::reset()
{
this->background=0;
this->foreground=0;
this->style=0;
}
SColor& SColor::setFg(int fg)
{
this->foreground=(fg==DEFAULT)?0:fg+30;
return *this;
}
SColor& SColor::setBg(int bg)
{
this->background=(bg==DEFAULT)?0:bg+40;
return *this;
}
SColor& SColor::setStyle(int style)
{
this->style=style;
return *this;
}
int SColor::getFg()const
{
if(foreground==DEFAULT)return DEFAULT;
return foreground-30;
}
int SColor::getBg()const
{
if(background==DEFAULT)return DEFAULT;
return background-40;
}
int SColor::getStyle()const
{
return style;
}