1 #include "ansi_escape_codes.hh"
2 
3 #include <string>
4 #include <vector>
5 #include <sstream>
6 #include <regex>
7 
8 namespace OB
9 {
10 
11 namespace ANSI_Escape_Codes
12 {
13 
14 // standard escaped characters
15 std::string const nl {"\n"};
16 std::string const cr {"\r"};
17 std::string const tab {"\t"};
18 std::string const alert {"\a"};
19 
20 // escape code sequence
21 std::string const esc {"\033["};
22 
23 // clears all attributes
24 std::string const reset {esc + "0m"};
25 
26 // style
27 std::string const bold {esc + "1m"};
28 std::string const dim {esc + "2m"};
29 std::string const italic {esc + "3m"};
30 std::string const underline {esc + "4m"};
31 std::string const blink {esc + "5m"};
32 std::string const rblink {esc + "6m"};
33 std::string const reverse {esc + "7m"};
34 std::string const conceal {esc + "8m"};
35 std::string const cross {esc + "9m"};
36 
37 // erasing
38 std::string const erase_end {esc + "K"};
39 std::string const erase_start {esc + "1K"};
40 std::string const erase_line {esc + "2K"};
41 std::string const erase_down {esc + "J"};
42 std::string const erase_up {esc + "1J"};
43 std::string const erase_screen {esc + "2J"};
44 
45 // cursor visibility
46 std::string const cursor_hide {esc + "?25l"};
47 std::string const cursor_show {esc + "?25h"};
48 
49 // cursor movement
50 std::string const cursor_home {esc + "0;0H"};
51 std::string const cursor_up {esc + "1A"};
52 std::string const cursor_down {esc + "1B"};
53 std::string const cursor_right {esc + "1C"};
54 std::string const cursor_left {esc + "1D"};
55 std::string const cursor_save {"\0337"};
56 std::string const cursor_load {"\0338"};
57 
58 // foreground color
59 std::string const fg_black {esc + "30m"};
60 std::string const fg_red {esc + "31m"};
61 std::string const fg_green {esc + "32m"};
62 std::string const fg_yellow {esc + "33m"};
63 std::string const fg_blue {esc + "34m"};
64 std::string const fg_magenta {esc + "35m"};
65 std::string const fg_cyan {esc + "36m"};
66 std::string const fg_white {esc + "37m"};
67 
68 // background color
69 std::string const bg_black {esc + "40m"};
70 std::string const bg_red {esc + "41m"};
71 std::string const bg_green {esc + "42m"};
72 std::string const bg_yellow {esc + "43m"};
73 std::string const bg_blue {esc + "44m"};
74 std::string const bg_magenta {esc + "45m"};
75 std::string const bg_cyan {esc + "46m"};
76 std::string const bg_white {esc + "47m"};
77 
fg_256(std::string x)78 std::string fg_256(std::string x)
79 {
80   auto n = std::stoi(x);
81   if (n < 0 || n > 256) return {};
82   std::stringstream ss;
83   ss << esc << "38;5;" << x << "m";
84   return ss.str();
85 }
86 
bg_256(std::string x)87 std::string bg_256(std::string x)
88 {
89   auto n = std::stoi(x);
90   if (n < 0 || n > 256) return {};
91   std::stringstream ss;
92   ss << esc << "48;5;" << x << "m";
93   return ss.str();
94 }
95 
htoi(std::string x)96 std::string htoi(std::string x)
97 {
98   std::stringstream ss;
99   ss << x;
100   unsigned int n;
101   ss >> std::hex >> n;
102   return std::to_string(n);
103 }
104 
valid_hstr(std::string & str)105 bool valid_hstr(std::string& str)
106 {
107   std::smatch m;
108   std::regex rx {"^#?((?:[0-9a-fA-F]{3}){1,2})$"};
109   if (std::regex_match(str, m, rx))
110   {
111     std::string hstr {m[1]};
112     if (hstr.size() == 3)
113     {
114       std::stringstream ss;
115       ss << hstr[0] << hstr[0] << hstr[1] << hstr[1] << hstr[2] << hstr[2];
116       hstr = ss.str();
117     }
118     str = hstr;
119     return true;
120   }
121   return false;
122 }
123 
fg_true(std::string x)124 std::string fg_true(std::string x)
125 {
126   if (! valid_hstr(x)) return {};
127   std::string h1 {x.substr(0, 2)};
128   std::string h2 {x.substr(2, 2)};
129   std::string h3 {x.substr(4, 2)};
130   std::stringstream ss; ss
131   << esc << "38;2;"
132   << htoi(h1) << ";"
133   << htoi(h2) << ";"
134   << htoi(h3) << "m";
135   return ss.str();
136 }
137 
bg_true(std::string x)138 std::string bg_true(std::string x)
139 {
140   if (! valid_hstr(x)) return {};
141   std::string h1 {x.substr(0, 2)};
142   std::string h2 {x.substr(2, 2)};
143   std::string h3 {x.substr(4, 2)};
144   std::stringstream ss; ss
145   << esc << "48;2;"
146   << htoi(h1) << ";"
147   << htoi(h2) << ";"
148   << htoi(h3) << "m";
149   return ss.str();
150 }
151 
cursor_set(size_t x,size_t y)152 std::string cursor_set(size_t x, size_t y)
153 {
154   std::stringstream ss;
155   ss << esc << y << ";" << x << "H";
156   return ss.str();
157 }
158 
159 } // namespace ANSI_Escape_Codes
160 
161 } // namespace OB
162