Skip to content

Commit

Permalink
Supports a set of string to display different colors.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 12, 2018
1 parent 8f4e956 commit 48a2031
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 27 deletions.
9 changes: 9 additions & 0 deletions lib/ansi-regex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// https://github.com/chalk/ansi-regex/blob/master/index.js

module.exports = function () {
const pattern = [
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\\u0007)',
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))'
].join('|');
return new RegExp(pattern, 'g');
};
32 changes: 26 additions & 6 deletions lib/color-safe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var colors = {};
module['exports'] = colors;


var defineProps = Object.defineProperties;
var styles_data = require('./styles-name')
var ansiRegex = require('./ansi-regex');
var ansiColors = styles_data.colors;
var ansiStyles = styles_data.styles;

Expand Down Expand Up @@ -39,15 +39,35 @@ function build(_styles_more) {
return builder;
}

function applyStyleCallback(str, _sty) {
var _Colors = new Colors()
for (var i = 0; i < _sty.length; i++) {
_Colors.string = str;
if (_sty[i]) str = _Colors[_sty[i]].valueOf(_sty[i]);
}
return str;
}

function regexReplace(str) {
return str.replace(/(\[)/ig, '\\[').replace(/(\])/ig, '\\]');
}

// 应用 Ansi 样式
function applyStyle(){
var args_len = arguments.length;
var str = args_len !== 0 && String(arguments[0]);
var _sty = this._styles;
var _Colors = new Colors()
for (var i = 0; i < _sty.length; i++) {
_Colors.string = str;
// console.log("_sty[i]::",_sty[i])
if(_sty[i]) str = _Colors[_sty[i]].valueOf();
var ansiArray = str.match(ansiRegex());
// 文本中是否带 ANSI 控制码
if (ansiRegex().test(str) && ansiArray) {
var leftReg = regexReplace(ansiArray[0]);
var rightReg = regexReplace(ansiArray[ansiArray.length - 1]);
var centerReg = new RegExp(leftReg + '(.*)' + rightReg, "g");
var centerStr = str.match(centerReg);
var sidesStr = str.split(centerStr);
str = applyStyleCallback(sidesStr[0], _sty) + centerStr + applyStyleCallback(sidesStr[1], _sty);
} else {
str = applyStyleCallback(str, _sty);
}
return str;
}
Expand Down
50 changes: 32 additions & 18 deletions lib/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,29 @@ for (var i = 0; i < colors.length; i++) {
var name = colors[i];
Object.defineProperty(Colors.prototype, name, {
get: function() {
// Foreground 前景色
this.fgcolor = i;
return this;
}
});
Object.defineProperty(Colors.prototype, name + '_b', {
get: function() {
get: function () {
// Background 背景色
this.bgcolor = i;
return this;
}
});
Object.defineProperty(Colors.prototype, name + '_bt', {
get: function() {
get: function () {
// Bright Foreground 明亮 前景色
this.fgcolor_bt = i;
return this;
}
});
Object.defineProperty(Colors.prototype, name + '_bbt', {
get: function() {
this.bgcolor_bbt = i;
get: function () {
// Bright Background 明亮 背景色
this.bgcolor_bt = i;
return this;
}
});
Expand All @@ -87,14 +91,12 @@ for (var i = 0; i < colors.length; i++) {

exports.color[name + '_bt'] = exports[name + '_bt'] = function(text) {
if (!isSupported) return text;
return '\x1b[' + (90 + i) + 'm' + text + '\x1b[0m';
return '\x1b[' + (90 + i) + 'm' + text + '\x1b[1m';
};
exports.color[name + '_bbt'] = exports[name + '_bbt'] = function(text) {
if (!isSupported) return text;
return '\x1b[' + (100 + i) + 'm' + text + '\x1b[0m';
return '\x1b[' + (100 + i) + 'm' + text + '\x1b[1m';
};


})(i)
}

Expand Down Expand Up @@ -167,32 +169,44 @@ for (var i = 0; i < styles.length; i++) {
})(i);
}

Colors.prototype.valueOf = function(){
var text = this.string;
Colors.prototype.colored = function (text) {
var reset = '\x1b[0m';
var is256 = isSupported;
if (this.fgcolor_x&&this.fgcolor_x!==null&&is256){
// 256 Foreground 256 前景色
if (this.fgcolor_x && this.fgcolor_x !== null && is256) {
text = '\x1b[38;5;' + this.fgcolor_x + 'm' + text + reset;
}
if (this.bgcolor_x&&this.bgcolor_x!==null&&is256){
// 256 Foreground 256 前景色
if (this.bgcolor_x && this.bgcolor_x !== null && is256) {
text = '\x1b[48;5;' + this.bgcolor_x + 'm' + text + reset;
}
// Foreground 前景色
if (this.fgcolor !== null && this.fgcolor < 8) {
text = '\x1b[' + (30 + this.fgcolor) + 'm' + text + reset;
}
if (this.bgcolor !== null && this.bgcolor < 8) {
text = '\x1b[' + (40 + this.bgcolor) + 'm' + text + reset;
}
// Bright Foreground 亮 前景色
if (this.fgcolor_bt !== null && this.fgcolor_bt < 8) {
text = '\x1b[' + (90 + this.fgcolor_bt) + 'm' + text + reset;
}
if (this.bgcolor_bbt !== null && this.bgcolor_bbt < 8) {
text = '\x1b[' + (100 + this.bgcolor_bbt) + 'm' + text + reset;
// Background 背景色
if (this.bgcolor !== null && this.bgcolor < 8) {
text = '\x1b[' + (40 + this.bgcolor) + 'm' + text + reset;
}
if (this.styles&&this.styles.length) {
// Bright Background 亮 背景色
if (this.bgcolor_bt !== null && this.bgcolor_bt < 8) {
text = '\x1b[' + (100 + this.bgcolor_bt) + 'm' + text + reset;
}

if (this.styles && this.styles.length) {
text = '\x1b[' + this.styles.join(';') + 'm' + text + reset;
}
return text;
}

Colors.prototype.valueOf = function(type){
var text = this.string;
text = this.colored(text);
return text;
}

exports.Colors = Colors;
32 changes: 29 additions & 3 deletions lib/styles-name.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
module.exports = {
colors:[
'black', 'red', 'green', 'yellow', 'blue',
'magenta', 'cyan', 'white'
// 字背景颜色范围:40~49
// 40:黑, 41:深红, 42:绿, 43:黄色, 44:蓝色, 45:紫色, 46:深绿, 47:白色,
// 字颜色:30~39
// 30:黑, 31:红, 32:绿, 33:黄, 34:蓝色, 35:紫色, 36:深绿, 37:白色,
//
// echo -e "\x1b[31;1m color red underline \x1b[0m"
// 1m 亮的颜色,默认不给是暗淡的颜色
//
// | ANSI | ANSI | ANSI | | Aixterm | Aixterm
// | Color | FG Code | BG Code | Bright Color | FG Code | BG Code
// +---------+---------+-------- +----------------+---------+--------
// | Black | 30 | 40 | Bright Black | 90 | 100
// | Red | 31 | 41 | Bright Red | 91 | 101
// | Green | 32 | 42 | Bright Green | 92 | 102
// | Yellow | 33 | 43 | Bright Yellow | 93 | 103
// | Blue | 34 | 44 | Bright Blue | 94 | 104
// | Magenta | 35 | 45 | Bright Magenta | 95 | 105
// | Cyan | 36 | 46 | Bright Cyan | 96 | 106
// | White | 37 | 47 | Bright White | 97 | 107
//
colors: [
'black',
'red',
'green',
'yellow',
'blue',
'magenta',
'cyan',
'white'
],
styles:[
'bold', //粗体
Expand Down

0 comments on commit 48a2031

Please sign in to comment.