libcsdbg  1.28
C++ exception (and generic) stack trace debug library
style.cpp
Go to the documentation of this file.
1 #include "../include/style.hpp"
2 
9 namespace csdbg {
10 
25 style::style(const i8 *nm, color_t fg, color_t bg, attrset_t set):
26 m_name(NULL),
27 m_fgcolor(fg),
28 m_bgcolor(bg),
29 m_attributes(set)
30 {
31  if ( unlikely(nm == NULL) )
32  throw exception("invalid argument: nm (=%p)", nm);
33 
34  m_name = new i8[strlen(nm) + 1];
35  strcpy(m_name, nm);
36 }
37 
38 
46 style::style(const style &src):
47 m_name(NULL),
48 m_fgcolor(src.m_fgcolor),
49 m_bgcolor(src.m_bgcolor),
50 m_attributes(src.m_attributes)
51 {
52  m_name = new i8[strlen(src.m_name) + 1];
53  strcpy(m_name, src.m_name);
54 }
55 
56 
61 {
62  delete[] m_name;
63  m_name = NULL;
64 }
65 
66 
74 inline style* style::clone() const
75 {
76  return new style(*this);
77 }
78 
79 
85 inline const i8* style::name() const
86 {
87  return m_name;
88 }
89 
90 
96 inline color_t style::fgcolor() const
97 {
98  return m_fgcolor;
99 }
100 
101 
107 inline color_t style::bgcolor() const
108 {
109  return m_bgcolor;
110 }
111 
112 
119 {
120  return m_attributes;
121 }
122 
123 
135 {
136  if ( unlikely(nm == NULL) )
137  throw exception("invalid argument: nm (=%p)", nm);
138 
139  u32 len = strlen(nm);
140  if (len > strlen(m_name)) {
141  delete[] m_name;
142  m_name = NULL;
143  m_name = new i8[len + 1];
144  }
145 
146  strcpy(m_name, nm);
147  return *this;
148 }
149 
150 
159 {
160  m_fgcolor = fg;
161  return *this;
162 }
163 
164 
173 {
174  m_bgcolor = bg;
175  return *this;
176 }
177 
178 
187 {
188  m_attributes = set;
189  return *this;
190 }
191 
192 
203 {
204  if ( unlikely(this == &rval) )
205  return *this;
206 
207  m_fgcolor = rval.m_fgcolor;
208  m_bgcolor = rval.m_bgcolor;
209  m_attributes = rval.m_attributes;
210 
211  return set_name(rval.m_name);
212 }
213 
214 
222 inline bool style::is_attr_enabled(attrset_t set) const
223 {
224  return (m_attributes & set) == set;
225 }
226 
227 
238 {
239  if (how)
240  m_attributes |= set;
241  else
242  m_attributes &= ~set;
243 
244  return *this;
245 }
246 
247 
260 style& style::to_string(string &dst) const
261 {
262  dst.clear();
263 
264  /* Add the background color, if not translucent */
265  if ( unlikely(m_bgcolor != CLEAR) )
266  dst.append("\e[48;5;%dm", m_bgcolor);
267 
268  /* Add the foreground color */
269  dst.append("\e[38;5;%dm", m_fgcolor);
270 
271  /* Add the escape sequence for each text formatting attribute */
272  if ( unlikely(is_attr_enabled(BOLD)) )
273  dst.append("\e[1m");
274 
275  if ( unlikely(is_attr_enabled(DIM)) )
276  dst.append("\e[2m");
277 
279  dst.append("\e[4m");
280 
282  dst.append("\e[5m");
283 
285  dst.append("\e[7m");
286 
288  dst.append("\e[8m");
289 
290  return const_cast<style&> (*this);
291 }
292 
293 
304 style& style::apply(string &dst) const
305 {
306  string esc;
307  to_string(esc);
308  dst.insert(0, esc).append("\e[0m");
309  return const_cast<style&> (*this);
310 }
311 
312 }
313 
virtual style & set_attr_enabled(attrset_t, bool)
Enable/disable a set of text formatting attributes.
Definition: style.cpp:237
virtual string & append(const string &)
Append a string.
Definition: string.cpp:404
virtual style * clone() const
Object virtual copy constructor.
Definition: style.cpp:74
virtual style & set_fgcolor(color_t)
Set the foreground color.
Definition: style.cpp:158
virtual style & to_string(string &) const
Set a string with all the style escape sequences.
Definition: style.cpp:260
char i8
8-bit signed integer
Definition: config.hpp:72
virtual style & apply(string &) const
Apply the style to some text.
Definition: style.cpp:304
A set of formatting attributes for VT100 (and compatible) terminals.
Definition: style.hpp:20
virtual const i8 * name() const
Get the style name.
Definition: style.cpp:85
virtual string & insert(u32, const string &)
Insert a string at a specified position.
Definition: string.cpp:575
u8 color_t
VT100 terminal color.
Definition: config.hpp:162
virtual style & set_name(const i8 *)
Set the style name.
Definition: style.cpp:134
virtual style & operator=(const style &)
Assignment operator.
Definition: style.cpp:202
virtual string & clear()
Clear contents.
Definition: string.cpp:387
virtual attrset_t attributes() const
Get the text formatting attributes.
Definition: style.cpp:118
virtual color_t fgcolor() const
Get the foreground color.
Definition: style.cpp:96
virtual color_t bgcolor() const
Get the background color.
Definition: style.cpp:107
attrset_t m_attributes
Text formatting attribute bitmask.
Definition: style.hpp:32
color_t m_bgcolor
Background color.
Definition: style.hpp:30
virtual style & set_bgcolor(color_t)
Set the background color.
Definition: style.cpp:172
unsigned int u32
32-bit unsigned integer
Definition: config.hpp:102
u16 attrset_t
VT100 attribute bitmask.
Definition: config.hpp:170
virtual bool is_attr_enabled(attrset_t) const
Check if a set of text formatting attributes is enabled.
Definition: style.cpp:222
This class is a throwable with a textual description of an error.
Definition: exception.hpp:26
style(const i8 *, color_t=WHITE, color_t=CLEAR, attrset_t=0)
Object constructor.
Definition: style.cpp:25
color_t m_fgcolor
Foreground (text) color.
Definition: style.hpp:28
virtual style & set_attributes(attrset_t)
Set the text formatting attributes.
Definition: style.cpp:186
#define unlikely(expr)
Offer a hint (negative) to the pipeline branch predictor.
Definition: config.hpp:349
i8 * m_name
Style name.
Definition: style.hpp:26
virtual ~style()
Object destructor.
Definition: style.cpp:60