Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ansi value of styles

  • Loading branch information...
commit 286806bb687e45b45960d6b93fd0eee4366d37f2 1 parent 0e34645
@mgutz authored
Showing with 63 additions and 47 deletions.
  1. +8 −1 README.md
  2. +55 −46 lib/colors.js
View
9 README.md
@@ -9,7 +9,7 @@ General usage
var color = require("mgutz-colors").color;
console.log(color("red on black", "red:black"));
-Style format is `color+attributes:bgColor+attributes`
+Style format is `color+attributes:backgroundColor+attributes`
"red" // red
"red+b" // red bold
@@ -37,6 +37,13 @@ Turn off colors easily
colors.plain = true;
console.log(bc("this is plain now"));
+Get color code of high intensity red on black background
+
+ // use ansi escape sequences directly for speed (like logging)
+ var redOnBlack = colors.ansi('red+h:black')
+ console.log(redOnBlack + message + colors.reset);
+
+
Output all color combinations in terminal
npm test
View
101 lib/colors.js
@@ -6,41 +6,10 @@
/* Whether to disable colors. */
exports.plain = false;
+var reset = exports.reset = "\033[0m"; // reset colors
-/**
- * Colorizes string. To disable colors, set `colors.plain = true`
- *
- * @param {String} style Style format.
- *
- * format:
- * color+attributes:bgColor+attributes
- *
- * color:
- * black
- * red
- * green
- * yellow
- * blue
- * magenta
- * cyan
- * white
- *
- * attributes:
- * b = bold
- * h = high intensity
- * u = underline
- *
- * @example
- * color("...", "red") -> red
- * color("...", "red+b") -> red bold
- * color("...", "red+u") -> red underline
- * color("...", "red+bh") -> red bold high-intensity
- * color("...", "red:white") -> red on white
- * color("...", "red+b:white+h") -> red bold on white high-intensity
- *
- */
-var color = exports.color = function(s, style) {
- if (exports.plain || !style || style.length === 0) return s;
+function ansiCode(style) {
+ if (exports.plain) return '';
var colors = {
black: 0,
@@ -80,37 +49,77 @@ var color = exports.color = function(s, style) {
bgStyle = background[1];
}
- var colored = '\033[';
+ var code = '\033[';
var base = fgIntensity.normal;
if (fgStyle) {
- if (fgStyle.indexOf('b') >= 0) colored += decoration.bold;
- if (fgStyle.indexOf('u') >= 0) colored += decoration.underline;
+ if (fgStyle.indexOf('b') >= 0) code += decoration.bold;
+ if (fgStyle.indexOf('u') >= 0) code += decoration.underline;
base = fgStyle.indexOf('h') >= 0 ? fgIntensity.high : fgIntensity.normal;
}
- colored += (base + colors[fg]).toString() + ';';
+ code += (base + colors[fg]).toString() + ';';
var base = 0;
if (bg) {
base = (bgStyle && bgStyle === 'h') ? bgIntensity.high : bgIntensity.normal;
- colored += (base + colors[bg]).toString() + ';';
+ code += (base + colors[bg]).toString() + ';';
}
- colored = colored.slice(0, -1); // remove last ';'
- colored += 'm' + s
- colored += "\033[0m"; // reset colors
+ code = code.slice(0, -1) + 'm'; // remove last ';'
- return colored;
+
+ return code;
}
+exports.ansi = ansiCode;
+
+/**
+ * Colorizes string. To disable colors, set `colors.plain = true`
+ *
+ * @param {String} style Style format.
+ *
+ * format:
+ * color+attributes:bgColor+attributes
+ *
+ * color:
+ * black
+ * red
+ * green
+ * yellow
+ * blue
+ * magenta
+ * cyan
+ * white
+ *
+ * attributes:
+ * b = bold
+ * h = high intensity
+ * u = underline
+ *
+ * @example
+ * color("...", "red") -> red
+ * color("...", "red+b") -> red bold
+ * color("...", "red+u") -> red underline
+ * color("...", "red+bh") -> red bold high-intensity
+ * color("...", "red:white") -> red on white
+ * color("...", "red+b:white+h") -> red bold on white high-intensity
+ *
+ */
+var color = exports.color = function(s, style) {
+ if (exports.plain || !style) return s;
+ return ansiCode(style) + s + reset;
+};
/**
- * Define a color function.
+ Define a color function.
*
* @param {String} style The style format.
*/
exports.fn = function(style) {
+ var ansi = ansiCode(style);
return function(s) {
- return color(s, style);
+ if (exports.plain || !s) return s;
+ return ansi + s + reset;
}
-}
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.