-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnsi.hpp
More file actions
320 lines (309 loc) · 7.61 KB
/
Ansi.hpp
File metadata and controls
320 lines (309 loc) · 7.61 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#pragma once
#define PCL_ANSI
#include<format>
#include<string>
#include<print>
#include<deque>
#include<regex>
/// @brief Reset all ansi style
inline void ResetAnsiStyle()
{std::print("\e[0m");return;}
/// @brief Move cursor to horizontal `x` and vertical `y`
inline void CursorGoto(short x,short y)
{std::print("\e[{};{}H",y+1,x+1);return;}
/// @brief Offset the cursor horizontally by `x` and vertically by `y`
inline void CursorOffset(short x,short y)
{
char op;
if(x!=0)
{
if(x>0) op='C';
else if(x<0) op='D',x=-x;
std::print("\e[{}{}",x,op);
}
if(y!=0)
{
if(y>0) op='B';
else if(y<0) op='A',y=-y;
std::print("\e[{}{}",y,op);
}
}
/// @brief Clear the line where the cursor is
inline void ClearCurrentLine()
{std::print("\e[2K");return;}
/** @brief Fill the visible screen with white spaces.
* The overflowed content won't be effected */
inline void ClearWholeScreen()
{std::print("\e[2J");return;}
/** @brief Save the current position of the cursor.
* Later you can use `RestoreCursorPos()` to restore this position. */
inline void SaveCurrentCursorPos()
{std::print("\e[2s");return;}
/** @brief Set position of the cursor to where you use
* `SaveCurrentCursorPos()`. */
inline void RestoreCursorPos()
{std::print("\e[2u");return;}
/// @brief Let the cursor invisible.
inline void HideCursor()
{std::print("\e[?25l");return;}
/// @brief Let the cursor visible.
inline void ShowCursor()
{std::print("\e[?25h");return;}
/// @brief Modify the title of the console (or terminal).
inline void SetConsoleTitle(std::string title)
{std::print("\e]0;{}\a",title);return;}
/// @brief Set the foreground color.
#ifdef PCL_COLOR
#include"Color.hpp"
inline void SetForegroundColor(Color col)
{
if(col.DontModify()) return;
std::print("\e[38;2;{};{};{}m",col.R,col.G,col.B);
}
/// @brief Set the background color.
inline void SetBackgroundColor(Color col)
{
if(col.DontModify()) return;
std::print("\e[48;2;{};{};{}m",col.R,col.G,col.B);
}
#endif
namespace pcpri
{
int string2int(std::string a)
{
int l=a.length(),ans=0;
for(int i=0;i<l;i++)
ans*=10,
ans+=a[i]-'0';
return ans;
}
}
#define pcANSI_MARKER_CHAR '%'
#define pcANSI_END_REGION '/'
#define pcANSI_BOLD 'b'
#define pcANSI_COLOR_8 'q'
#define pcANSI_COLOR_256 'c'
#define pcANSI_COLOR_BACK 'b'
#define pcANSI_COLOR_FORE 'f'
#define pcANSI_DARKEN 'd'
#define pcANSI_GRAY 'g'
#define pcANSI_INVERT '!'
#define pcANSI_ITALIC 'i'
#define pcANSI_LINK 'l'
#define pcANSI_STRIKETHROUGH 's'
#define pcANSI_TWINKLE 't'
#define pcANSI_UNDERLINE 'u'
#ifdef PCL_COLOR
#define pcANSI_COLOR_TRUE 'C'
#endif
namespace pcpri
{
std::string parseSimpleMark(char mark)
{
switch(mark)
{
case pcANSI_MARKER_CHAR:
return std::format("{}",pcANSI_MARKER_CHAR);
case pcANSI_END_REGION:
return "\e[0m";
case pcANSI_BOLD:
return "\e[1m";
case pcANSI_GRAY:
return "\e[2m";
case pcANSI_ITALIC:
return "\e[3m";
case pcANSI_UNDERLINE:
return "\e[4m";
case pcANSI_TWINKLE:
return "\e[5m";
case pcANSI_INVERT:
return "\e[7m";
case pcANSI_DARKEN:
return "\e[8m";
case pcANSI_STRIKETHROUGH:
return "\e[9m";
default:
return std::format("{}",pcANSI_MARKER_CHAR);
}
}
int parseMark(std::string& res,std::string input,int len,int i,bool close=false)
{
std::string link,text;
int temp1=0,temp2=0;
#ifdef PCL_COLOR
Color col;
#endif
switch(input[i])
{
break;case pcANSI_LINK:
if(input[++i]!='[') break; i++;
for(;i<len&&input[i]!=']';i++)
text+=input[i];
if(input[++i]!='(') break; i++;
for(;i<len&&input[i]!=')';i++)
link+=input[i];
res+=std::format("\e]8;;{0}\e{2}{1}\e]8;;\e{2}",
link,text,close?"\\\a":"\\");
break;case pcANSI_COLOR_8:
i++;
if(input[i]==pcANSI_COLOR_BACK) temp1=40;
if(input[i]==pcANSI_COLOR_FORE) temp1=30;
res+=std::format("\e[{}{}",
temp1+input[++i]-'0',close?"m\a":"m");
break;case pcANSI_COLOR_256:
temp2=0;i++;
if(input[i]==pcANSI_COLOR_BACK) temp1=48;
if(input[i]==pcANSI_COLOR_FORE) temp1=38;
if(input[++i]!='[') break; i++;
for(;i<len&&input[i]!=']';i++)
temp2*=10,temp2+=input[i]-'0';
res+=std::format("\e[{};5;{}{}",
temp1,temp2,close?"m\a":"m");
#ifdef PCL_COLOR
break;case pcANSI_COLOR_TRUE:
i++;
if(input[i]==pcANSI_COLOR_BACK) temp1=48;
if(input[i]==pcANSI_COLOR_FORE) temp1=38;
if(input[++i]!='[') break; i++;
for(;i<len&&input[i]!=']';i++)
text+=input[i];
col=Color(text);
if(col.DontModify())
break;
res+=std::format("\e[{};2;{};{};{}{}",
temp1,col.R,col.G,col.B,close?"m\a":"m");
#endif
break;default:
res+=parseSimpleMark(input[i])+(close?"\a":"");
break;}
return i;
}
}
/**
* @brief Parse a ANSI format string to ESC string.
* @param input the string to parse
* @param close Add a `\a` char in the end of ESC.
* Don't use this unless you KNOW what you are doing.
*/
std::string AnsiParse(std::string input,bool close=false)
{
std::string res,link,text;
std::deque<std::string> fmt;
int len=input.length(),last,temp1,temp2;
for(int i=0;i<len;i++)
if(input[i]==pcANSI_MARKER_CHAR&&i!=len-1)
{
i++;last=i;
if(input[i]!=pcANSI_END_REGION)
{
i=pcpri::parseMark(res,input,len,i,close);
fmt.push_back(input.substr(last,i-last+1));
}
else
{
res+=pcpri::parseSimpleMark(pcANSI_END_REGION)+(close?"\a":"");
if(!fmt.empty())
fmt.pop_back();
for(auto j:fmt)
pcpri::parseMark(res,j,j.size(),0,close);
}
}
else res+=input[i];
return res;
}
/// @brief Parse ansi after formatting
template<typename ...Tps>
inline void AnsiPrintA(std::string fmt,Tps ...args)
{
std::print("{}",AnsiParse(std::vformat(fmt,std::make_format_args(args...))));
return;
}
/// @brief Parse ansi before formatting
template<typename ...Tps>
inline void AnsiPrintB(std::string fmt,Tps ...args)
{
std::print("{}",std::vformat(AnsiParse(fmt),std::make_format_args(args...)));
return;
}
/// @brief Using `AnsiPrintA()`
template<typename ...Tps>
inline void AnsiPosPrintA(short x,short y,std::string ftm,Tps...Args)
{
CursorGoto(x,y);
AnsiPrintA(ftm,Args...);
return;
}
/// @brief Using `AnsiPrintB()`
template<typename ...Tps>
inline void AnsiPosPrintB(short x,short y,std::string ftm,Tps...Args)
{
CursorGoto(x,y);
AnsiPrintB(ftm,Args...);
return;
}
#ifdef ALWAYS_PARSE_BEFORE
#define AnsiPrint AnsiPrintB
#define AnsiPosPrint AnsiPosPrintB
#else
/**
* @brief Print an ansi string
* ```
* %/ END_REGION
* %b BOLD
* %d DARKEN
* %g GRAY
* %! INVERT
* %i ITALIC
* %l LINK
* %s STRIKETHROUGH
* %t TWINKLE
* %u UNDERLINE
* ```
* For colors:
* ```
* %<mode><back/foreground>[<color>]
* <mode>:
* q 8-color
* c 256-color
* C true-color //if Color.hpp is included
* In this mode, <color> could be:
* - RRGGBB (e.g. #20c0ff)
* - RGB (e.g. #0f6)
* - Named colors (e.g. red, dodgerblue)
* <back/foreground>:
* b background
* f foreground
* ```
* For links:
* ```
* %l[<text>](<url>)
* ```
* Examples:
* ```text
* %Cf[red]This is red text%/ and this is normal text.
* %bThis is bold text%/ and this is normal text.
* %l[Click here](https://example.com)%/ to visit example.com.
* ```
* @note if `ALWAYS_PARSE_BEFORE` is defined,
* this macro will redirect to `AnsiPrintB` (Parse before formatting)
*/
#define AnsiPrint AnsiPrintA
/**
* @note if `ALWAYS_PARSE_BEFORE` is defined,
* this macro will redirect to `AnsiPosPrintB` (Parse before formatting)
*/
#define AnsiPosPrint AnsiPosPrintA
#endif
/// @brief Calculate the visable part of a ANSI formatted string.
int AnsiVisLen(std::string s)
{
std::string t=AnsiParse(s,true);
int len=t.length(),ans=0;
for(int i=0;i<len;i++)
if(t[i]=='\e')
while(t[i]!='\a') i++;
else if(t[i]=='\a')
continue;
else ans++;
return ans;
};