colorizer.js
3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*!
* Casper is a navigation utility for PhantomJS.
*
* Documentation: http://n1k0.github.com/casperjs/
* Repository: http://github.com/n1k0/casperjs
*
* Copyright (c) 2011-2012 Nicolas Perriault
*
* Part of source code is Copyright Joyent, Inc. and other Node contributors.
*
* 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.
*
*/
var fs = require('fs');
var utils = require('utils');
exports.create = function create() {
return new Colorizer();
};
/**
* This is a port of lime colorizer.
* http://trac.symfony-project.org/browser/tools/lime/trunk/lib/lime.php)
*
* (c) Fabien Potencier, Symfony project, MIT license
*/
var Colorizer = function() {
var options = { bold: 1, underscore: 4, blink: 5, reverse: 7, conceal: 8 };
var foreground = { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37 };
var background = { black: 40, red: 41, green: 42, yellow: 43, blue: 44, magenta: 45, cyan: 46, white: 47 };
var styles = {
'ERROR': { bg: 'red', fg: 'white', bold: true },
'INFO': { fg: 'green', bold: true },
'TRACE': { fg: 'green', bold: true },
'PARAMETER': { fg: 'cyan' },
'COMMENT': { fg: 'yellow' },
'WARNING': { fg: 'red', bold: true },
'GREEN_BAR': { fg: 'white', bg: 'green', bold: true },
'RED_BAR': { fg: 'white', bg: 'red', bold: true },
'INFO_BAR': { bg: 'cyan', fg: 'white', bold: true }
};
/**
* Adds a style to provided text.
*
* @params String text
* @params String styleName
* @return String
*/
this.colorize = function colorize(text, styleName, pad) {
if (fs.isWindows() || !(styleName in styles)) {
return text;
}
return this.format(text, styles[styleName], pad);
};
/**
* Formats a text using a style declaration object.
*
* @param String text
* @param Object style
* @return String
*/
this.format = function format(text, style, pad) {
if (fs.isWindows() || !utils.isObject(style)) {
return text;
}
var codes = [];
if (style.fg && foreground[style.fg]) {
codes.push(foreground[style.fg]);
}
if (style.bg && background[style.bg]) {
codes.push(background[style.bg]);
}
for (var option in options) {
if (style[option] === true) {
codes.push(options[option]);
}
}
// pad
if (typeof pad === "number" && text.length < pad) {
text += new Array(pad - text.length + 1).join(' ');
}
return "\033[" + codes.join(';') + 'm' + text + "\033[0m";
};
};
exports.Colorizer = Colorizer;