├── LICENSE ├── README.md ├── lib └── ansi-color.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 James Smith 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 2. Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | 3. The name of the author may not be used to endorse or promote products 13 | derived from this software without specific prior written permission. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ANSI Color Code Output Support for CommonJS + Node.js 2 | =========================================== 3 | 4 | This CommonJS module provides basic ANSI color code support, to allow you to 5 | format your console output with foreground and background colors as well as 6 | providing bold and underline support. 7 | 8 | This module does not modify any built-in object prototypes, and so is safe 9 | to use with other modules. 10 | 11 | Tested on node.js. 12 | 13 | Basic usage: 14 | ----------- 15 | // Load the module 16 | var color = require("ansi-color").set; 17 | 18 | // Print the word "Error" to stdout in red 19 | console.log(color("Error", "red")); 20 | 21 | // Print the word "Error" in red and underlined 22 | console.log(color("Error", "red+underline")); 23 | 24 | // Print the word "Success" in bold green, followed by a message 25 | console.log(color("Success", "green+bold"), "Something was successful!"); 26 | 27 | Supported Colors/Formats: 28 | ------------------------- 29 | Note: Any of the below formatting strings can be combined together by joining 30 | together desired formats with a + symbol. Eg: bold+cyan+white_bg 31 | 32 | - Bold Text: bold 33 | - Underlined Text: underline 34 | - Blinking Text: blink 35 | - Black Text: black 36 | - Red Text: red 37 | - Green Text: green 38 | - Yellow Text: yellow 39 | - Blue Text: blue 40 | - Magenta Text: magenta 41 | - Cyan Text: cyan 42 | - White Text: white 43 | - Black Background: black_bg 44 | - Red Background: red_bg 45 | - Green Background: green_bg 46 | - Yellow Background: yellow_bg 47 | - Blue Background: blue_bg 48 | - Magenta Background: magenta_bg 49 | - Cyan Background: cyan_bg 50 | - White Background: white_bg 51 | -------------------------------------------------------------------------------- /lib/ansi-color.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | // ANSI color code outputs for strings 3 | 4 | var ANSI_CODES = { 5 | "off": 0, 6 | "bold": 1, 7 | "italic": 3, 8 | "underline": 4, 9 | "blink": 5, 10 | "inverse": 7, 11 | "hidden": 8, 12 | "black": 30, 13 | "red": 31, 14 | "green": 32, 15 | "yellow": 33, 16 | "blue": 34, 17 | "magenta": 35, 18 | "cyan": 36, 19 | "white": 37, 20 | "black_bg": 40, 21 | "red_bg": 41, 22 | "green_bg": 42, 23 | "yellow_bg": 43, 24 | "blue_bg": 44, 25 | "magenta_bg": 45, 26 | "cyan_bg": 46, 27 | "white_bg": 47 28 | }; 29 | 30 | function setColor(str,color) { 31 | if(!color) return str; 32 | var color_attrs = color.split("+"); 33 | var ansi_str = ""; 34 | for(var i=0, attr; attr = color_attrs[i]; i++) { 35 | ansi_str += "\033[" + ANSI_CODES[attr] + "m"; 36 | } 37 | ansi_str += str + "\033[" + ANSI_CODES["off"] + "m"; 38 | return ansi_str; 39 | } 40 | 41 | function logMessage(message,color) { 42 | console.log(setColor(message,color)); 43 | } 44 | 45 | function replace(full_text, search_regex, color) { 46 | try { 47 | var regex = new RegExp('(' + search_regex + ')', 'ig'); 48 | var new_text = full_text.replace(regex, setColor('$1', color)); 49 | return new_text; 50 | } catch (e) { 51 | return full_text; 52 | } 53 | } 54 | 55 | if (typeof exports !== "undefined") { 56 | exports.log = logMessage; 57 | exports.set = setColor; 58 | exports.replace = replace; 59 | } else if (typeof define !== "undefined") { 60 | define([], function() { return { set: setColor, log: logMessage }; }); 61 | } 62 | }()); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ansi-color", 3 | "version": "0.2.1", 4 | "description": "This module provides basic ANSI color code support, to allow you to format your console output with foreground and background colors as well as providing bold, italic and underline support.", 5 | "author": "James Smith ", 6 | "directories": { 7 | "lib": "./lib" 8 | }, 9 | "main": "./lib/ansi-color.js", 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/loopj/commonjs-ansi-color.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/loopj/commonjs-ansi-color/issues" 16 | }, 17 | "keywords": [ 18 | "ansi", 19 | "color", 20 | "console" 21 | ], 22 | "license": "BSD License", 23 | "contributors": [{ 24 | "name": "vBm", 25 | "email": "the.vbm@gmail.com" 26 | }] 27 | } --------------------------------------------------------------------------------