11#pragma  once
22
33#include  < string> 
4- //  TODO: remove include
5- #include  " private/platform.hpp" 
6- 
4+ #include  " cpp-terminal/private/platform.hpp" 
75namespace  Term  {
8- 
9- enum  class  style  {
10-     reset = 0 ,
11-     bold = 1 ,
12-     dim = 2 ,
13-     italic = 3 ,
14-     underline = 4 ,
15-     blink = 5 ,
16-     blink_rapid = 6 ,
17-     reversed = 7 ,
18-     conceal = 8 ,
19-     crossed = 9 ,
20-     overline = 53 
6+ /* 
7+  * The 3bit/4bit colors for the terminal 
8+  * get the foreground color: Color4 + 30, Background color: Color4 + 40 
9+  * See https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit 
10+  */  
11+ enum  class  Color4  : std::uint8_t  {
12+     //  FG: 30, BG: 40
13+     BLACK = 0 ,
14+     //  FG: 31, BG: 41
15+     RED = 1 ,
16+     //  FG: 32, BG: 42
17+     GREEN = 2 ,
18+     //  FG: 33, BG: 43
19+     YELLOW = 3 ,
20+     //  FG: 34, BG: 44
21+     BLUE = 4 ,
22+     //  FG: 35, BG: 45
23+     MAGENTA = 5 ,
24+     //  FG: 36, BG: 46
25+     CYAN = 6 ,
26+     //  FG: 37, BG: 47
27+     WHITE = 7 ,
28+     //  Use the default terminal color, FG: 39, BG: 49
29+     DEFAULT = 9 ,
30+     //  FG: 90, BG: 100
31+     GRAY = 60 ,
32+     //  FG: 91, BG: 101
33+     RED_BRIGHT = 61 ,
34+     //  FG: 92, BG: 102
35+     GREEN_BRIGHT = 62 ,
36+     //  FG: 93, BG: 103
37+     YELLOW_BRIGHT = 63 ,
38+     //  FG: 94, BG: 104
39+     BLUE_BRIGHT = 64 ,
40+     //  FG: 95, BG: 105
41+     MAGENTA_BRIGHT = 65 ,
42+     //  FG: 96, BG: 106
43+     CYAN_BRIGHT = 66 ,
44+     //  FG: 97, BG: 107
45+     WHITE_BRIGHT = 67 
2146};
22- 
23- enum  class  fg  {
24-     black = 30 ,
25-     red = 31 ,
26-     green = 32 ,
27-     yellow = 33 ,
28-     blue = 34 ,
29-     magenta = 35 ,
30-     cyan = 36 ,
31-     white = 37 ,
32-     reset = 39 ,
33-     gray = 90 ,
34-     bright_red = 91 ,
35-     bright_green = 92 ,
36-     bright_yellow = 93 ,
37-     bright_blue = 94 ,
38-     bright_magenta = 95 ,
39-     bright_cyan = 96 ,
40-     bright_white = 97 
47+ /* 
48+  * Styles for text in the terminal 
49+  */  
50+ enum  class  Style  : std::uint8_t  {
51+     //  resets all attributes (styles and colors)
52+     RESET = 0 ,
53+     //  Thick text font
54+     BOLD = 1 ,
55+     //  lighter, slimmer text font
56+     DIM = 2 ,
57+     //  slightly bend text font
58+     ITALIC = 3 ,
59+     //  draws a line below the text
60+     UNDERLINE = 4 ,
61+     BLINK = 5 ,
62+     BLINK_RAPID = 6 ,
63+     REVERSED = 7 ,
64+     CONCEAL = 8 ,
65+     //  strikes through the text, mostly supported
66+     CROSSED = 9 ,
67+     //  draws a line over the text, barely supported
68+     OVERLINE = 53 
4169};
4270
43- enum  class  bg  {
44-     black = 40 ,
45-     red = 41 ,
46-     green = 42 ,
47-     yellow = 43 ,
48-     blue = 44 ,
49-     magenta = 45 ,
50-     cyan = 46 ,
51-     white = 47 ,
52-     reset = 49 ,
53-     gray = 100 ,
54-     bright_red = 101 ,
55-     bright_green = 102 ,
56-     bright_yellow = 103 ,
57-     bright_blue = 104 ,
58-     bright_magenta = 105 ,
59-     bright_cyan = 106 ,
60-     bright_white = 107 
71+ //  Represents a RGB (24bit) color
72+ struct  RGB  {
73+     std::uint8_t  r{}, g{}, b{};
74+     bool  empty = false ;
75+ };
76+ //  indicates the color mode (basically the original color resolution)
77+ //  also used to manually override the original color resolution
78+ enum  class  Mode  {
79+     //  no color was used
80+     UNSET,
81+     //  a 4bit color was used
82+     BIT4,
83+     //  a 8bit color was used
84+     BIT8,
85+     //  a 24bit (RGB) color was used
86+     BIT24,
87+     //  use 24bit colors, but fall back to 8bit if unsupported
88+     AUTO24
89+ };
90+ //  represents an RGB foreground and background color
91+ struct  RGBF  {
92+     RGB rgb_fg;
93+     Mode mode_fg{};
94+     RGB rgb_bg;
95+     Mode mode_bg{};
96+ };
97+ /* 
98+  * reference colors for converting RGB colors to 4bit colors and vice versa 
99+  */  
100+ struct  Bit4_reference  {
101+     static  constexpr  RGB BLACK{0 , 0 , 0 , false };
102+     static  constexpr  RGB RED{151 , 12 , 40 , false };
103+     static  constexpr  RGB GREEN{1 , 142 , 66 , false };
104+     static  constexpr  RGB YELLOW{238 , 198 , 67 , false };
105+     static  constexpr  RGB BLUE{13 , 33 , 161 , false };
106+     static  constexpr  RGB MAGENTA{255 , 0 , 144 , false };
107+     static  constexpr  RGB CYAN{0 , 159 , 184 , false };
108+     static  constexpr  RGB WHITE{240 , 240 , 240 , false };
109+     static  constexpr  RGB GRAY{127 , 127 , 127 , false };
110+     static  constexpr  RGB RED_BRIGHT{241 , 85 , 116 , false };
111+     static  constexpr  RGB GREEN_BRIGHT{52 , 254 , 146 , false };
112+     static  constexpr  RGB YELLOW_BRIGHT{243 , 215 , 124 , false };
113+     static  constexpr  RGB BLUE_BRIGHT{63 , 136 , 197 , false };
114+     static  constexpr  RGB MAGENTA_BRIGHT{255 , 92 , 184 , false };
115+     static  constexpr  RGB CYAN_BRIGHT{51 , 228 , 255 , false };
116+     static  constexpr  RGB WHITE_BRIGHT{255 , 255 , 255 , false };
117+     static  constexpr  RGB NONE{0 , 0 , 0 , true };
61118};
62119
63- std::string color (style);
64- std::string color (fg);
65- std::string color (bg);
66- 
67- std::string color24_fg (unsigned  int , unsigned  int , unsigned  int );
68- 
69- std::string color24_bg (unsigned  int , unsigned  int , unsigned  int );
70- 
71- void  write (const  std::string&);
120+ //  Converts a 4bit color to Term::RGB
121+ RGB bit4_to_rgb (Color4 color);
122+ //  Converts a 8bit color to Term::RGB
123+ RGB bit8_to_rgb (std::uint8_t  color);
124+ //  converts rgb values into Term::RGB
125+ RGB bit24_to_rgb (std::uint8_t  r, std::uint8_t  g, std::uint8_t  b);
126+ //  creates an empty rgb color
127+ RGB rgb_empty ();
72128
73- std::string cursor_off ();
129+ //  compares two Term::RGB colors and returns how much they are different
130+ std::uint16_t  rgb_compare (RGB rgb_first, RGB rgb_second);
74131
75- std::string cursor_on ();
132+ //  Converts an RGB color to a 4bit color
133+ Color4 rgb_to_bit4 (RGB rgb);
134+ //  Converts an RGB color to a 8bit color
135+ uint8_t  rgb_to_bit8 (RGB rgb);
76136
77- std::string clear_screen ();
137+ //  checks if the terminal supports RGB (24bit) colors
138+ bool  bit24_support ();
139+ //  returns ANSI code for 24bit colors, if not supported falls back to 8bit
140+ std::string rgb_to_bit24_auto_fg (RGB color);
141+ //  returns ANSI code for 24bit colors, if not supported falls back to 8bit
142+ std::string rgb_to_bit24_auto_bg (RGB color);
78143
79- //  clears screen + scroll back buffer
80- std::string clear_screen_buffer ();
144+ //  Set the given 4bit color from Term::Color4
145+ std::string color_fg (Color4 color);
146+ //  Set the given 4bit / 8bit color
147+ std::string color_fg (std::uint8_t  color);
148+ //  Set the given 24bit color
149+ std::string color_fg (std::uint8_t  r, std::uint8_t  g, std::uint8_t  b);
150+ //  Set the given RGB (24bit) color
151+ std::string color_fg (RGB rgb);
152+ //  Set the given foreground color from the RGBF struct
153+ std::string color_fg (RGBF rgbf);
154+ //  Set the given foreground color from the RGBF struct with an optional override
155+ std::string color_fg (RGBF rgbf, Mode mode);
81156
82- //  If an attempt is made to move the cursor out of the window, the result is
83- //  undefined.
84- std::string move_cursor (size_t , size_t );
157+ //  Set the given 4bit color from Term::Color4
158+ std::string color_bg (Color4 color);
159+ //  Set the given 4bit / 8bit color
160+ std::string color_bg (std::uint8_t  color);
161+ //  Set the given 24bit color
162+ std::string color_bg (std::uint8_t  r, std::uint8_t  g, std::uint8_t  b);
163+ //  Set the given RGB (24bit) color
164+ std::string color_bg (RGB rgb);
165+ //  Set the given background color from the RGBF struct
166+ std::string color_bg (RGBF rgbf);
167+ //  Set the given background color from the RGBF struct with an optional override
168+ std::string color_bg (RGBF rgbf, Mode mode);
85169
86- //  If an attempt is made to move the cursor to the right of the right margin,
87- //  the cursor stops at the right margin.
88- std::string move_cursor_right (int );
170+ std::string style (Style style);
89171
90- //  If an attempt is made to move the cursor below the bottom margin, the cursor
91- //  stops at the bottom margin.
92- std::string move_cursor_down (int );
172+ //  prints the given Term::RGBF color in its original color mode
173+ std::string colorf (RGBF rgbf);
93174
94- std::string cursor_position_report ();
175+ //  Create a Term::RGBF color using a 4bit foreground color
176+ RGBF rgbf_fg (Color4 color);
177+ //  Create a Term::RGBF color using a 24bit (RGB) foreground color
178+ RGBF rgbf_fg (std::uint8_t  r, std::uint8_t  g, std::uint8_t  b);
179+ //  Create a Term::RGBF color using a Term::RGB foreground color
180+ RGBF rgbf_fg (RGB rgb);
95181
96- std::string erase_to_eol ();
182+ //  Create a Term::RGBF color using a 4bit background color
183+ RGBF rgbf_bg (Color4 color);
184+ //  Create a Term::RGBF color using a 24bit (RGB) background color
185+ RGBF rgbf_bg (std::uint8_t  r, std::uint8_t  g, std::uint8_t  b);
186+ //  Create a Term::RGBF color using a Term::RGB background color
187+ RGBF rgbf_bg (RGB rgb);
97188
98- bool  is_stdin_a_tty ();
99- bool  is_stdout_a_tty ();
100- bool  get_term_size (int &, int &);
189+ //  Create a Term::RGBF color using a 4bit foreground and background color
190+ RGBF rgbf_fb (Color4 fg, Color4 bg);
191+ //  Create a Term::RGBF color using a 24bit (RGB) fore- and background color
192+ RGBF rgbf_fb (std::uint8_t  r_fg,
193+              std::uint8_t  g_fg,
194+              std::uint8_t  b_fg,
195+              std::uint8_t  r_bg,
196+              std::uint8_t  g_bg,
197+              std::uint8_t  b_bg);
198+ //  Create a Term::RGBF color using a Term::RGB fore- and background color
199+ RGBF rgbf_fb (RGB rgb_fg, RGB rgb_bg);
101200
102- std::string restore_screen ();
201+ //  Create an empty Term::RGBF color for disabling colors completely
202+ RGBF rgbf_empty ();
103203
104- std::string save_screen ();
204+ //  Print the given Term::RGBF color in Term::Mode::AUTO
205+ std::string color_auto (RGBF rgbf);
206+ //  print the given Term::RGBF color in the given color mode
207+ std::string color_auto (RGBF rgbf, Mode mode);
105208
106- void  get_cursor_position (int &, int &);
209+ //  get the terminal size (row, column) / (Y, X)
210+ std::tuple<std::size_t , std::size_t > get_size ();
211+ //  check if stdin is connected to a TTY
212+ bool  stdin_connected ();
213+ //  check if stdout is connected to a TTY
214+ bool  stdout_connected ();
215+ //  turn off the cursor
216+ std::string cursor_off ();
217+ //  turn on the cursor
218+ std::string cursor_on ();
219+ //  clear the screen
220+ std::string clear_screen ();
221+ //  clear the screen and the scroll-back buffer
222+ std::string clear_buffer ();
223+ //  move the cursor to the given (row, column) / (Y, X)
224+ std::string cursor_move (std::size_t  row, std::size_t  column);
225+ //  move the cursor the given rows up
226+ std::string cursor_up (std::size_t  rows);
227+ //  move the cursor the given rows down
228+ std::string cursor_down (std::size_t  rows);
229+ //  move the cursor the given columns left
230+ std::string cursor_left (std::size_t  columns);
231+ //  move the cursor the given columns right
232+ std::string cursor_right (std::size_t  columns);
233+ //  returns the current cursor position (row, column) (Y, X)
234+ std::tuple<std::size_t , std::size_t > cursor_position ();
235+ //  the ANSI code to generate a cursor position report
236+ std::string cursor_position_report ();
237+ //  clears the screen from the current cursor position to the end of the screen
238+ std::string clear_eol ();
239+ //  save the current terminal state
240+ std::string screen_save ();
241+ //  load a previously saved terminal state
242+ std::string screen_load ();
243+ //  change the title of the terminal, only supported by a few terminals
244+ std::string terminal_title (const  std::string& title);
107245
108246//  initializes the terminal
109247class  Terminal  : public  Private ::BaseTerminal {
@@ -121,5 +259,4 @@ class Terminal : public Private::BaseTerminal {
121259
122260    ~Terminal () override ;
123261};
124- 
125262}  //  namespace Term
0 commit comments