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