├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── benchmarks └── bench.d ├── dub.json └── source └── colorize ├── colors.d ├── cwrite.d ├── package.d └── winterm.d /.gitignore: -------------------------------------------------------------------------------- 1 | __test__library__ 2 | .dub 3 | libcolorize.a 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: d 2 | sudo: false 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Pedro Tacla Yamada 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | d-colorize 2 | ==================== 3 | [![Build Status](https://travis-ci.org/yamadapc/d-colorize.svg?branch=master)](https://travis-ci.org/yamadapc/d-colorize) 4 | [![Gitter chat](https://badges.gitter.im/yamadapc/d-colorize.png)](https://gitter.im/yamadapc/d-colorize) 5 | [![Analytics](https://ga-beacon.appspot.com/UA-54450544-1/d-colorize/README)](https://github.com/igrigorik/ga-beacon) 6 | - - - 7 | 8 | A partial port of Ruby's [colorize](https://github.com/fazibear/colorize) 9 | library to D. 10 | 11 | To put it simply, this is a simple helper for printing colored output to a 12 | terminal. 13 | 14 | ## Installing 15 | This package is registered in the dub registry as 16 | [colorize](http://code.dlang.org/packages/colorize). 17 | 18 | ## Usage 19 | ```d 20 | import colorize : fg, color, cwriteln, cwritefln; 21 | 22 | void main() 23 | { 24 | cwriteln("This is blue".color(fg.blue)); 25 | 26 | auto c = "red"; 27 | cwritefln("This is %s".color(c), c); 28 | } 29 | ``` 30 | - - - 31 | 32 | ## Supporting cross-platform color printing 33 | Colorize exports functions for wrapping strings around ANSI color escape 34 | sequences. Simply printing these strings should work fine for any UNIX system. 35 | However, for "colorized" printing to work on Windows, it's necessary to use one 36 | of the exported helper printing functions, provided by 37 | [p0nce](https://github.com/p0nce), also in this module. 38 | 39 | Colorizing output works by using the `color` API described below and for Windows 40 | compatibility, we provide functions which parse the escape sequences and call 41 | appropriate system-level. On all platforms other than Windows, these functions 42 | should work just as their `std.stdio` counterparts. 43 | 44 | These are: 45 | ### cwrite 46 | ```d 47 | void cwrite(T...)(T args) if (!is(T[0] : File)) 48 | ``` 49 | #### With an overloaded version for File output: 50 | ```d 51 | void cwrite(S...)(File f, S args) 52 | ``` 53 | ### cwritef 54 | ```d 55 | void cwritef(Char, T...)(in Char[] fmt, T args) if (!is(T[0] : File)) 56 | ``` 57 | #### With an overloaded version for File output: 58 | ```d 59 | void cwritef(Char, A...)(File f, in Char[] fmt, A args) 60 | ``` 61 | ### cwriteln 62 | ```d 63 | void cwriteln(T...)(T args) 64 | ``` 65 | ### cwritefln 66 | ```d 67 | void cwritefln(Char, T...)(in Char[] fmt, T args) 68 | ``` 69 | - - - 70 | 71 | ## Setting background, foreground and text modes: 72 | ```d 73 | string color( 74 | const string str, 75 | const fg c, 76 | const bg b=bg.init, 77 | const mode m=mode.init 78 | ) pure; 79 | ``` 80 | 81 | Wraps a string around color escape sequences. 82 | 83 | ### Params 84 | * str = The string to wrap with colors and modes 85 | * c = The foreground color (see the fg enum type) 86 | * b = The background color (see the bg enum type) 87 | * m = The text mode (see the mode enum type) 88 | 89 | ### Example 90 | 91 | ```d 92 | color("This is red over green blinking", fg.blue, bg.green, mode.blink) 93 | ``` 94 | 95 | - - - 96 | 97 | ## Setting background colors: 98 | ```d 99 | string color(const string str, const bg b) pure; // alias to background 100 | ``` 101 | 102 | Wraps a string around a background color escape sequence. 103 | 104 | ### Params 105 | * str = The string to wrap with background color `b` 106 | * b = The background color (see the bg enum type) 107 | 108 | ## Example 109 | ```d 110 | color("This has a blue background", bg.blue); 111 | background("This has a red background", bg.red); 112 | ``` 113 | 114 | - - - 115 | 116 | ## Setting text modes: 117 | ```d 118 | string color(const string str, const mode m) pure; // alias to `style` 119 | ``` 120 | 121 | Wraps a string around a text mode. 122 | 123 | ### Params 124 | * str = The string to wrap with style `m` 125 | * m = The text mode (see the mode enum type) 126 | 127 | ### Example 128 | ```d 129 | color("This text is bold", mode.bold); 130 | style("This text is blinking", mode.blink); 131 | ``` 132 | 133 | ## Coloring with strings: 134 | ```d 135 | string color(const string str, const string name) pure; 136 | ``` 137 | 138 | Wraps a string around the foreground color, background color or text style 139 | represented by the color `name`. Foreground colors are represented by their enum 140 | key (`"blue"` will be `34`, `"red"` `31`, and so on) and backgrounds/modes are 141 | prefixed with either `"bg_"` or `"mode_"` (thus, `"bg_black"` will be `40`, 142 | `"mode_bold"` `1` and so on). 143 | 144 | ### Example 145 | ```d 146 | color("This text is blue", "blue"); 147 | "This is red over blue, blinking" 148 | .color("red") 149 | .color("bg_blue") 150 | .color("mode_blue"); 151 | ``` 152 | 153 | ### Params 154 | 155 | ## Available colors and modes 156 | ### `fg` enum type (Foreground colors) 157 | Foreground text colors are available through the `fg` enum. Currently available 158 | colors are: 159 | - `fg.init` (39) 160 | - `fg.black` (30) 161 | - `fg.red` (31) 162 | - `fg.green` (32) 163 | - `fg.yellow` (33) 164 | - `fg.blue` (34) 165 | - `fg.magenta` (35) 166 | - `fg.cyan` (36) 167 | - `fg.white` (37) 168 | - `fg.light_black` (90) 169 | - `fg.light_red` (91) 170 | - `fg.light_green` (92) 171 | - `fg.light_yellow` (93) 172 | - `fg.light_blue` (94) 173 | - `fg.light_magenta` (95) 174 | - `fg.light_cyan` (96) 175 | - `fg.light_white` (97) 176 | 177 | ### `bg` enum type (Background colors) 178 | Background colors are available with the same names through the `bg` enum. This 179 | is because background colors come with an offset of 10 to their foreground 180 | counterparts and we wanted to avoid calculating the offset at runtime. 181 | 182 | ### `mode` enum type (Text modes) 183 | Text modes are available through the `mode` enum. Currently available text modes 184 | are: 185 | - `mode.init` (0) 186 | - `mode.bold` (1) 187 | - `mode.underline` (4) 188 | - `mode.blink` (5) 189 | - `mode.swap` (7) 190 | - `mode.hide` (8) 191 | 192 | ## License 193 | Copyright (c) 2014 Pedro Tacla Yamada. Licensed under the MIT license. 194 | Please refer to the [LICENSE](LICENSE) file for more info. 195 | 196 | ## Donations 197 | Would you like to buy me a beer? Send bitcoin to 3JjxJydvoJjTrhLL86LGMc8cNB16pTAF3y 198 | -------------------------------------------------------------------------------- /benchmarks/bench.d: -------------------------------------------------------------------------------- 1 | import std.stdio; 2 | import std.string : format; 3 | import std.datetime : comparingBenchmark; 4 | 5 | void main() 6 | { 7 | auto bs = comparingBenchmark!(testColorizeString, testColorizeEnum, 8 | 10_000); 9 | 10 | writeln("string : ", bs.baseTime); 11 | writeln("enum : ", bs.targetTime); 12 | writeln("point : ", bs.point); 13 | } 14 | 15 | static enum color : int 16 | { 17 | black = 30, 18 | red = 31, 19 | green = 32, 20 | brown = 33, 21 | blue = 34, 22 | magenta = 35, 23 | cyan = 36, 24 | gray = 37, 25 | bg_black = 40, 26 | bg_red = 41, 27 | bg_green = 42, 28 | bg_brown = 43, 29 | bg_blue = 44, 30 | bg_magenta = 45, 31 | bg_cyan = 46, 32 | bg_gray = 47, 33 | bold = 0 34 | } 35 | 36 | void testColorizeString() 37 | { 38 | auto str = colorize("this is blue", "blue"); 39 | } 40 | 41 | void testColorizeEnum() 42 | { 43 | auto str = colorize("this is blue", color.blue); 44 | } 45 | 46 | string colorize(const string str, const string color) pure 47 | { 48 | int color_num = 0; 49 | switch (color) { 50 | case "black": color_num = 30; break; 51 | case "red": color_num = 31; break; 52 | case "green": color_num = 32; break; 53 | case "brown": color_num = 33; break; 54 | case "blue": color_num = 34; break; 55 | case "magenta": color_num = 35; break; 56 | case "cyan": color_num = 36; break; 57 | case "gray": color_num = 37; break; 58 | 59 | case "bg_black": color_num = 40; break; 60 | case "bg_red": color_num = 41; break; 61 | case "bg_green": color_num = 42; break; 62 | case "bg_brown": color_num = 43; break; 63 | case "bg_blue": color_num = 44; break; 64 | case "bg_magenta": color_num = 45; break; 65 | case "bg_cyan": color_num = 46; break; 66 | case "bg_gray": color_num = 47; break; 67 | 68 | case "bold": color_num = 0; break; 69 | 70 | default: 71 | throw new Exception("unknown color \"" ~ color ~ "\""); 72 | } 73 | 74 | return format("\033[%dm%s\033[0m", color_num, str); 75 | } 76 | 77 | string colorize(const string str, color c) pure 78 | { 79 | return format("\033[%dm%s\033[0m", c, str); 80 | } 81 | -------------------------------------------------------------------------------- /dub.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "colorize", 3 | "description": "A port of Ruby's colorize library to D.", 4 | "license": "MIT", 5 | "copyright": "Copyright © 2014, Pedro Tacla Yamada", 6 | "importPaths": ["source"], 7 | "sourcePaths": ["source"], 8 | "authors": [ 9 | "Pedro Tacla Yamada", 10 | "ponce" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /source/colorize/colors.d: -------------------------------------------------------------------------------- 1 | /** 2 | * Authors: Pedro Tacla Yamada 3 | * Date: June 9, 2014 4 | * License: Licensed under the MIT license. See LICENSE for more information 5 | * Version: 1.0.2 6 | */ 7 | module colorize.colors; 8 | 9 | import std.string : format; 10 | 11 | private template color_type(int offset) 12 | { 13 | static enum type : int 14 | { 15 | init = 39 + offset, 16 | 17 | black = 30 + offset, 18 | red = 31 + offset, 19 | green = 32 + offset, 20 | yellow = 33 + offset, 21 | blue = 34 + offset, 22 | magenta = 35 + offset, 23 | cyan = 36 + offset, 24 | white = 37 + offset, 25 | 26 | light_black = 90 + offset, 27 | light_red = 91 + offset, 28 | light_green = 92 + offset, 29 | light_yellow = 93 + offset, 30 | light_blue = 94 + offset, 31 | light_magenta = 95 + offset, 32 | light_cyan = 96 + offset, 33 | light_white = 97 + offset 34 | } 35 | } 36 | 37 | alias color_type!0 .type fg; 38 | alias color_type!10 .type bg; 39 | 40 | // Text modes 41 | static enum mode : int 42 | { 43 | init = 0, 44 | bold = 1, 45 | underline = 4, 46 | blink = 5, 47 | swap = 7, 48 | hide = 8 49 | } 50 | 51 | /** 52 | * Wraps a string around color escape sequences. 53 | * 54 | * Params: 55 | * str = The string to wrap with colors and modes 56 | * c = The foreground color (see the fg enum type) 57 | * b = The background color (see the bg enum type) 58 | * m = The text mode (see the mode enum type) 59 | * Example: 60 | * --- 61 | * writeln("This is blue".color(fg.blue)); 62 | * writeln( 63 | * color("This is red over green blinking", fg.blue, bg.green, mode.blink) 64 | * ); 65 | * --- 66 | */ 67 | 68 | string color( 69 | const string str, 70 | const fg c=fg.init, 71 | const bg b=bg.init, 72 | const mode m=mode.init 73 | ) pure 74 | { 75 | return format("\033[%d;%d;%dm%s\033[0m", m, c, b, str); 76 | } 77 | 78 | unittest 79 | { 80 | import colorize.cwrite; 81 | string ret; 82 | 83 | ret = "This is yellow".color(fg.yellow); 84 | cwriteln(ret); 85 | assert(ret == "\033[33mThis is yellow\033[0m"); 86 | 87 | ret = "This is light green".color(fg.light_green); 88 | cwriteln(ret); 89 | assert(ret == "\033[92mThis is light green\033[0m"); 90 | 91 | ret = "This is light blue with red background".color(fg.light_blue, bg.red); 92 | cwriteln(ret); 93 | assert(ret == "\033[0;94;41mThis is light blue with red background\033[0m"); 94 | 95 | ret = "This is red on blue blinking".color(fg.red, bg.blue, mode.blink); 96 | cwriteln(ret); 97 | assert(ret == "\033[5;31;44mThis is red on blue blinking\033[0m"); 98 | 99 | ret = color("This is magenta", "magenta"); 100 | cwriteln(ret); 101 | assert(ret == "\033[35mThis is magenta\033[0m"); 102 | } 103 | 104 | string colorHelper(const string str, const string name) pure 105 | { 106 | int code; 107 | 108 | switch(name) 109 | { 110 | case "init": code = 39; break; 111 | 112 | case "black" : code = 30; break; 113 | case "red" : code = 31; break; 114 | case "green" : code = 32; break; 115 | case "yellow" : code = 33; break; 116 | case "blue" : code = 34; break; 117 | case "magenta": code = 35; break; 118 | case "cyan" : code = 36; break; 119 | case "white" : code = 37; break; 120 | 121 | case "light_black" : code = 90; break; 122 | case "light_red" : code = 91; break; 123 | case "light_green" : code = 92; break; 124 | case "light_yellow" : code = 93; break; 125 | case "light_blue" : code = 94; break; 126 | case "light_magenta": code = 95; break; 127 | case "light_cyan" : code = 96; break; 128 | case "light_white" : code = 97; break; 129 | 130 | case "bg_init": code = 49; break; 131 | 132 | case "bg_black" : code = 40; break; 133 | case "bg_red" : code = 41; break; 134 | case "bg_green" : code = 42; break; 135 | case "bg_yellow" : code = 43; break; 136 | case "bg_blue" : code = 44; break; 137 | case "bg_magenta": code = 45; break; 138 | case "bg_cyan" : code = 46; break; 139 | case "bg_white" : code = 47; break; 140 | 141 | case "bg_light_black" : code = 100; break; 142 | case "bg_light_red" : code = 101; break; 143 | case "bg_light_green" : code = 102; break; 144 | case "bg_light_yellow" : code = 103; break; 145 | case "bg_light_blue" : code = 104; break; 146 | case "bg_light_magenta": code = 105; break; 147 | case "bg_light_cyan" : code = 106; break; 148 | case "bg_light_white" : code = 107; break; 149 | 150 | case "mode_init": code = 0; break; 151 | case "mode_bold" : code = 1; break; 152 | case "mode_underline": code = 4; break; 153 | case "mode_blink" : code = 5; break; 154 | case "mode_swap" : code = 7; break; 155 | case "mode_hide" : code = 8; break; 156 | 157 | default: 158 | throw new Exception( 159 | "Unknown fg color, bg color or mode \"" ~ name ~ "\"" 160 | ); 161 | } 162 | 163 | return format("\033[%dm%s\033[0m", code, str); 164 | } 165 | 166 | string colorHelper(T)(const string str, const T t=T.init) pure 167 | if(is(T : fg) || is(T : bg) || is(T : mode)) 168 | { 169 | return format("\033[%dm%s\033[0m", t, str); 170 | } 171 | 172 | // Custom colors 173 | string customForeground(const string str, float r, float g, float b) 174 | { 175 | uint red = cast(uint)(r * 5 + 0.5); 176 | uint green = cast(uint)(g * 5 + 0.5); 177 | uint blue = cast(uint)(b * 5 + 0.5); 178 | 179 | return format("\033[38;5;%dm%s\033[0m", 180 | 16 + (red * 36 + green * 6 + blue), 181 | str); 182 | } 183 | 184 | alias colorHelper!bg background; 185 | alias colorHelper!fg foreground; 186 | alias colorHelper!mode style; 187 | 188 | alias background color; 189 | alias foreground color; 190 | alias style color; 191 | alias colorHelper color; 192 | 193 | unittest 194 | { 195 | import colorize.cwrite; 196 | string ret; 197 | 198 | ret = "This is red on blue blinking" 199 | .foreground(fg.red) 200 | .background(bg.blue) 201 | .style(mode.blink); 202 | 203 | cwriteln(ret); 204 | assert(ret == "\033[5m\033[44m\033[31mThis is red on blue blinking\033[0m\033[0m\033[0m"); 205 | 206 | ret = "This is a custom color"; 207 | cwriteln(ret.customForeground(1, 0.1, 0.3)); 208 | } 209 | -------------------------------------------------------------------------------- /source/colorize/cwrite.d: -------------------------------------------------------------------------------- 1 | /** 2 | * Authors: ponce 3 | * Date: July 28, 2014 4 | * License: Licensed under the MIT license. See LICENSE for more information 5 | * Version: 1.0.2 6 | */ 7 | module colorize.cwrite; 8 | 9 | import std.stdio : File, stdout; 10 | 11 | import colorize.winterm; 12 | 13 | /// Coloured write. 14 | void cwrite(T...)(T args) if (!is(T[0] : File)) 15 | { 16 | stdout.cwrite(args); 17 | } 18 | 19 | /// Coloured writef. 20 | void cwritef(Char, T...)(in Char[] fmt, T args) if (!is(T[0] : File)) 21 | { 22 | stdout.cwritef(fmt, args); 23 | } 24 | 25 | /// Coloured writefln. 26 | void cwritefln(Char, T...)(in Char[] fmt, T args) 27 | { 28 | stdout.cwritef(fmt ~ "\n", args); 29 | } 30 | 31 | /// Coloured writeln. 32 | void cwriteln(T...)(T args) 33 | { 34 | // Most general instance 35 | stdout.cwrite(args, '\n'); 36 | } 37 | 38 | /// Coloured writef to a File. 39 | void cwritef(Char, A...)(File f, in Char[] fmt, A args) 40 | { 41 | import std.string : format; 42 | auto s = format(fmt, args); 43 | f.cwrite(s); 44 | } 45 | 46 | /// Coloured writef to a File. 47 | void cwrite(S...)(File f, S args) 48 | { 49 | import std.conv : to; 50 | 51 | string s = ""; 52 | foreach(arg; args) 53 | s ~= to!string(arg); 54 | 55 | version(Windows) 56 | { 57 | WinTermEmulation winterm; 58 | winterm.initialize(); 59 | foreach(dchar c ; s) 60 | { 61 | auto charAction = winterm.feed(c); 62 | final switch(charAction) with (WinTermEmulation.CharAction) 63 | { 64 | case drop: break; 65 | case write: f.write(c); break; 66 | case flush: f.flush(); break; 67 | } 68 | } 69 | } 70 | else 71 | { 72 | f.write(s); 73 | } 74 | } 75 | 76 | -------------------------------------------------------------------------------- /source/colorize/package.d: -------------------------------------------------------------------------------- 1 | /** 2 | * Authors: ponce 3 | * Date: July 28, 2014 4 | * License: Licensed under the MIT license. See LICENSE for more information 5 | * Version: 1.0.2 6 | */ 7 | module colorize; 8 | 9 | public import colorize.colors; 10 | public import colorize.cwrite; 11 | -------------------------------------------------------------------------------- /source/colorize/winterm.d: -------------------------------------------------------------------------------- 1 | /** 2 | * Authors: ponce 3 | * Date: July 28, 2014 4 | * License: Licensed under the MIT license. See LICENSE for more information 5 | * Version: 1.0.2 6 | */ 7 | module colorize.winterm; 8 | 9 | version(Windows) 10 | { 11 | import core.sys.windows.windows; 12 | 13 | // Patch for DMD 2.065 compatibility 14 | static if( __VERSION__ < 2066 ) private enum nogc = 1; 15 | 16 | // This is a state machine to enable terminal colors on Windows. 17 | // Parses and interpret ANSI/VT100 Terminal Control Escape Sequences. 18 | // Only supports colour sequences, will output char incorrectly on invalid input. 19 | struct WinTermEmulation 20 | { 21 | public: 22 | @nogc void initialize() nothrow 23 | { 24 | // saves console attributes 25 | _console = GetStdHandle(STD_OUTPUT_HANDLE); 26 | _savedInitialColor = (0 != GetConsoleScreenBufferInfo(_console, &consoleInfo)); 27 | _state = State.initial; 28 | } 29 | 30 | @nogc ~this() nothrow 31 | { 32 | // Restore initial text attributes on release 33 | if (_savedInitialColor) 34 | { 35 | SetConsoleTextAttribute(_console, consoleInfo.wAttributes); 36 | _savedInitialColor = false; 37 | } 38 | } 39 | 40 | enum CharAction 41 | { 42 | write, 43 | drop, 44 | flush 45 | } 46 | 47 | // Eat one character and update color state accordingly. 48 | // Returns what to do with the fed character. 49 | @nogc CharAction feed(dchar d) nothrow 50 | { 51 | final switch(_state) with (State) 52 | { 53 | case initial: 54 | if (d == '\x1B') 55 | { 56 | _state = escaped; 57 | return CharAction.flush; 58 | } 59 | break; 60 | 61 | case escaped: 62 | if (d == '[') 63 | { 64 | _state = readingAttribute; 65 | _parsedAttr = 0; 66 | return CharAction.drop; 67 | } 68 | break; 69 | 70 | 71 | case readingAttribute: 72 | if (d >= '0' && d <= '9') 73 | { 74 | _parsedAttr = _parsedAttr * 10 + (d - '0'); 75 | return CharAction.drop; 76 | } 77 | else if (d == ';') 78 | { 79 | executeAttribute(_parsedAttr); 80 | _parsedAttr = 0; 81 | return CharAction.drop; 82 | } 83 | else if (d == 'm') 84 | { 85 | executeAttribute(_parsedAttr); 86 | _state = State.initial; 87 | return CharAction.drop; 88 | } 89 | break; 90 | } 91 | return CharAction.write; 92 | } 93 | 94 | private: 95 | HANDLE _console; 96 | bool _savedInitialColor; 97 | CONSOLE_SCREEN_BUFFER_INFO consoleInfo; 98 | State _state; 99 | WORD _currentAttr; 100 | int _parsedAttr; 101 | 102 | enum State 103 | { 104 | initial, 105 | escaped, 106 | readingAttribute 107 | } 108 | 109 | @nogc void setForegroundColor(WORD fgFlags) nothrow 110 | { 111 | _currentAttr = _currentAttr & ~(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); 112 | _currentAttr = _currentAttr | fgFlags; 113 | SetConsoleTextAttribute(_console, _currentAttr); 114 | } 115 | 116 | @nogc void setBackgroundColor(WORD bgFlags) nothrow 117 | { 118 | _currentAttr = _currentAttr & ~(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY); 119 | _currentAttr = _currentAttr | bgFlags; 120 | SetConsoleTextAttribute(_console, _currentAttr); 121 | } 122 | 123 | @nogc void executeAttribute(int attr) nothrow 124 | { 125 | switch (attr) 126 | { 127 | case 0: 128 | // reset all attributes 129 | SetConsoleTextAttribute(_console, consoleInfo.wAttributes); 130 | break; 131 | 132 | default: 133 | if ( (30 <= attr && attr <= 37) || (90 <= attr && attr <= 97) ) 134 | { 135 | WORD color = 0; 136 | if (90 <= attr && attr <= 97) 137 | { 138 | color = FOREGROUND_INTENSITY; 139 | attr -= 60; 140 | } 141 | attr -= 30; 142 | color |= (attr & 1 ? FOREGROUND_RED : 0) | (attr & 2 ? FOREGROUND_GREEN : 0) | (attr & 4 ? FOREGROUND_BLUE : 0); 143 | setForegroundColor(color); 144 | } 145 | 146 | if ( (40 <= attr && attr <= 47) || (100 <= attr && attr <= 107) ) 147 | { 148 | WORD color = 0; 149 | if (100 <= attr && attr <= 107) 150 | { 151 | color = BACKGROUND_INTENSITY; 152 | attr -= 60; 153 | } 154 | attr -= 40; 155 | color |= (attr & 1 ? BACKGROUND_RED : 0) | (attr & 2 ? BACKGROUND_GREEN : 0) | (attr & 4 ? BACKGROUND_BLUE : 0); 156 | setBackgroundColor(color); 157 | } 158 | } 159 | } 160 | } 161 | } 162 | --------------------------------------------------------------------------------