Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit a15fabe5703d55478ab73335cb2b1777ea533d00 @dominictarr committed Feb 15, 2011
Showing with 170 additions and 0 deletions.
  1. +22 −0 README.md
  2. +1 −0 index.js
  3. +24 −0 package.json
  4. +85 −0 style.js
  5. +38 −0 test/style.expresso.js
22 README.md
@@ -0,0 +1,22 @@
+# Style #
+
+functional terminal colours.
+
+
+/*
+typesetting functions:
+
+align
+
+for an array of arrays, count length of each line and allign each column.
+
+[[1,2,3,4], ['one', 'two', 'three','four']]
+becomes
+1 2 3 4
+one two three four
+
+
+
+*/
+
+
1 index.js
@@ -0,0 +1 @@
+module.exports = require('./style')
24 package.json
@@ -0,0 +1,24 @@
+{
+ "name": "style",
+ "description": "color in your node.js console, without monkeypatching",
+ "version": "0.1.0",
+ "author": "Dominic Tarr <dominic.tarr@gmail.com>",
+ "repository": "http://github.com/dominictarr/style.git",
+ "engine": [
+ "node >=0.2.0"
+ ],
+ "main": "./style",
+ "engines": {
+ "node": ">=0.2.0"
+ },
+ "devDependencies":{
+ "expresso": ">=0.6.0"
+ },
+ "directories": {
+ "lib": "."
+ },
+ "homepage": "https://github.com/dominictarr/style",
+ "scripts": {
+ "test": "expresso test/*.js"
+ }
+}
85 style.js
@@ -0,0 +1,85 @@
+
+
+//style.js
+
+/*
+style(list of functions, or function names) // returns a function which will apply those functions to it's args.
+
+
+
+style('red')(message) //make message red,
+style('red','bold')(message) //make message red, and bold.
+style('red','bold', function (v){return v})(message) //make message red, and bold.
+
+style.add(name,function) //add function under name
+
+
+
+*/
+
+var curry = require('curry')
+
+module.exports = nu()
+
+function nu (){
+
+ function style (){
+
+ var funx = []
+ for(var i in arguments){
+ var func = arguments[i]
+ funx.push('function' == typeof func ? func : style[func])
+ }
+
+ return function (v){
+
+ funx.forEach(function (x){
+ v = x(v)
+ })
+
+ return v
+ }
+
+ }
+
+ function termStyle (start,end,content){
+
+ return '\033[' + start + 'm' + content +
+ '\033[' + end + 'm';
+
+ }
+ var styles = {
+ //styles
+ 'bold' : [1, 22],
+ 'italic' : [3, 23],
+ 'underline' : [4, 24],
+ 'inverse' : [7, 27],
+ //grayscale
+ 'white' : [37, 39],
+ 'grey' : [90, 39],
+ 'black' : [90, 39],
+ //colors
+ 'blue' : [34, 39],
+ 'cyan' : [36, 39],
+ 'green' : [32, 39],
+ 'magenta' : [35, 39],
+ 'red' : [31, 39],
+ 'yellow' : [33, 39],
+ };
+
+
+ for(var name in styles){
+ style[name] = curry(styles[name],termStyle)
+
+ }
+
+ style.identity = function (v){return v}
+ style.plain = function (v){
+
+ return v.replace(/\u001b\[\d+m/g,'')
+ }
+
+ style.new = nu
+
+ return style
+}
38 test/style.expresso.js
@@ -0,0 +1,38 @@
+//style.expresso.js
+
+var style = require ('../style')
+ , it = require('it-is')
+
+exports ['style function returns functions'] = function (){
+
+ it(style(function (v) { return v })(10)).equal(10)
+
+ it(style).property('identity',it.function())
+
+ var r = Math.random()
+
+ it(style('identity')(r)).equal(r)
+
+}
+
+
+var styles = [ 'bold', 'underline', 'italic'
+ , 'inverse', 'grey', 'yellow'
+ , 'red', 'green', 'blue', 'white'
+ , 'cyan', 'magenta']
+
+exports ['style has colour functions'] = function (){
+
+ it(styles).every(function (e){
+ style(e,console.log)(e + ' ')
+ it(style(e)('x')).matches(/\u001b\[\d+m.*?\u001b\[\d+m/g)
+ })
+}
+
+exports ['style has plain (strips colour info)'] = function (){
+
+ it(styles).every(function (e){
+ style(e,'plain',console.log)(e + ' ')
+ it(style(e,'plain')('asdfzxcv')).equal('asdfzxcv')
+ })
+}

0 comments on commit a15fabe

Please sign in to comment.