Loading...
Searching...
No Matches
TextStyleVT100.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 DRAIN_TEXT_STYLE_VT100
32#define DRAIN_TEXT_STYLE_VT100
33
34#include <iostream>
35#include <map>
36#include <set>
37
38#include "StringBuilder.h"
39#include "TextStyle.h"
40
41namespace drain
42{
43
44
45class TextStyleVT100 : public TextStyle {
46
47public:
48
49 template <typename ... T>
50 inline
51 TextStyleVT100(const T &... args){
52 //set(args...);
53 };
54
55 inline virtual
57
58 template <typename ... TT>
59 void write(std::ostream & ostr, const TT &... args) const{
60 startWrite(ostr);
61 append(ostr, args...);
62 endWrite(ostr);
63 };
64
65
66 virtual
67 void startWrite(std::ostream & ostr) const {
68 }
69
70 virtual
71 void endWrite(std::ostream & ostr) const {
72 ostr << "\033[0m"; // VT100 end marker
73 }
74
75 template <typename ... TT>
76 void append(std::ostream & ostr, const TT &... args) const{
77 _append(ostr, false, args...);
78 };
79
80
81 typedef std::map<drain::TextStyle::Colour,int> color_codemap_t;
82 typedef std::map<drain::TextStyle::Style,int> style_codemap_t;
83 typedef std::map<drain::TextStyle::Line,int> line_codemap_t;
84
85 static
86 const color_codemap_t color_codemap;
87
88 static
89 const style_codemap_t style_codemap;
90
91 static
92 const line_codemap_t line_codemap;
93
94 template <class T>
95 static
96 const std::map<T,int> & getCodeMap();
97
98
100 template <class E>
101 static
102 int getIntCode(const E & enumCode){
103
104 typedef std::map<E,int> codemap_t;
105
106 const codemap_t & map = getCodeMap<E>();
107
108 typename codemap_t::const_iterator it = map.find(enumCode);
109 if (it != map.end()){
110 return it->second;
111 }
112 else {
113
114 for (const auto & entry: map){
115 std::cerr << entry.first << '=' << (int)entry.first << " VT100=" << entry.second << std::endl;
116 }
117
118
119 std::cerr << StringBuilder<>(TypeName<E>::str(), ": no such enumCode: ", enumCode) << std::endl;
120
121 std::cerr << __FILE__ << '/' << __FUNCTION__ << ": no such enumCode: " << enumCode << std::endl;
122 throw std::runtime_error("No such enumCode: "); // TYPE!
123 return 0; // drain::TextDecorator::Colour::NO_COLOR;
124 }
125 }
126
127protected:
128
133 template <typename T, typename ... TT>
134 // static
135 void _append(std::ostream & ostr, bool init, const T & arg, const TT &... args) const{
136 if (init){
137 _appendControlSuffix(ostr);
138 }
139 ostr << arg;
140 _append(ostr, false, args...);
141 };
142
143 template <typename ... TT>
144 // static
145 void _append(std::ostream & ostr, bool start, const Colour & colour, const TT &... args) const{
146 _appendControlPrefix(ostr, start);
147 ostr << getIntCode(colour);
148 _append(ostr, true, args...);
149 };
150
151 template <typename ... TT>
152 // static
153 void _append(std::ostream & ostr, bool start, const Line & line, const TT &... args) const{
154 _appendControlPrefix(ostr, start);
155 ostr << getIntCode(line);
156 _append(ostr, true, args...);
157 };
158
159 template <typename ... TT>
160 // static
161 void _append(std::ostream & ostr, bool start, const Style & style, const TT &... args) const{
162 _appendControlPrefix(ostr, start);
163 ostr << getIntCode(style);
164 _append(ostr, true, args...);
165 };
166
167 inline
168 void _append(std::ostream & ostr, bool init) const{
169 if (init){
170 _appendControlSuffix(ostr);
171 }
172 };
173
174 virtual
175 void _appendControlPrefix(std::ostream & ostr, bool start) const {
176 if (!start){
177 ostr << "\033["; // start VT100 def
178 }
179 else {
180 ostr << ';'; // separator; continuing VT100 defs
181 }
182 }
183
184 virtual
185 void _appendControlSuffix(std::ostream & ostr) const {
186 ostr << 'm'; // VT100 end marker
187 }
188
189
190
191};
192
193
194
195template <>
196const std::map<TextStyle::Colour,int> & TextStyleVT100::getCodeMap();
197
198template <>
199const std::map<TextStyle::Line,int> & TextStyleVT100::getCodeMap();
200
201template <>
202const std::map<TextStyle::Style,int> & TextStyleVT100::getCodeMap();
203
204inline
205std::ostream & operator<<(std::ostream & ostr, const TextStyleVT100 &t){
206 ostr << __FILE__ << ':' << __LINE__ << '?';
207 return ostr;
208}
209
210DRAIN_TYPENAME(TextStyleVT100);
211
212
213} // ::drain
214
215#endif
Definition TextStyleVT100.h:45
void _append(std::ostream &ostr, bool init, const T &arg, const TT &... args) const
Definition TextStyleVT100.h:135
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
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.
Definition Type.h:542