ColorizedStream.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. #ifndef D_COLORIZED_STREAM_H
  36. #define D_COLORIZED_STREAM_H
  37. #include <string>
  38. #include <sstream>
  39. #include <deque>
  40. #include "config.h"
  41. namespace aria2 {
  42. namespace colors {
  43. class Color {
  44. private:
  45. std::string str_;
  46. public:
  47. explicit Color(const char* str) : str_(std::string("\033[") + str + "m") {}
  48. const std::string& str() const { return str_; }
  49. };
  50. extern const Color black;
  51. extern const Color red;
  52. extern const Color green;
  53. extern const Color yellow;
  54. extern const Color blue;
  55. extern const Color magenta;
  56. extern const Color cyan;
  57. extern const Color white;
  58. extern const Color lightred;
  59. extern const Color lightgreen;
  60. extern const Color lightyellow;
  61. extern const Color lightblue;
  62. extern const Color lightmagenta;
  63. extern const Color lightcyan;
  64. extern const Color lightwhite;
  65. extern const Color clear;
  66. } // namespace colors
  67. typedef std::char_traits<char> traits_t;
  68. class ColorizedStreamBuf : public std::basic_streambuf<char, traits_t> {
  69. enum part_t { eColor, eString };
  70. typedef std::pair<part_t, std::string> elem_t;
  71. typedef std::deque<elem_t> elems_t;
  72. elems_t elems;
  73. public:
  74. ColorizedStreamBuf() { elems.push_back(std::make_pair(eString, "")); }
  75. void setColor(const colors::Color& color)
  76. {
  77. elems.push_back(std::make_pair(eColor, color.str()));
  78. elems.push_back(std::make_pair(eString, ""));
  79. }
  80. traits_t::int_type overflow(traits_t::int_type c) CXX11_OVERRIDE
  81. {
  82. elems.back().second.push_back((char)c);
  83. return std::char_traits<char>::not_eof(c);
  84. }
  85. void append(const std::string& str) { elems.back().second += str; }
  86. void append(const char* str) { elems.back().second += str; }
  87. std::string str(bool color) const;
  88. std::string str(bool color, size_t max) const;
  89. };
  90. class ColorizedStream : public std::basic_ostream<char, traits_t> {
  91. public:
  92. ColorizedStream()
  93. : std::basic_ios<char, traits_t>(&buf),
  94. std::basic_ostream<char, traits_t>(&buf)
  95. {
  96. init(&buf);
  97. }
  98. void setColor(const colors::Color& color) { buf.setColor(color); }
  99. void append(const std::string& str) { buf.append(str); }
  100. void append(const char* str) { buf.append(str); }
  101. std::string str(bool colors) const { return buf.str(colors); }
  102. std::string str(bool colors, size_t max) const
  103. {
  104. return buf.str(colors, max);
  105. }
  106. private:
  107. ColorizedStreamBuf buf;
  108. };
  109. inline ColorizedStream& operator<<(ColorizedStream& stream,
  110. const std::string& str)
  111. {
  112. stream.append(str);
  113. return stream;
  114. }
  115. inline ColorizedStream& operator<<(ColorizedStream& stream, const char* str)
  116. {
  117. stream.append(str);
  118. return stream;
  119. }
  120. inline ColorizedStream& operator<<(ColorizedStream& stream,
  121. const colors::Color& c)
  122. {
  123. stream.setColor(c);
  124. return stream;
  125. }
  126. } // namespace aria2
  127. #endif // D_COLORIZED_STREAM_H