Permalink
Browse files

First commit, version 0.5.0

  • Loading branch information...
0 parents commit 6cca0d98ba10dbad0d92123e41810fcb085ae61b Trevor Burnham committed Dec 12, 2010
Showing with 279 additions and 0 deletions.
  1. +52 −0 README.mdown
  2. +7 −0 demo.coffee
  3. +122 −0 lib/sty.js
  4. +22 −0 package.json
  5. +76 −0 src/sty.coffee
@@ -0,0 +1,52 @@
+# Sty
+
+## Colorization and more for Node.js console output
+
+### Quick start
+
+Using [CoffeeScript](http://coffeescript.org):
+
+ sty = require 'sty'
+ console.log "#{sty.red 'WARNING:'} Color is #{sty.green 'awesome!'}"
+
+Nesting works as you'd expect:
+
+ console.log "#{sty.bgblack sty.bold "#{sty.red 'U'}S#{sty.cyan 'A'}"}"
+
+You can also use some pseudo-HTML, through `sty.parse`:
+
+ console.log sty.parse 'You are being <b>attacked</b> by the <b><yellow>great dragon</yellow>!</b>'
+
+Forget valid XML; you can move tags around freely:
+
+ console.log sty.parse '<b>W<u>h</b>o</u>a'
+
+Tags that aren't supported by sty are ignored (that is, `sty.parse '<foo>'` is just `'<foo>'`).
+
+### Advanced usage
+
+If you want to use ANSI codes directly, each is a valid `sty` function name; so, for instance, `sty[1]` is identical to `sty.bold`. You can also use these numbers as HTML tags, prefixed by `a` or `A`; for instance, you can get a blink effect with `<A5>`:
+
+ console.log sty.parse 'I saw the best minds of my generation destroyed by <A5>madness</A5>, starving hysterical naked.'
+
+### More documentation
+
+For a complete list of every function/tag name in sty, run demo.coffee (`coffee demo.coffee`—you'll want to `npm install coffee-script` first if you don't have CoffeeScript installed).
+
+### Disabling Sty
+
+Weird output will result if the environment doesn't support TTY (e.g. if you're looking at output in TextMate). Unfortunately, there's no way for Sty to determine that right now (it will do so if Node supports the `process.isTTY` method—a proposed addition), but you can manually use `sty.disable()` to turn it off.
+
+### License and credits
+
+Copyright (c) 2010 Trevor Burnham and made available under the MIT license:
+
+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.
+
+This distribution includes a slightly modified version of Bob Righter's [node-xml](https://github.com/robrighter/node-xml/), also under the MIT license.
+
+Thanks go to Chris Lloyd for [colored.js](https://github.com/chrislloyd/colored.js), which inspired this project.
@@ -0,0 +1,7 @@
+sty = require 'sty'
+
+functionNames = (name for name of sty)
+functionNames.sort()
+for name in functionNames
+ continue if name in ['disable', 'enable'] or name.match /\d+/
+ console.log sty[name] name
@@ -0,0 +1,122 @@
+(function() {
+ var code, codeRegex, codeStr, codes, name, num, numRegex, resetRegex, resetStr, styleStr, supported, tagRegex, _fn;
+ if (!((process.isTTY != null) && !process.isTTY())) {
+ supported = true;
+ }
+ exports.disable = function() {
+ return supported = false;
+ };
+ exports.enable = function() {
+ return supported = true;
+ };
+ codes = {
+ bold: 1,
+ underline: 4,
+ reverse: 7,
+ black: 30,
+ red: 31,
+ green: 32,
+ yellow: 33,
+ blue: 34,
+ magenta: 35,
+ cyan: 36,
+ white: 37
+ };
+ for (name in codes) {
+ code = codes[name];
+ if (code >= 30 && code < 40) {
+ codes["bg" + name] = code + 10;
+ }
+ }
+ codes.b = codes.bold;
+ codes.u = codes.underline;
+ for (num = 0; num <= 109; num++) {
+ codes[num] = num;
+ }
+ resetStr = "\x1B[0m";
+ codeStr = function(code) {
+ var x, _i, _len, _results;
+ if (code instanceof Array) {
+ return (function() {
+ _results = [];
+ for (_i = 0, _len = code.length; _i < _len; _i++) {
+ x = code[_i];
+ _results.push(codeStr(x));
+ }
+ return _results;
+ }()).join('');
+ } else {
+ return "\x1B[" + code + "m";
+ }
+ };
+ resetRegex = /\x1B\[0m/g;
+ codeRegex = /\x1B\[\d+m/g;
+ tagRegex = /(<\w+>|<A\d+>)|(<\/\w+>|<A\d+>)/i;
+ numRegex = /\d+/;
+ styleStr = function(str, code) {
+ str = str.replace(resetRegex, "" + resetStr + (codeStr(code)));
+ return "" + (codeStr(code)) + str + resetStr;
+ };
+ _fn = function(name, code) {
+ return exports[name] = function(str) {
+ if (str == null) {
+ str = '';
+ }
+ if (supported) {
+ return styleStr(str, code);
+ } else {
+ return str;
+ }
+ };
+ };
+ for (name in codes) {
+ code = codes[name];
+ _fn(name, code);
+ }
+ exports.parse = function(str) {
+ var activeCodes, match, result, tag;
+ if (str == null) {
+ str = '';
+ }
+ result = '';
+ activeCodes = [];
+ while (match = str.match(tagRegex)) {
+ result += str.slice(0, match.index);
+ if (match[1]) {
+ tag = match[1].slice(1, -1);
+ if (tag.match(numRegex)) {
+ tag = tag.slice(1);
+ }
+ if (tag in codes) {
+ if (activeCodes && supported) {
+ result += resetStr;
+ }
+ activeCodes.push(codes[tag]);
+ if (supported) {
+ result += codeStr(activeCodes);
+ }
+ } else {
+ result += match[1];
+ }
+ } else {
+ tag = match[2].slice(2, -1);
+ if (tag.match(numRegex)) {
+ tag = tag.slice(1);
+ }
+ if (tag in codes) {
+ activeCodes.splice(activeCodes.indexOf(codes[tag]), 1);
+ if (supported) {
+ result += resetStr + codeStr(activeCodes);
+ }
+ } else {
+ result += match[2];
+ }
+ }
+ str = str.slice(match.index + (match[1] || match[2]).length);
+ }
+ if (activeCodes && supported) {
+ result += str + resetStr;
+ }
+ return result;
+ };
+}).call(this);
@@ -0,0 +1,22 @@
+{
+ 'name': 'sty',
+ 'description': 'Color and more for the console',
+ 'keywords': ['color', 'console', 'utility'],
+ 'author': 'Trevor Burnham',
+ 'email': 'trevorburnham@gmail.com',
+ 'homepage': 'http://github.com/TrevorBurnham/sty',
+ 'repository':
+ {
+ 'type':'git',
+ 'url':'http://github.com/TrevorBurnham/sty.git'
+ },
+ 'licenses': [{'type': 'MIT'}],
+ 'version': '0.5.0',
+ 'engines': {
+ 'node': '>= 0.2.0'
+ },
+ 'directories': {
+ 'lib': './lib'
+ },
+ 'main': './lib/sty'
+}
@@ -0,0 +1,76 @@
+supported = true unless process.isTTY? and !process.isTTY()
+exports.disable = -> supported = false
+exports.enable = -> supported = true
+
+codes =
+ bold: 1
+ underline: 4
+ reverse: 7
+ black: 30
+ red: 31
+ green: 32
+ yellow: 33
+ blue: 34
+ magenta: 35
+ cyan: 36
+ white: 37
+
+# Background colors:
+for name, code of codes
+ codes["bg#{name}"] = code + 10 if code >= 30 and code < 40
+
+# Aliases:
+codes.b = codes.bold
+codes.u = codes.underline
+
+# Allow code numbers as well
+codes[num] = num for num in [0..109]
+
+# ANSI string sequences
+resetStr = "\x1B[0m"
+codeStr = (code) ->
+ if code instanceof Array
+ (codeStr x for x in code).join ''
+ else
+ "\x1B[#{code}m"
+
+# Regexes
+resetRegex = /\x1B\[0m/g
+codeRegex = /\x1B\[\d+m/g
+tagRegex = /(<\w+>|<A\d+>)|(<\/\w+>|<A\d+>)/i
+numRegex = /\d+/
+
+styleStr = (str, code) ->
+ str = str.replace resetRegex, "#{resetStr}#{codeStr code}" # allow nesting
+ "#{codeStr code}#{str}#{resetStr}"
+
+for name, code of codes
+ exports[name] = (str = '') -> if supported then styleStr str, code else str
+
+exports.parse = (str = '') ->
+ result = ''
+ activeCodes = []
+
+ while match = str.match tagRegex
+ result += str[0...match.index]
+ if match[1] # open tag
+ tag = match[1][1...-1]
+ if tag.match numRegex then tag = tag[1..]
+ if tag of codes
+ result += resetStr if activeCodes and supported
+ activeCodes.push codes[tag]
+ result += codeStr activeCodes if supported
+ else
+ result += match[1]
+ else # close tag
+ tag = match[2][2...-1]
+ if tag.match numRegex then tag = tag[1..]
+ if tag of codes
+ activeCodes.splice activeCodes.indexOf(codes[tag]), 1
+ result += resetStr + codeStr activeCodes if supported
+ else
+ result += match[2]
+ str = str[match.index + (match[1] or match[2]).length..]
+
+ result += str + resetStr if activeCodes and supported
+ result

0 comments on commit 6cca0d9

Please sign in to comment.