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/*
44 * To turn off the any text attribute on a VT100 or ANSI-compatible terminal, you must use the
45 * Reset All Attributes command.
46 * The VT100 does not have a specific command to turn off only a single attribute without affecting others.
47 *
48 * NOTE: this does not work like TextStyle. Consider alternative design, say embedding TextStyle as a member.
49 */
50
51class TextStyleVT100 : public TextStyle {
52
53public:
54
55 template <typename ... T>
56 inline
57 TextStyleVT100(const T &... args){
58 //set(args...);
59 }
60
61 inline virtual
63
64 template <typename ... TT>
65 void write(std::ostream & ostr, const TT &... args) const{
66 startWrite(ostr);
67 append(ostr, args...);
68 endWrite(ostr);
69 }
70
71
72 virtual
73 void startWrite(std::ostream & ) const { // ostr
74 }
75
76 virtual
77 void endWrite(std::ostream & ostr) const {
78 ostr << "\033[0m"; // VT100 end marker
79 }
80
81 template <typename ... TT>
82 void append(std::ostream & ostr, const TT &... args) const{
83 _append(ostr, false, args...);
84 }
85
86
87 typedef std::map<drain::TextStyle::Colour,int> color_codemap_t;
88 typedef std::map<drain::TextStyle::Style,int> style_codemap_t;
89 typedef std::map<drain::TextStyle::Line,int> line_codemap_t;
90
91 static
92 const color_codemap_t color_codemap;
93
94 static
95 const style_codemap_t style_codemap;
96
97 static
98 const line_codemap_t line_codemap;
99
100 template <class T>
101 static
102 const std::map<T,int> & getCodeMap();
103
104
106 template <class E>
107 static
108 int getIntCode(const E & enumCode){
109
110 typedef std::map<E,int> codemap_t;
111
112 const codemap_t & map = getCodeMap<E>();
113
114 typename codemap_t::const_iterator it = map.find(enumCode);
115 if (it != map.end()){
116 return it->second;
117 }
118 else {
119
120 for (const auto & entry: map){
121 std::cerr << entry.first << '=' << (int)entry.first << " VT100=" << entry.second << std::endl;
122 }
123
124
125 std::cerr << StringBuilder<>(TypeName<E>::str(), ": no such enumCode: ", enumCode) << std::endl;
126
127 std::cerr << __FILE__ << '/' << __FUNCTION__ << ": no such enumCode: " << enumCode << std::endl;
128 throw std::runtime_error("No such enumCode: "); // TYPE!
129 return 0; // drain::TextDecorator::Colour::NO_COLOR;
130 }
131 }
132
133protected:
134
139 template <typename T, typename ... TT>
140 // static
141 void _append(std::ostream & ostr, bool INIT, const T & arg, const TT &... args) const{
142 if (INIT){
143 _appendControlSuffix(ostr);
144 }
145 ostr << arg;
146 _append(ostr, false, args...);
147 }
148
149 template <typename ... TT>
150 // static
151 void _append(std::ostream & ostr, bool START, const Colour & colour, const TT &... args) const{
152 _appendControlPrefix(ostr, START);
153 ostr << getIntCode(colour);
154 _append(ostr, true, args...);
155 }
156
157 template <typename ... TT>
158 // static
159 void _append(std::ostream & ostr, bool START, const Line & line, const TT &... args) const{
160 _appendControlPrefix(ostr, START);
161 ostr << getIntCode(line);
162 _append(ostr, true, args...);
163 }
164
165 template <typename ... TT>
166 void _append(std::ostream & ostr, bool START, const Style & style, const TT &... args) const {
167 _appendControlPrefix(ostr, START);
168 ostr << getIntCode(style);
169 _append(ostr, true, args...);
170 }
171
172 template <typename ... TT>
173 void _append(std::ostream & ostr, bool START, const Control & ctrl) const {
174 _appendControlSuffix(ostr);
175 }
176
177 inline
178 void _append(std::ostream & ostr, bool init) const{
179 if (init){
180 _appendControlSuffix(ostr);
181 }
182 };
183
184 virtual
185 void _appendControlPrefix(std::ostream & ostr, bool START) const {
186 if (!START){
187 ostr << "\033["; // start VT100 def
188 }
189 else {
190 ostr << ';'; // separator; continuing VT100 defs
191 }
192 }
193
194 virtual
195 void _appendControlSuffix(std::ostream & ostr) const {
196 ostr << 'm'; // VT100 end marker
197 }
198
199
200
201};
202
203
204
205template <>
206const std::map<TextStyle::Colour,int> & TextStyleVT100::getCodeMap();
207
208template <>
209const std::map<TextStyle::Line,int> & TextStyleVT100::getCodeMap();
210
211template <>
212const std::map<TextStyle::Style,int> & TextStyleVT100::getCodeMap();
213
214inline
215std::ostream & operator<<(std::ostream & ostr, const TextStyleVT100 &t){
216 ostr << __FILE__ << ':' << __LINE__ << '?';
217 return ostr;
218}
219
220DRAIN_TYPENAME(TextStyleVT100);
221
222
223} // ::drain
224
225#endif
Definition TextStyleVT100.h:51
void _append(std::ostream &ostr, bool INIT, const T &arg, const TT &... args) const
Definition TextStyleVT100.h:141
static int getIntCode(const E &enumCode)
Given an enum value, returns the corresponding numeric VT100 code.
Definition TextStyleVT100.h:108
Utility for scanning text segments.
Definition TextStyle.h:49
Definition Data.h:1473
std::ostream & operator<<(std::ostream &ostr, const PlainData< DT > &d)
Definition Data.h:631
Default implementation.
Definition TypeName.h:54