View difference between Paste ID: 9ZX9RCEK and hsMmJsp1
SHOW: | | - or go back to the newest paste.
1
#include <iostream>
2
using std::ostream, std::cout, std::endl;
3
4
const char escape           = char( 27 );
5
const char ansi_prefix[3]   = {escape, '[', '\0'};
6
7
// Based on code from an example at
8
// <url: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences>:
9
#ifdef _WIN32
10
    #include <stdint.h>
11
    namespace winapi {
12
        using BOOL      = int;
13
        using HANDLE    = void*;
14
        using DWORD     = uint32_t;
15
        const auto STD_OUTPUT_HANDLE                    = static_cast<DWORD>( -11 );
16
        const auto ENABLE_VIRTUAL_TERMINAL_PROCESSING   = static_cast<DWORD>( 4 );
17
        const auto INVALID_HANDLE_VALUE                 = reinterpret_cast<HANDLE>( -1 );
18
19
        extern "C" auto __stdcall GetStdHandle( const DWORD ) -> HANDLE;
20
        extern "C" auto __stdcall GetConsoleMode( HANDLE, DWORD* ) -> BOOL;
21
        extern "C" auto __stdcall SetConsoleMode( HANDLE, DWORD ) -> BOOL;
22
23
        inline auto enable_virtual_terminal_mode()
24
            -> bool
25
        {
26
            const HANDLE output_handle = GetStdHandle( STD_OUTPUT_HANDLE );
27
            if( output_handle == INVALID_HANDLE_VALUE) {
28
                return false;
29
            }
30
31
            DWORD mode = 0;
32
            if( not GetConsoleMode( output_handle, &mode ) ){
33
                return false;
34
            }
35
36
            mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
37
            return !!SetConsoleMode( output_handle, mode );
38
        }
39
        
40
        inline const bool colors_are_available = enable_virtual_terminal_mode();
41
    }  // namespace winapi
42
#endif
43
44
// From the same Microsoft page:
45
struct Ansi_color{ enum Enum: int {
46
    original_colors             =   0,          // Returns to default state prior to modification.
47
    bold_or_bright              =   1,          // Applies brightness/intensity flag to foreground color
48
    underline                   =   4,          // Adds underline
49
    no_underline                =  24,          // Removes underline
50
    negative                    =   7,          // Swaps foreground and background colors
51
    positive                    =  27,          // Returns foreground/background to normal
52
    foreground_black            =  30,          // Applies non-bold/bright black to foreground
53
    foreground_red              =  31,          // Applies non-bold/bright red to foreground
54
    foreground_green            =  32,          // Applies non-bold/bright green to foreground
55
    foreground_yellow           =  33,          // Applies non-bold/bright yellow to foreground
56
    foreground_blue             =  34,          // Applies non-bold/bright blue to foreground
57
    foreground_magenta          =  35,          // Applies non-bold/bright magenta to foreground
58
    foreground_cyan             =  36,          // Applies non-bold/bright cyan to foreground
59
    foreground_white            =  37,          // Applies non-bold/bright white to foreground
60
    foreground_extended         =  38,          // Applies extended color value to the foreground (see details below)
61
    foreground_default          =  39,          // Applies only the foreground portion of the defaults (see 0)
62
    background_black            =  40,          // Applies non-bold/bright black to background
63
    background_red              =  41,          // Applies non-bold/bright red to background
64
    background_green            =  42,          // Applies non-bold/bright green to background
65
    background_yellow           =  43,          // Applies non-bold/bright yellow to background
66
    background_blue             =  44,          // Applies non-bold/bright blue to background
67
    background_magenta          =  45,          // Applies non-bold/bright magenta to background
68
    background_cyan             =  46,          // Applies non-bold/bright cyan to background
69
    background_white            =  47,          // Applies non-bold/bright white to background
70
    background_extended         =  48,          // Applies extended color value to the background (see details below)
71
    background_default          =  49,          // Applies only the background portion of the defaults (see 0)
72
    bright_foreground_black     =  90,          // Applies bold/bright black to foreground
73
    bright_foreground_red       =  91,          // Applies bold/bright red to foreground
74
    bright_foreground_green     =  92,          // Applies bold/bright green to foreground
75
    bright_foreground_yellow    =  93,          // Applies bold/bright yellow to foreground
76
    bright_foreground_blue      =  94,          // Applies bold/bright blue to foreground
77
    bright_foreground_magenta   =  95,          // Applies bold/bright magenta to foreground
78
    bright_foreground_cyan      =  96,          // Applies bold/bright cyan to foreground
79
    bright_foreground_white     =  97,          // Applies bold/bright white to foreground
80
    bright_background_black     = 100,          // Applies bold/bright black to background
81
    bright_background_red       = 101,          // Applies bold/bright red to background
82
    bright_background_green     = 102,          // Applies bold/bright green to background
83
    bright_background_yellow    = 103,          // Applies bold/bright yellow to background
84
    bright_background_blue      = 104,          // Applies bold/bright blue to background
85
    bright_background_magenta   = 105,          // Applies bold/bright magenta to background
86
    bright_background_cyan      = 106,          // Applies bold/bright cyan to background
87
    bright_background_white     = 107           // Applies bold/bright white to background
88
}; };
89
90
auto operator<<( ostream& stream, const Ansi_color::Enum code )
91
    -> ostream&
92
{ return stream << ansi_prefix << +code << 'm'; }
93
94
auto main() -> int
95
{
96
    cout << "Colored text:" << endl;
97
    for(    int code = Ansi_color::bright_foreground_red;
98
            code <= Ansi_color::bright_foreground_white;
99
            ++code ) {
100
        cout << Ansi_color::Enum( code ) << "word ";
101
    }
102
    cout << Ansi_color::original_colors << endl;
103
    cout << "And that was that." << endl;
104
}