mirror of
https://github.com/yamadapc/d-colorize
synced 2024-11-21 15:44:00 +01:00
Initial commit.
This commit is contained in:
commit
304f29787e
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
__test__library__
|
||||
.dub
|
||||
libcolorize.a
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Pedro Tacla Yamada
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
10
README.md
Normal file
10
README.md
Normal file
|
@ -0,0 +1,10 @@
|
|||
d-colorize
|
||||
====================
|
||||
|
||||
__THIS MODULE HASN'T BEEN PROPERLY TESTED OR PUBLISHED YET__
|
||||
__(but it's trivial, so it shouldn't take too much)__
|
||||
__TODO__
|
||||
|
||||
## License
|
||||
Copyright (c) 2014 Pedro Tacla Yamada. Licensed under the MIT license.
|
||||
Please refer to the [LICENSE](LICENSE) file for more info.
|
8
dub.json
Normal file
8
dub.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "colorize",
|
||||
"description": "A port of Ruby's colorize library to D.",
|
||||
"copyright": "Copyright © 2014, Pedro Tacla Yamada",
|
||||
"authors": [
|
||||
"Pedro Tacla Yamada"
|
||||
]
|
||||
}
|
86
source/colorize.d
Normal file
86
source/colorize.d
Normal file
|
@ -0,0 +1,86 @@
|
|||
import std.string : format;
|
||||
|
||||
static enum fg : int
|
||||
{
|
||||
init = 39,
|
||||
|
||||
black = 30,
|
||||
red = 31,
|
||||
green = 32,
|
||||
yellow = 33,
|
||||
blue = 34,
|
||||
magenta = 35,
|
||||
cyan = 36,
|
||||
white = 37,
|
||||
|
||||
light_black = 90,
|
||||
light_red = 91,
|
||||
light_green = 92,
|
||||
light_yellow = 93,
|
||||
light_blue = 94,
|
||||
light_magenta = 95,
|
||||
light_cyan = 96,
|
||||
light_white = 97
|
||||
}
|
||||
|
||||
static enum bg : int
|
||||
{
|
||||
init = 49,
|
||||
|
||||
black = 40,
|
||||
red = 41,
|
||||
green = 42,
|
||||
yellow = 43,
|
||||
blue = 44,
|
||||
magenta = 45,
|
||||
cyan = 46,
|
||||
white = 47,
|
||||
|
||||
light_black = 100,
|
||||
light_red = 101,
|
||||
light_green = 102,
|
||||
light_yellow = 103,
|
||||
light_blue = 104,
|
||||
light_magenta = 105,
|
||||
light_cyan = 106,
|
||||
light_white = 107
|
||||
}
|
||||
|
||||
static enum mode : int
|
||||
{
|
||||
init = 0,
|
||||
bold = 1,
|
||||
underline = 4,
|
||||
blink = 5,
|
||||
swap = 7,
|
||||
hide = 8
|
||||
}
|
||||
|
||||
|
||||
string colorize(string str, fg c, bg b=bg.init, mode m=mode.init)
|
||||
pure
|
||||
{
|
||||
return format("\033[%d;%d;%dm%s\033[0m", m, c, b, str);
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
import std.stdio;
|
||||
string ret;
|
||||
|
||||
ret = "This is yellow".colorize(fg.yellow);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[0;33;49mThis is yellow\033[0m");
|
||||
|
||||
ret = "This is light green".colorize(fg.light_green);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[0;92;49mThis is light green\033[0m");
|
||||
|
||||
ret = "This is light blue with red background".colorize(fg.light_blue, bg.red);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[0;94;41mThis is light blue with red background\033[0m");
|
||||
|
||||
ret = "This is red on blue blinking".colorize(fg.red, bg.blue, mode.blink);
|
||||
writeln(ret);
|
||||
assert(ret == "\033[5;31;44mThis is red on blue blinking\033[0m");
|
||||
}
|
Loading…
Reference in a new issue