2014-07-09 18:49:00 +02:00
|
|
|
d-colorize
|
|
|
|
====================
|
2014-07-10 05:05:22 +02:00
|
|
|
[![Build Status](https://travis-ci.org/yamadapc/d-colorize.svg?branch=master)](https://travis-ci.org/yamadapc/d-colorize)
|
2014-07-31 23:44:04 +02:00
|
|
|
[![Gitter chat](https://badges.gitter.im/yamadapc/d-colorize.png)](https://gitter.im/yamadapc/d-colorize)
|
2014-09-03 19:45:42 +02:00
|
|
|
[![Analytics](https://ga-beacon.appspot.com/UA-54450544-1/d-colorize/README)](https://github.com/igrigorik/ga-beacon)
|
2014-07-10 05:05:22 +02:00
|
|
|
- - -
|
2014-07-09 18:49:00 +02:00
|
|
|
|
2014-07-10 04:31:45 +02:00
|
|
|
A partial port of Ruby's [colorize](https://github.com/fazibear/colorize)
|
|
|
|
library to D.
|
|
|
|
|
|
|
|
To put it simply, this is a simple helper for printing colored output to a
|
|
|
|
terminal.
|
|
|
|
|
|
|
|
## Installing
|
2014-07-10 04:38:24 +02:00
|
|
|
This package is registered in the dub registry as
|
|
|
|
[colorize](http://code.dlang.org/packages/colorize).
|
2014-07-10 04:24:24 +02:00
|
|
|
|
2014-07-10 04:28:30 +02:00
|
|
|
## Usage
|
2014-07-10 04:24:24 +02:00
|
|
|
```d
|
2014-07-31 23:32:56 +02:00
|
|
|
import colorize : fg, color, cwriteln, cwritefln;
|
2014-07-10 04:24:24 +02:00
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2014-07-31 23:32:56 +02:00
|
|
|
cwriteln("This is blue".color(fg.blue));
|
|
|
|
|
|
|
|
auto c = "red";
|
|
|
|
cwritefln("This is %s".color(c), c);
|
2014-07-10 04:24:24 +02:00
|
|
|
}
|
|
|
|
```
|
2014-08-01 09:29:24 +02:00
|
|
|
- - -
|
|
|
|
|
|
|
|
## Supporting cross-platform color printing
|
|
|
|
Colorize exports functions for wrapping strings around ANSI color escape
|
|
|
|
sequences. Simply printing these strings should work fine for any UNIX system.
|
|
|
|
However, for "colorized" printing to work on Windows, it's necessary to use one
|
|
|
|
of the exported helper printing functions, provided by
|
|
|
|
[p0nce](https://github.com/p0nce), also in this module.
|
2014-07-10 04:24:24 +02:00
|
|
|
|
2014-08-01 09:29:24 +02:00
|
|
|
Colorizing output works by using the `color` API described below and for Windows
|
|
|
|
compatibility, we provide functions which parse the escape sequences and call
|
|
|
|
appropriate system-level. On all platforms other than Windows, these functions
|
|
|
|
should work just as their `std.stdio` counterparts.
|
|
|
|
|
|
|
|
These are:
|
|
|
|
### cwrite
|
|
|
|
```d
|
|
|
|
void cwrite(T...)(T args) if (!is(T[0] : File))
|
|
|
|
```
|
|
|
|
#### With an overloaded version for File output:
|
|
|
|
```d
|
|
|
|
void cwrite(S...)(File f, S args)
|
|
|
|
```
|
|
|
|
### cwritef
|
|
|
|
```d
|
|
|
|
void cwritef(Char, T...)(in Char[] fmt, T args) if (!is(T[0] : File))
|
|
|
|
```
|
|
|
|
#### With an overloaded version for File output:
|
|
|
|
```d
|
|
|
|
void cwritef(Char, A...)(File f, in Char[] fmt, A args)
|
|
|
|
```
|
|
|
|
### cwriteln
|
|
|
|
```d
|
|
|
|
void cwriteln(T...)(T args)
|
|
|
|
```
|
|
|
|
### cwritefln
|
|
|
|
```d
|
|
|
|
void cwritefln(Char, T...)(in Char[] fmt, T args)
|
|
|
|
```
|
2014-07-13 21:27:18 +02:00
|
|
|
- - -
|
|
|
|
|
|
|
|
## Setting background, foreground and text modes:
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
string color(
|
2014-07-13 21:27:18 +02:00
|
|
|
const string str,
|
|
|
|
const fg c,
|
|
|
|
const bg b=bg.init,
|
|
|
|
const mode m=mode.init
|
|
|
|
) pure;
|
|
|
|
```
|
2014-07-10 04:24:24 +02:00
|
|
|
|
|
|
|
Wraps a string around color escape sequences.
|
|
|
|
|
2014-07-10 04:28:30 +02:00
|
|
|
### Params
|
2014-07-10 04:24:24 +02:00
|
|
|
* str = The string to wrap with colors and modes
|
|
|
|
* c = The foreground color (see the fg enum type)
|
|
|
|
* b = The background color (see the bg enum type)
|
|
|
|
* m = The text mode (see the mode enum type)
|
|
|
|
|
2014-07-13 21:27:18 +02:00
|
|
|
### Example
|
2014-07-10 04:24:24 +02:00
|
|
|
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
color("This is red over green blinking", fg.blue, bg.green, mode.blink)
|
2014-07-10 04:24:24 +02:00
|
|
|
```
|
|
|
|
|
2014-07-13 21:27:18 +02:00
|
|
|
- - -
|
|
|
|
|
|
|
|
## Setting background colors:
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
string color(const string str, const bg b) pure; // alias to background
|
2014-07-13 21:27:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Wraps a string around a background color escape sequence.
|
|
|
|
|
|
|
|
### Params
|
|
|
|
* str = The string to wrap with background color `b`
|
|
|
|
* b = The background color (see the bg enum type)
|
|
|
|
|
|
|
|
## Example
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
color("This has a blue background", bg.blue);
|
2014-07-13 21:27:18 +02:00
|
|
|
background("This has a red background", bg.red);
|
|
|
|
```
|
|
|
|
|
|
|
|
- - -
|
|
|
|
|
|
|
|
## Setting text modes:
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
string color(const string str, const mode m) pure; // alias to `style`
|
2014-07-13 21:27:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Wraps a string around a text mode.
|
|
|
|
|
|
|
|
### Params
|
|
|
|
* str = The string to wrap with style `m`
|
|
|
|
* m = The text mode (see the mode enum type)
|
|
|
|
|
|
|
|
### Example
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
color("This text is bold", mode.bold);
|
2014-07-13 21:27:18 +02:00
|
|
|
style("This text is blinking", mode.blink);
|
|
|
|
```
|
|
|
|
|
|
|
|
## Coloring with strings:
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
string color(const string str, const string name) pure;
|
2014-07-13 21:27:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Wraps a string around the foreground color, background color or text style
|
|
|
|
represented by the color `name`. Foreground colors are represented by their enum
|
|
|
|
key (`"blue"` will be `34`, `"red"` `31`, and so on) and backgrounds/modes are
|
|
|
|
prefixed with either `"bg_"` or `"mode_"` (thus, `"bg_black"` will be `40`,
|
|
|
|
`"mode_bold"` `1` and so on).
|
|
|
|
|
|
|
|
### Example
|
|
|
|
```d
|
2014-07-31 23:28:18 +02:00
|
|
|
color("This text is blue", "blue");
|
2014-07-13 21:27:18 +02:00
|
|
|
"This is red over blue, blinking"
|
2014-07-31 23:28:18 +02:00
|
|
|
.color("red")
|
|
|
|
.color("bg_blue")
|
|
|
|
.color("mode_blue");
|
2014-07-13 21:27:18 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### Params
|
|
|
|
|
2014-07-10 04:28:30 +02:00
|
|
|
## Available colors and modes
|
|
|
|
### `fg` enum type (Foreground colors)
|
2014-07-10 04:24:24 +02:00
|
|
|
Foreground text colors are available through the `fg` enum. Currently available
|
|
|
|
colors are:
|
|
|
|
- `fg.init` (39)
|
|
|
|
- `fg.black` (30)
|
|
|
|
- `fg.red` (31)
|
|
|
|
- `fg.green` (32)
|
|
|
|
- `fg.yellow` (33)
|
|
|
|
- `fg.blue` (34)
|
|
|
|
- `fg.magenta` (35)
|
|
|
|
- `fg.cyan` (36)
|
|
|
|
- `fg.white` (37)
|
2014-07-10 08:03:48 +02:00
|
|
|
- `fg.light_black` (90)
|
|
|
|
- `fg.light_red` (91)
|
|
|
|
- `fg.light_green` (92)
|
|
|
|
- `fg.light_yellow` (93)
|
|
|
|
- `fg.light_blue` (94)
|
|
|
|
- `fg.light_magenta` (95)
|
|
|
|
- `fg.light_cyan` (96)
|
|
|
|
- `fg.light_white` (97)
|
2014-07-10 04:24:24 +02:00
|
|
|
|
2014-07-10 04:28:30 +02:00
|
|
|
### `bg` enum type (Background colors)
|
2014-07-10 04:24:24 +02:00
|
|
|
Background colors are available with the same names through the `bg` enum. This
|
|
|
|
is because background colors come with an offset of 10 to their foreground
|
|
|
|
counterparts and we wanted to avoid calculating the offset at runtime.
|
|
|
|
|
2014-07-10 04:28:30 +02:00
|
|
|
### `mode` enum type (Text modes)
|
2014-07-10 04:24:24 +02:00
|
|
|
Text modes are available through the `mode` enum. Currently available text modes
|
|
|
|
are:
|
|
|
|
- `mode.init` (0)
|
|
|
|
- `mode.bold` (1)
|
|
|
|
- `mode.underline` (4)
|
|
|
|
- `mode.blink` (5)
|
|
|
|
- `mode.swap` (7)
|
|
|
|
- `mode.hide` (8)
|
2014-07-09 18:49:00 +02:00
|
|
|
|
|
|
|
## License
|
|
|
|
Copyright (c) 2014 Pedro Tacla Yamada. Licensed under the MIT license.
|
|
|
|
Please refer to the [LICENSE](LICENSE) file for more info.
|
2016-02-02 18:49:36 +01:00
|
|
|
|
|
|
|
## Donations
|
|
|
|
Would you like to buy me a beer? Send bitcoin to 3JjxJydvoJjTrhLL86LGMc8cNB16pTAF3y
|