console.hpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 #ifndef ecl_console_TERMCOLOR_HPP_
00013 #define ecl_console_TERMCOLOR_HPP_
00014 
00015 // the following snippet of code detects the current OS and
00016 // defines the appropriate macro that is used to wrap some
00017 // platform specific things
00018 #if defined(_WIN32) || defined(_WIN64)
00019 #   define OS_WINDOWS
00020 #elif defined(__APPLE__)
00021 #   define OS_MACOS
00022 #elif defined(linux) || defined(__linux)
00023 #   define OS_LINUX
00024 #else
00025 #   error "unsupported platform"
00026 #endif
00027 
00028 
00029 // This headers provides the `isatty()`/`fileno()` functions,
00030 // which are used for testing whether a standart stream refers
00031 // to the terminal. As for Windows, we also need WinApi funcs
00032 // for changing colors attributes of the terminal.
00033 #if defined(OS_MACOS) || defined(OS_LINUX)
00034 #   include <unistd.h>
00035 #elif defined(OS_WINDOWS)
00036 #   include <io.h>
00037 #   include <windows.h>
00038 #endif
00039 
00040 #include <iostream>
00041 #include <cstdio>
00042 
00043 namespace ecl
00044 {
00045     // Forward declaration of the `console` namespace.
00046     // All comments are below.
00047     namespace console
00048     {
00049         inline FILE* get_standard_stream(const std::ostream& stream);
00050         inline bool is_atty(const std::ostream& stream);
00051 
00052     #if defined(OS_WINDOWS)
00053         void win_change_attributes(std::ostream& stream, int foreground, int background=-1);
00054     #endif
00055     }
00056 
00057 
00058     inline
00059     std::ostream& reset(std::ostream& stream)
00060     {
00061         if (console::is_atty(stream))
00062         {
00063         #if defined(OS_MACOS) || defined(OS_LINUX)
00064             stream << "\033[00m";
00065         #elif defined(OS_WINDOWS)
00066             console::win_change_attributes(stream, -1, -1);
00067         #endif
00068         }
00069         return stream;
00070     }
00071 
00072 
00073     inline
00074     std::ostream& bold(std::ostream& stream)
00075     {
00076         if (console::is_atty(stream))
00077         {
00078         #if defined(OS_MACOS) || defined(OS_LINUX)
00079             stream << "\033[1m";
00080         #elif defined(OS_WINDOWS)
00081         #endif
00082         }
00083         return stream;
00084     }
00085 
00086 
00087     inline
00088     std::ostream& dark(std::ostream& stream)
00089     {
00090         if (console::is_atty(stream))
00091         {
00092         #if defined(OS_MACOS) || defined(OS_LINUX)
00093             stream << "\033[2m";
00094         #elif defined(OS_WINDOWS)
00095         #endif
00096         }
00097         return stream;
00098     }
00099 
00100 
00101     inline
00102     std::ostream& underline(std::ostream& stream)
00103     {
00104         if (console::is_atty(stream))
00105         {
00106         #if defined(OS_MACOS) || defined(OS_LINUX)
00107             stream << "\033[4m";
00108         #elif defined(OS_WINDOWS)
00109         #endif
00110         }
00111         return stream;
00112     }
00113 
00114 
00115     inline
00116     std::ostream& blink(std::ostream& stream)
00117     {
00118         if (console::is_atty(stream))
00119         {
00120         #if defined(OS_MACOS) || defined(OS_LINUX)
00121             stream << "\033[5m";
00122         #elif defined(OS_WINDOWS)
00123         #endif
00124         }
00125         return stream;
00126     }
00127 
00128 
00129     inline
00130     std::ostream& reverse(std::ostream& stream)
00131     {
00132         if (console::is_atty(stream))
00133         {
00134         #if defined(OS_MACOS) || defined(OS_LINUX)
00135             stream << "\033[7m";
00136         #elif defined(OS_WINDOWS)
00137         #endif
00138         }
00139         return stream;
00140     }
00141 
00142 
00143     inline
00144     std::ostream& concealed(std::ostream& stream)
00145     {
00146         if (console::is_atty(stream))
00147         {
00148         #if defined(OS_MACOS) || defined(OS_LINUX)
00149             stream << "\033[8m";
00150         #elif defined(OS_WINDOWS)
00151         #endif
00152         }
00153         return stream;
00154     }
00155 
00156 
00157     inline
00158     std::ostream& grey(std::ostream& stream)
00159     {
00160         if (console::is_atty(stream))
00161         {
00162         #if defined(OS_MACOS) || defined(OS_LINUX)
00163             stream << "\033[30m";
00164         #elif defined(OS_WINDOWS)
00165             console::win_change_attributes(stream,
00166                 0   // grey (black)
00167             );
00168         #endif
00169         }
00170         return stream;
00171     }
00172 
00173     inline
00174     std::ostream& red(std::ostream& stream)
00175     {
00176         if (console::is_atty(stream))
00177         {
00178         #if defined(OS_MACOS) || defined(OS_LINUX)
00179             stream << "\033[31m";
00180         #elif defined(OS_WINDOWS)
00181             console::win_change_attributes(stream,
00182                 FOREGROUND_RED
00183             );
00184         #endif
00185         }
00186         return stream;
00187     }
00188 
00189     inline
00190     std::ostream& green(std::ostream& stream)
00191     {
00192         if (console::is_atty(stream))
00193         {
00194         #if defined(OS_MACOS) || defined(OS_LINUX)
00195             stream << "\033[32m";
00196         #elif defined(OS_WINDOWS)
00197             console::win_change_attributes(stream,
00198                 FOREGROUND_GREEN
00199             );
00200         #endif
00201         }
00202         return stream;
00203     }
00204 
00205     inline
00206     std::ostream& yellow(std::ostream& stream)
00207     {
00208         if (console::is_atty(stream))
00209         {
00210         #if defined(OS_MACOS) || defined(OS_LINUX)
00211             stream << "\033[33m";
00212         #elif defined(OS_WINDOWS)
00213             console::win_change_attributes(stream,
00214                 FOREGROUND_GREEN | FOREGROUND_RED
00215             );
00216         #endif
00217         }
00218         return stream;
00219     }
00220 
00221     inline
00222     std::ostream& blue(std::ostream& stream)
00223     {
00224         if (console::is_atty(stream))
00225         {
00226         #if defined(OS_MACOS) || defined(OS_LINUX)
00227             stream << "\033[34m";
00228         #elif defined(OS_WINDOWS)
00229             console::win_change_attributes(stream,
00230                 FOREGROUND_BLUE
00231             );
00232         #endif
00233         }
00234         return stream;
00235     }
00236 
00237     inline
00238     std::ostream& magenta(std::ostream& stream)
00239     {
00240         if (console::is_atty(stream))
00241         {
00242         #if defined(OS_MACOS) || defined(OS_LINUX)
00243             stream << "\033[35m";
00244         #elif defined(OS_WINDOWS)
00245             console::win_change_attributes(stream,
00246                 FOREGROUND_BLUE | FOREGROUND_RED
00247             );
00248         #endif
00249         }
00250         return stream;
00251     }
00252 
00253     inline
00254     std::ostream& cyan(std::ostream& stream)
00255     {
00256         if (console::is_atty(stream))
00257         {
00258         #if defined(OS_MACOS) || defined(OS_LINUX)
00259             stream << "\033[36m";
00260         #elif defined(OS_WINDOWS)
00261             console::win_change_attributes(stream,
00262                 FOREGROUND_BLUE | FOREGROUND_GREEN
00263             );
00264         #endif
00265         }
00266         return stream;
00267     }
00268 
00269     inline
00270     std::ostream& white(std::ostream& stream)
00271     {
00272         if (console::is_atty(stream))
00273         {
00274         #if defined(OS_MACOS) || defined(OS_LINUX)
00275             stream << "\033[37m";
00276         #elif defined(OS_WINDOWS)
00277             console::win_change_attributes(stream,
00278                 FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
00279             );
00280         #endif
00281         }
00282         return stream;
00283     }
00284 
00285 
00286 
00287     inline
00288     std::ostream& on_grey(std::ostream& stream)
00289     {
00290         if (console::is_atty(stream))
00291         {
00292         #if defined(OS_MACOS) || defined(OS_LINUX)
00293             stream << "\033[40m";
00294         #elif defined(OS_WINDOWS)
00295             console::win_change_attributes(stream, -1,
00296                 0   // grey (black)
00297             );
00298         #endif
00299         }
00300         return stream;
00301     }
00302 
00303     inline
00304     std::ostream& on_red(std::ostream& stream)
00305     {
00306         if (console::is_atty(stream))
00307         {
00308         #if defined(OS_MACOS) || defined(OS_LINUX)
00309             stream << "\033[41m";
00310         #elif defined(OS_WINDOWS)
00311             console::win_change_attributes(stream, -1,
00312                 BACKGROUND_RED
00313             );
00314         #endif
00315         }
00316         return stream;
00317     }
00318 
00319     inline
00320     std::ostream& on_green(std::ostream& stream)
00321     {
00322         if (console::is_atty(stream))
00323         {
00324         #if defined(OS_MACOS) || defined(OS_LINUX)
00325             stream << "\033[42m";
00326         #elif defined(OS_WINDOWS)
00327             console::win_change_attributes(stream, -1,
00328                 BACKGROUND_GREEN
00329             );
00330         #endif
00331         }
00332         return stream;
00333     }
00334 
00335     inline
00336     std::ostream& on_yellow(std::ostream& stream)
00337     {
00338         if (console::is_atty(stream))
00339         {
00340         #if defined(OS_MACOS) || defined(OS_LINUX)
00341             stream << "\033[43m";
00342         #elif defined(OS_WINDOWS)
00343             console::win_change_attributes(stream, -1,
00344                 BACKGROUND_GREEN | BACKGROUND_RED
00345             );
00346         #endif
00347         }
00348         return stream;
00349     }
00350 
00351     inline
00352     std::ostream& on_blue(std::ostream& stream)
00353     {
00354         if (console::is_atty(stream))
00355         {
00356         #if defined(OS_MACOS) || defined(OS_LINUX)
00357             stream << "\033[44m";
00358         #elif defined(OS_WINDOWS)
00359             console::win_change_attributes(stream, -1,
00360                 BACKGROUND_BLUE
00361             );
00362         #endif
00363         }
00364         return stream;
00365     }
00366 
00367     inline
00368     std::ostream& on_magenta(std::ostream& stream)
00369     {
00370         if (console::is_atty(stream))
00371         {
00372         #if defined(OS_MACOS) || defined(OS_LINUX)
00373             stream << "\033[45m";
00374         #elif defined(OS_WINDOWS)
00375             console::win_change_attributes(stream, -1,
00376                 BACKGROUND_BLUE | BACKGROUND_RED
00377             );
00378         #endif
00379         }
00380         return stream;
00381     }
00382 
00383     inline
00384     std::ostream& on_cyan(std::ostream& stream)
00385     {
00386         if (console::is_atty(stream))
00387         {
00388         #if defined(OS_MACOS) || defined(OS_LINUX)
00389             stream << "\033[46m";
00390         #elif defined(OS_WINDOWS)
00391             console::win_change_attributes(stream, -1,
00392                 BACKGROUND_GREEN | BACKGROUND_BLUE
00393             );
00394         #endif
00395         }
00396         return stream;
00397     }
00398 
00399     inline
00400     std::ostream& on_white(std::ostream& stream)
00401     {
00402         if (console::is_atty(stream))
00403         {
00404         #if defined(OS_MACOS) || defined(OS_LINUX)
00405             stream << "\033[47m";
00406         #elif defined(OS_WINDOWS)
00407             console::win_change_attributes(stream, -1,
00408                 BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
00409             );
00410         #endif
00411         }
00412 
00413         return stream;
00414     }
00415 
00416 
00417 
00422     namespace console
00423     {
00427         inline
00428         FILE* get_standard_stream(const std::ostream& stream)
00429         {
00430             if (&stream == &std::cout)
00431                 return stdout;
00432             else if (&stream == &std::cerr)
00433                 return stderr;
00434             return nullptr;
00435         }
00436 
00437 
00440         inline
00441         bool is_atty(const std::ostream& stream)
00442         {
00443             // DJS : don't worry about strict checking so we can use this with
00444             // stringstreams. Upstream issue at https://github.com/ikalnitsky/termcolor/issues/5
00445             return true;
00446 
00447             FILE* std_stream = get_standard_stream(stream);
00448 
00449 
00450         #if defined(OS_MACOS) || defined(OS_LINUX)
00451             return ::isatty(fileno(std_stream));
00452         #elif defined(OS_WINDOWS)
00453             return ::_isatty(_fileno(std_stream));
00454         #endif
00455         }
00456 
00457 
00458     #if defined(OS_WINDOWS)
00459 
00460 
00461         void win_change_attributes(std::ostream& stream, int foreground, int background)
00462         {
00463             // yeah, i know.. it's ugly, it's windows.
00464             static WORD defaultAttributes = 0;
00465 
00466             // get terminal handle
00467             HANDLE hTerminal = INVALID_HANDLE_VALUE;
00468             if (&stream == &std::cout)
00469                 hTerminal = GetStdHandle(STD_OUTPUT_HANDLE);
00470             else if (&stream == &std::cerr)
00471                 hTerminal = GetStdHandle(STD_ERROR_HANDLE);
00472 
00473             // save default terminal attributes if it unsaved
00474             if (!defaultAttributes)
00475             {
00476                 CONSOLE_SCREEN_BUFFER_INFO info;
00477                 if (!GetConsoleScreenBufferInfo(hTerminal, &info))
00478                     return;
00479                 defaultAttributes = info.wAttributes;
00480             }
00481 
00482             // restore all default settings
00483             if (foreground == -1 && background == -1)
00484             {
00485                 SetConsoleTextAttribute(hTerminal, defaultAttributes);
00486                 return;
00487             }
00488 
00489             // get current settings
00490             CONSOLE_SCREEN_BUFFER_INFO info;
00491             if (!GetConsoleScreenBufferInfo(hTerminal, &info))
00492                 return;
00493 
00494             if (foreground != -1)
00495             {
00496                 info.wAttributes &= ~(info.wAttributes & 0x0F);
00497                 info.wAttributes |= static_cast<WORD>(foreground);
00498             }
00499 
00500             if (background != -1)
00501             {
00502                 info.wAttributes &= ~(info.wAttributes & 0xF0);
00503                 info.wAttributes |= static_cast<WORD>(background);
00504             }
00505 
00506             SetConsoleTextAttribute(hTerminal, info.wAttributes);
00507         }
00508     #endif // OS_WINDOWS
00509 
00510     } // namespace console
00511 
00512 } // namespace ecl
00513 
00514 
00515 #undef OS_WINDOWS
00516 #undef OS_MACOS
00517 #undef OS_LINUX
00518 
00519 #endif // ecl_console_TERMCOLOR_HPP_


ecl_console
Author(s): Daniel Stonier
autogenerated on Thu Jun 16 2016 09:47:13