Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using std::ostream, std::cout, std::endl;
- const char escape = char( 27 );
- const char ansi_prefix[3] = {escape, '[', '\0'};
- // Based on code from an example at
- // <url: https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences>:
- #ifdef _WIN32
- #include <stdint.h>
- namespace winapi {
- using BOOL = int;
- using HANDLE = void*;
- using DWORD = uint32_t;
- const auto STD_OUTPUT_HANDLE = static_cast<DWORD>( -11 );
- const auto ENABLE_VIRTUAL_TERMINAL_PROCESSING = static_cast<DWORD>( 4 );
- const auto INVALID_HANDLE_VALUE = reinterpret_cast<HANDLE>( -1 );
- extern "C" auto __stdcall GetStdHandle( const DWORD ) -> HANDLE;
- extern "C" auto __stdcall GetConsoleMode( HANDLE, DWORD* ) -> BOOL;
- extern "C" auto __stdcall SetConsoleMode( HANDLE, DWORD ) -> BOOL;
- inline auto enable_virtual_terminal_mode()
- -> bool
- {
- const HANDLE output_handle = GetStdHandle( STD_OUTPUT_HANDLE );
- if( output_handle == INVALID_HANDLE_VALUE) {
- return false;
- }
- DWORD mode = 0;
- if( not GetConsoleMode( output_handle, &mode ) ){
- return false;
- }
- mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
- return !!SetConsoleMode( output_handle, mode );
- }
- inline const bool colors_are_available = enable_virtual_terminal_mode();
- } // namespace winapi
- #endif
- // From the same Microsoft page:
- struct Ansi_color{ enum Enum: int {
- original_colors = 0, // Returns to default state prior to modification.
- bold_or_bright = 1, // Applies brightness/intensity flag to foreground color
- underline = 4, // Adds underline
- no_underline = 24, // Removes underline
- negative = 7, // Swaps foreground and background colors
- positive = 27, // Returns foreground/background to normal
- foreground_black = 30, // Applies non-bold/bright black to foreground
- foreground_red = 31, // Applies non-bold/bright red to foreground
- foreground_green = 32, // Applies non-bold/bright green to foreground
- foreground_yellow = 33, // Applies non-bold/bright yellow to foreground
- foreground_blue = 34, // Applies non-bold/bright blue to foreground
- foreground_magenta = 35, // Applies non-bold/bright magenta to foreground
- foreground_cyan = 36, // Applies non-bold/bright cyan to foreground
- foreground_white = 37, // Applies non-bold/bright white to foreground
- foreground_extended = 38, // Applies extended color value to the foreground (see details below)
- foreground_default = 39, // Applies only the foreground portion of the defaults (see 0)
- background_black = 40, // Applies non-bold/bright black to background
- background_red = 41, // Applies non-bold/bright red to background
- background_green = 42, // Applies non-bold/bright green to background
- background_yellow = 43, // Applies non-bold/bright yellow to background
- background_blue = 44, // Applies non-bold/bright blue to background
- background_magenta = 45, // Applies non-bold/bright magenta to background
- background_cyan = 46, // Applies non-bold/bright cyan to background
- background_white = 47, // Applies non-bold/bright white to background
- background_extended = 48, // Applies extended color value to the background (see details below)
- background_default = 49, // Applies only the background portion of the defaults (see 0)
- bright_foreground_black = 90, // Applies bold/bright black to foreground
- bright_foreground_red = 91, // Applies bold/bright red to foreground
- bright_foreground_green = 92, // Applies bold/bright green to foreground
- bright_foreground_yellow = 93, // Applies bold/bright yellow to foreground
- bright_foreground_blue = 94, // Applies bold/bright blue to foreground
- bright_foreground_magenta = 95, // Applies bold/bright magenta to foreground
- bright_foreground_cyan = 96, // Applies bold/bright cyan to foreground
- bright_foreground_white = 97, // Applies bold/bright white to foreground
- bright_background_black = 100, // Applies bold/bright black to background
- bright_background_red = 101, // Applies bold/bright red to background
- bright_background_green = 102, // Applies bold/bright green to background
- bright_background_yellow = 103, // Applies bold/bright yellow to background
- bright_background_blue = 104, // Applies bold/bright blue to background
- bright_background_magenta = 105, // Applies bold/bright magenta to background
- bright_background_cyan = 106, // Applies bold/bright cyan to background
- bright_background_white = 107 // Applies bold/bright white to background
- }; };
- auto operator<<( ostream& stream, const Ansi_color::Enum code )
- -> ostream&
- { return stream << ansi_prefix << +code << 'm'; }
- auto main() -> int
- {
- cout << "Colored text:" << endl;
- for( int code = Ansi_color::bright_foreground_red;
- code <= Ansi_color::bright_foreground_white;
- ++code ) {
- cout << Ansi_color::Enum( code ) << "word ";
- }
- cout << Ansi_color::original_colors << endl;
- cout << "And that was that." << endl;
- }
Add Comment
Please, Sign In to add comment