1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2013 Nils Maier
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 
36 #ifndef D_COLORIZED_STREAM_H
37 #define D_COLORIZED_STREAM_H
38 
39 #include <string>
40 #include <sstream>
41 #include <deque>
42 
43 #include "config.h"
44 
45 namespace aria2 {
46 
47 namespace colors {
48 
49 class Color {
50 private:
51   std::string str_;
52 
53 public:
Color(const char * str)54   explicit Color(const char* str) : str_(std::string("\033[") + str + "m") {}
55 
str()56   const std::string& str() const { return str_; }
57 };
58 
59 extern const Color black;
60 extern const Color red;
61 extern const Color green;
62 extern const Color yellow;
63 extern const Color blue;
64 extern const Color magenta;
65 extern const Color cyan;
66 extern const Color white;
67 
68 extern const Color lightred;
69 extern const Color lightgreen;
70 extern const Color lightyellow;
71 extern const Color lightblue;
72 extern const Color lightmagenta;
73 extern const Color lightcyan;
74 extern const Color lightwhite;
75 
76 extern const Color clear;
77 
78 } // namespace colors
79 
80 typedef std::char_traits<char> traits_t;
81 class ColorizedStreamBuf : public std::basic_streambuf<char, traits_t> {
82   enum part_t { eColor, eString };
83   typedef std::pair<part_t, std::string> elem_t;
84   typedef std::deque<elem_t> elems_t;
85   elems_t elems;
86 
87 public:
ColorizedStreamBuf()88   ColorizedStreamBuf() { elems.push_back(std::make_pair(eString, "")); }
89 
setColor(const colors::Color & color)90   void setColor(const colors::Color& color)
91   {
92     elems.push_back(std::make_pair(eColor, color.str()));
93     elems.push_back(std::make_pair(eString, ""));
94   }
95 
overflow(traits_t::int_type c)96   traits_t::int_type overflow(traits_t::int_type c) CXX11_OVERRIDE
97   {
98     elems.back().second.push_back((char)c);
99     return std::char_traits<char>::not_eof(c);
100   }
101 
append(const std::string & str)102   void append(const std::string& str) { elems.back().second += str; }
103 
append(const char * str)104   void append(const char* str) { elems.back().second += str; }
105 
106   std::string str(bool color) const;
107   std::string str(bool color, size_t max) const;
108 };
109 
110 class ColorizedStream : public std::basic_ostream<char, traits_t> {
111 public:
ColorizedStream()112   ColorizedStream()
113       : std::basic_ios<char, traits_t>(&buf),
114         std::basic_ostream<char, traits_t>(&buf)
115   {
116     init(&buf);
117   }
118 
setColor(const colors::Color & color)119   void setColor(const colors::Color& color) { buf.setColor(color); }
append(const std::string & str)120   void append(const std::string& str) { buf.append(str); }
append(const char * str)121   void append(const char* str) { buf.append(str); }
122 
str(bool colors)123   std::string str(bool colors) const { return buf.str(colors); }
str(bool colors,size_t max)124   std::string str(bool colors, size_t max) const
125   {
126     return buf.str(colors, max);
127   }
128 
129 private:
130   ColorizedStreamBuf buf;
131 };
132 
133 inline ColorizedStream& operator<<(ColorizedStream& stream,
134                                    const std::string& str)
135 {
136   stream.append(str);
137   return stream;
138 }
139 
140 inline ColorizedStream& operator<<(ColorizedStream& stream, const char* str)
141 {
142   stream.append(str);
143   return stream;
144 }
145 
146 inline ColorizedStream& operator<<(ColorizedStream& stream,
147                                    const colors::Color& c)
148 {
149   stream.setColor(c);
150   return stream;
151 }
152 
153 } // namespace aria2
154 
155 #endif // D_COLORIZED_STREAM_H
156