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 <list>
36#include <string>
37
38#include <drain/TextStyle.h>
39
40#include "EnumFlagger.h"
41
42namespace drain
43{
44
45
46// Compare this with TextStyle. Join or separate?
47
49
54
55protected:
56
57 // input sep
58 std::string separator;
59
60public:
61
62 //enum Colour {NONE=0, DEFAULT=1, BLACK=2, GRAY=4, RED=8, GREEN=16, YELLOW=32, BLUE=64, PURPLE=128, CYAN=256, WHITE=512};
63
65
67
69
70
72
77 StyleFlags style;
78 LineFlag line;
79 //std::map<std::type_info,size_t> state;
80
81 inline
82 TextDecorator() : separator(","), color(TextStyle::DEFAULT_COLOR){
83 reset(); // ??
84 }
85
86 inline
87 TextDecorator(const TextDecorator & deco) :separator(deco.separator), color(deco.color), style(deco.style), line(deco.line){
88 reset(); // ??
89 }
90
91 virtual inline
92 ~TextDecorator(){
93 //reset(); ???
94 }
95
96 inline
97 std::ostream & begin(std::ostream & ostr) const {
98 return _begin(ostr);
99 }
100
101 inline
102 std::ostream & begin(std::ostream & ostr, const std::string & conf){
103 set(conf);
104 return _begin(ostr);
105 }
106
107
108 inline
109 std::ostream & end(std::ostream & ostr) const {
110 return _end(ostr);
111 }
112
113 inline
114 void setSeparator(const std::string & separator){
115 this->separator = separator;
116 }
117
118 //std::list<>
119 void reset(){
120 style.reset();
121 color.reset();
122 line.reset();
123 }
124
125 template<typename T, typename ... TT>
126 void set(T arg, const TT &... args) {
127 //set(arg);
128 reset();
129 add(arg, args...);
130 };
131
133 /*
134 template<typename ... TT>
135 void set(const std::string & keys, const TT &... args){
136 reset();
137 addKeys(keys);
138 add(args...);
139 }
140 */
141
143
146 template<typename ... TT>
147 inline
148 void add(const std::string & keys, const TT &... args){
149 // std::cout << "Handling1: " << keys << '\n';
150 addKeys(keys); // string handler
151 add(args...);
152 }
153
155 template<typename ... TT>
156 inline
157 void add(const char *keys, const TT &... args){
158 // std::cout << "Handling2: " << keys << '\n';
159 addKeys(keys); // string handler
160 add(args...);
161 }
162
164 template<typename ... TT>
165 inline
166 void add(char *keys, const TT &... args){
167 // std::cout << "Handling3: " << keys << '\n';
168 addKeys(keys); // string handler
169 add(args...);
170 }
171
173 template<typename T, typename ... TT>
174 void add(T arg, const TT &... args) {
175 // std::cout << "Adding:" << arg << ':' << typeid(arg).name() << '\n';
176 add(arg);
177 add(args...);
178 };
179
181 inline
182 void add(TextStyle::Colour c){
183 color.set(c);
184 }
185
187 inline
188 void add(TextStyle::Line l){
189 line.set(l);
190 }
191
193 inline
194 void add(TextStyle::Style s){
195 style.add(s);
196 }
197
198
199
200
201 void debug(std::ostream & ostr) const;
202
203 //std::string str;
204
205protected:
206
207
208 virtual inline
209 std::ostream & _begin(std::ostream & ostr) const {
210 return ostr;
211 }
212
213 virtual inline
214 std::ostream & _end(std::ostream & ostr) const {
215 return ostr;
216 }
217
218
219 inline
220 void set(){}; // could be reset? No! This is the last one called..
221
222 inline
223 void add(){};
224
225 void addKey(const std::string & key);
226
227 void addKeys(const std::string & keys);
228
229};
230
231
232
233inline
234std::ostream & operator<<(std::ostream & ostr, const TextDecorator & decorator){
235 //decorator.debug(ostr);
236 ostr << decorator.style << '=' << decorator.style.getValue();
237 ostr << ':';
238 ostr << decorator.color << '=' << decorator.color.getValue();
239 ostr << ':';
240 ostr << decorator.line << '=' << decorator.line.getValue();
241 return ostr;
242}
243
244
245
247
248public:
249
250 virtual inline
252 }
253
255
260 template <typename F>
261 static
262 void appendCodes(const EnumFlagger<F> & styleFlags, std::list<int> & codes);
263
264
265protected:
266
267 virtual
268 std::ostream & _begin(std::ostream & ostr) const;
269
270 virtual
271 std::ostream & _end(std::ostream & ostr) const;
272
273
274};
275
276template <typename F>
277void TextDecoratorVt100::appendCodes(const EnumFlagger<F> & styleFlags, std::list<int> & codes){
278
279 if (styleFlags){ // is non-zero
280
281 std::list<typename EnumFlagger<F>::value_t> l;
282
283 FlagResolver::valuesToList(styleFlags.value, styleFlags.getDict(), l);
284
285 for (typename EnumFlagger<F>::value_t v: l){
286 codes.push_back(TextStyleVT100::getIntCode(v));
287 }
288
289 }
290
291}
292
293
294} // ::drain
295
296#endif
Flagger accepting values of enum type E.
Definition EnumFlagger.h:56
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:311
void add(const V &arg, const VV &... args)
Add bit values.
Definition Flags.h:293
Definition TextDecorator.h:246
virtual std::ostream & _begin(std::ostream &ostr) const
Export style to VT100 numeric codes.
Definition TextDecorator.cpp:161
static void appendCodes(const EnumFlagger< F > &styleFlags, std::list< int > &codes)
Internal utility: convert given abstract style to numeric VT100 codes.
Definition TextDecorator.h:277
Utility for ...
Definition TextDecorator.h:53
void add(TextStyle::Line l)
Change the current line setting.
Definition TextDecorator.h:188
void add(const std::string &keys, const TT &... args)
Sets given keys.
Definition TextDecorator.h:148
void add(T arg, const TT &... args)
Adds several keys.
Definition TextDecorator.h:174
ColourFlag color
Read input stream until any char in endChars is encountered. The end char will not be included,...
Definition TextDecorator.h:76
void add(char *keys, const TT &... args)
Third case of strings...
Definition TextDecorator.h:166
void addKey(const std::string &key)
Definition TextDecorator.cpp:102
void add(const char *keys, const TT &... args)
Second case of strings...
Definition TextDecorator.h:157
void add(TextStyle::Colour c)
Change the current color setting.
Definition TextDecorator.h:182
void add(TextStyle::Style s)
Change the current current style setting.
Definition TextDecorator.h:194
static int getIntCode(const E &enumCode)
Given an enum value, returns the corresponding numeric VT100 code.
Definition TextStyleVT100.h:101
Utility for scanning text segments.
Definition TextStyle.h:49
Definition DataSelector.cpp:1277