Go to the documentation of this file.00001 #ifndef __HAYAI_CONSOLE
00002 #define __HAYAI_CONSOLE
00003
00004 #include <iostream>
00005
00006 #if !defined(HAYAI_NO_COLOR)
00007 # if defined(_WIN32)
00008 # ifndef NOMINMAX
00009 # define NOMINMAX
00010 # endif
00011 # include <windows.h>
00012 # else
00013 # include <unistd.h>
00014 # include <cstdio>
00015 # endif
00016 #endif
00017
00018
00019 namespace hayai
00020 {
00022 class Console
00023 {
00024 public:
00026 enum TextColor
00027 {
00029 TextDefault,
00030
00034 TextBlack,
00035
00037 TextBlue,
00038
00040 TextGreen,
00041
00043 TextCyan,
00044
00046 TextRed,
00047
00049 TextPurple,
00050
00052 TextYellow,
00053
00057 TextWhite
00058 };
00059
00060
00062
00065 inline static Console& Instance()
00066 {
00067 static Console singleton;
00068 return singleton;
00069 }
00070
00071
00073 inline static bool IsFormattingEnabled()
00074 {
00075 return Instance()._formattingEnabled;
00076 }
00077
00078
00080 inline static void SetFormattingEnabled(bool enabled)
00081 {
00082 Instance()._formattingEnabled = enabled;
00083 }
00084 private:
00085 inline Console()
00086 : _formattingEnabled(true)
00087 {
00088
00089 }
00090
00091
00092 bool _formattingEnabled;
00093 };
00094
00095 #if defined(_WIN32) && !defined(HAYAI_NO_COLOR) // Windows
00096 static inline WORD GetConsoleAttributes()
00097 {
00098 CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
00099 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
00100 &consoleInfo);
00101 return consoleInfo.wAttributes;
00102 }
00103
00104 inline std::ostream& operator <<(std::ostream& stream,
00105 const Console::TextColor& color)
00106 {
00107 static const WORD defaultConsoleAttributes =
00108 GetConsoleAttributes();
00109 WORD newColor;
00110
00111 if ((!Console::IsFormattingEnabled()) ||
00112 ((stream.rdbuf() != std::cout.rdbuf()) &&
00113 (stream.rdbuf() != std::cerr.rdbuf())))
00114 {
00115 return stream;
00116 }
00117
00118 switch (color)
00119 {
00120 case Console::TextDefault:
00121 newColor = defaultConsoleAttributes;
00122 break;
00123
00124 case Console::TextBlack:
00125 newColor = 0;
00126 break;
00127
00128 case Console::TextBlue:
00129 newColor = FOREGROUND_BLUE;
00130 break;
00131
00132 case Console::TextGreen:
00133 newColor = FOREGROUND_GREEN;
00134 break;
00135
00136 case Console::TextCyan:
00137 newColor = FOREGROUND_GREEN | FOREGROUND_BLUE;
00138 break;
00139
00140 case Console::TextRed:
00141 newColor = FOREGROUND_RED;
00142 break;
00143
00144 case Console::TextPurple:
00145 newColor = FOREGROUND_RED | FOREGROUND_BLUE;
00146 break;
00147
00148 case Console::TextYellow:
00149 newColor =
00150 FOREGROUND_RED |
00151 FOREGROUND_GREEN |
00152 FOREGROUND_INTENSITY;
00153 break;
00154
00155 case Console::TextWhite:
00156 newColor =
00157 FOREGROUND_RED |
00158 FOREGROUND_GREEN |
00159 FOREGROUND_BLUE |
00160 FOREGROUND_INTENSITY;
00161 break;
00162 }
00163
00164 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), newColor);
00165 return stream;
00166 }
00167 #elif !defined(HAYAI_NO_COLOR) // Linux or others
00168 inline std::ostream& operator <<(std::ostream& stream,
00169 const Console::TextColor& color)
00170 {
00171 static const bool outputNoColor = isatty(fileno(stdout)) != 1;
00172
00173 if ((!Console::IsFormattingEnabled()) ||
00174 (outputNoColor) ||
00175 ((stream.rdbuf() != std::cout.rdbuf()) &&
00176 (stream.rdbuf() != std::cerr.rdbuf())))
00177 {
00178 return stream;
00179 }
00180
00181 const char* value = "";
00182
00183 switch (color)
00184 {
00185 case Console::TextDefault:
00186 value = "\033[m"; break;
00187
00188 case Console::TextBlack:
00189 value = "\033[0;30m"; break;
00190
00191 case Console::TextBlue:
00192 value = "\033[0;34m"; break;
00193
00194 case Console::TextGreen:
00195 value = "\033[0;32m"; break;
00196
00197 case Console::TextCyan:
00198 value = "\033[0;36m"; break;
00199
00200 case Console::TextRed:
00201 value = "\033[0;31m"; break;
00202
00203 case Console::TextPurple:
00204 value = "\033[0;35m"; break;
00205
00206 case Console::TextYellow:
00207 value = "\033[0;33m"; break;
00208
00209 case Console::TextWhite:
00210 value = "\033[0;37m"; break;
00211 }
00212
00213 return stream << value;
00214 }
00215 #else // No color
00216 inline std::ostream& operator <<(std::ostream& stream,
00217 const Console::TextColor&)
00218 {
00219 return stream;
00220 }
00221 #endif
00222 }
00223 #endif