Loading...
Searching...
No Matches
TextDecorator.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30*/
31#ifndef TEXT_DECORATOR_H_
32#define TEXT_DECORATOR_H_
33
34#include <iostream>
35#include <sstream>
36#include <list>
37#include <string>
38
39#include <drain/TextStyle.h>
40#include <drain/TypeUtils.h>
41
42#include "EnumFlags.h"
43
44namespace drain
45{
46
47
48// Compare this with TextStyle. Join or separate?
49
51
56
57protected:
58
59 // input sep
60 std::string separator;
61
62public:
63
64 //enum Colour {NONE=0, DEFAULT=1, BLACK=2, GRAY=4, RED=8, GREEN=16, YELLOW=32, BLUE=64, PURPLE=128, CYAN=256, WHITE=512};
65
67
69
71
72
74
79 StyleFlags style;
80 LineFlag line;
81 //std::map<std::type_info,size_t> state;
82
83 inline
84 TextDecorator() : separator(","), color(TextStyle::DEFAULT_COLOR){
85 reset(); // ??
86 }
87
88 inline
89 TextDecorator(const TextDecorator & deco) :separator(deco.separator), color(deco.color), style(deco.style), line(deco.line){
90 reset(); // ??
91 }
92
93 virtual inline
94 ~TextDecorator(){
95 //reset(); ???
96 }
97
98 inline
99 std::ostream & begin(std::ostream & ostr) const {
100 return _begin(ostr);
101 }
102
103 inline
104 std::ostream & begin(std::ostream & ostr, const std::string & conf){
105 set(conf);
106 return _begin(ostr);
107 }
108
109
110 inline
111 std::ostream & end(std::ostream & ostr) const {
112 return _end(ostr);
113 }
114
115 inline
116 void setSeparator(const std::string & separator){
117 this->separator = separator;
118 }
119
120 //std::list<>
121 void reset(){
122 style.reset();
123 color.reset();
124 line.reset();
125 }
126
127 template<typename T, typename ... TT>
128 void set(T arg, const TT &... args) {
129 //set(arg);
130 reset();
131 add(arg, args...);
132 };
133
135 /*
136 template<typename ... TT>
137 void set(const std::string & keys, const TT &... args){
138 reset();
139 addKeys(keys);
140 add(args...);
141 }
142 */
143
145
148 template<typename ... TT>
149 inline
150 void add(const std::string & keys, const TT &... args){
151 // std::cout << "Handling1: " << keys << '\n';
152 addKeys(keys); // string handler
153 add(args...);
154 }
155
157 template<typename ... TT>
158 inline
159 void add(const char *keys, const TT &... args){
160 // std::cout << "Handling2: " << keys << '\n';
161 addKeys(keys); // string handler
162 add(args...);
163 }
164
166 template<typename ... TT>
167 inline
168 void add(char *keys, const TT &... args){
169 // std::cout << "Handling3: " << keys << '\n';
170 addKeys(keys); // string handler
171 add(args...);
172 }
173
175 template<typename T, typename ... TT>
176 void add(T arg, const TT &... args) {
177 // std::cout << "Adding:" << arg << ':' << typeid(arg).name() << '\n';
178 add(arg);
179 add(args...);
180 };
181
183 inline
184 void add(TextStyle::Colour c){
185 color.set(c);
186 }
187
189 inline
190 void add(TextStyle::Line l){
191 line.set(l);
192 }
193
195 inline
196 void add(TextStyle::Style s){
197 style.add(s);
198 }
199
200
201
202
203 void debug(std::ostream & ostr) const;
204
205 //std::string str;
206
207protected:
208
209
210 virtual inline
211 std::ostream & _begin(std::ostream & ostr) const {
212 return ostr;
213 }
214
215 virtual inline
216 std::ostream & _end(std::ostream & ostr) const {
217 return ostr;
218 }
219
220
221 inline
222 void set(){}; // could be reset? No! This is the last one called..
223
224 inline
225 void add(){};
226
227 void addKey(const std::string & key);
228
229 void addKeys(const std::string & keys);
230
231};
232
233
234
235inline
236std::ostream & operator<<(std::ostream & ostr, const TextDecorator & decorator){
237 //decorator.debug(ostr);
238 ostr << decorator.style << '=' << decorator.style.getValue();
239 ostr << ':';
240 ostr << decorator.color << '=' << decorator.color.getValue();
241 ostr << ':';
242 ostr << decorator.line << '=' << decorator.line.getValue();
243 return ostr;
244}
245
246
247
249
250public:
251
252 virtual inline
254 }
255
257
262 template <typename F>
263 static
264 void appendCodes(const EnumFlagger<F> & styleFlags, std::list<int> & codes);
265
266
267protected:
268
269 virtual
270 std::ostream & _begin(std::ostream & ostr) const;
271
272 virtual
273 std::ostream & _end(std::ostream & ostr) const;
274
275
276};
277
278template <typename F>
279void TextDecoratorVt100::appendCodes(const EnumFlagger<F> & styleFlags, std::list<int> & codes){
280
281 if (styleFlags){ // is non-zero
282
283 std::list<typename EnumFlagger<F>::value_t> l;
284
285 FlagResolver::valuesToList(styleFlags.value, styleFlags.getDict(), l);
286
287 for (typename EnumFlagger<F>::value_t v: l){
288 codes.push_back(TextStyleVT100::getIntCode(v));
289 }
290
291 }
292
293}
294
295
296} // ::drain
297
298#endif
Flagger accepting values of enum type E.
Definition EnumFlags.h:190
static void valuesToList(ivalue_t value, const drain::Dictionary< key_t, T > &dict, std::list< V > &container)
Given a bit vector (integer value), extracts separate flag values to a list.
Definition FlagBase.h:313
void add(const V &arg, const VV &... args)
Add bit values.
Definition Flags.h:296
Definition TextDecorator.h:248
virtual std::ostream & _begin(std::ostream &ostr) const
Export style to VT100 numeric codes.
Definition TextDecorator.cpp:162
static void appendCodes(const EnumFlagger< F > &styleFlags, std::list< int > &codes)
Internal utility: convert given abstract style to numeric VT100 codes.
Definition TextDecorator.h:279
Utility for ...
Definition TextDecorator.h:55
void add(TextStyle::Line l)
Change the current line setting.
Definition TextDecorator.h:190
void add(const std::string &keys, const TT &... args)
Sets given keys.
Definition TextDecorator.h:150
void add(T arg, const TT &... args)
Adds several keys.
Definition TextDecorator.h:176
ColourFlag color
Read input stream until any char in endChars is encountered. The end char will not be included,...
Definition TextDecorator.h:78
void add(char *keys, const TT &... args)
Third case of strings...
Definition TextDecorator.h:168
void addKey(const std::string &key)
Definition TextDecorator.cpp:103
void add(const char *keys, const TT &... args)
Second case of strings...
Definition TextDecorator.h:159
void add(TextStyle::Colour c)
Change the current color setting.
Definition TextDecorator.h:184
void add(TextStyle::Style s)
Change the current current style setting.
Definition TextDecorator.h:196
static int getIntCode(const E &enumCode)
Given an enum value, returns the corresponding numeric VT100 code.
Definition TextStyleVT100.h:102
Utility for scanning text segments.
Definition TextStyle.h:50
Definition DataSelector.cpp:1277