Comparing version 1.1.2 to 1.2.0
var color = require('./lib/color'); | ||
exports = module.exports = color; | ||
exports.program = require('./lib/program'); | ||
var logging = require('./lib/logging'); | ||
exports.logging = logging; | ||
exports.Logging = logging.Logging; | ||
exports.scheme = require('./lib/scheme'); |
@@ -14,4 +14,7 @@ /* | ||
exports.isatty = false; | ||
exports.disabled = false; | ||
function isColorSupported() { | ||
if (exports.disabled) return false; | ||
// you can force to tty | ||
@@ -18,0 +21,0 @@ if (!exports.isatty && !tty.isatty()) return false; |
@@ -12,14 +12,6 @@ /* | ||
var EventEmitter = require('events').EventEmitter; | ||
var scheme = require('./scheme'); | ||
var color = require('./color'); | ||
var paint = color.paint; | ||
var count = 0; | ||
var _levels = { | ||
'debug': 10, | ||
'info': 20, | ||
'warn': 30, | ||
'error': 40, | ||
'log': 50 | ||
}; | ||
function colorize(name, text) { | ||
@@ -37,17 +29,9 @@ var func; | ||
function log(context, level, args) { | ||
if (_levels[context.level] > _levels[level]) return; | ||
var text = ''; | ||
var stream = process.stdout; | ||
text += Array(count + 1).join(' '); | ||
if (count) { | ||
text += context.icons.logIcon; | ||
} | ||
if (level === 'error') { | ||
stream = process.stderr; | ||
} | ||
var name = context.colors[level]; | ||
text += colorize(name, util.format.apply(context, args)) + os.EOL; | ||
stream.write(text); | ||
var definedLevels = { | ||
'debug': 1, | ||
'info': 2, | ||
'start': 2, | ||
'end': 2, | ||
'warn': 3, | ||
'error': 4 | ||
} | ||
@@ -57,88 +41,73 @@ | ||
this.level = level || 'info'; | ||
this.indent = 0; | ||
this.scheme = scheme.arrow; | ||
} | ||
Logging.prototype.__proto__ = EventEmitter.prototype; | ||
if (os.type() === 'Windows_NT') { | ||
Logging.prototype.icons = { | ||
logIcon: paint('|- ').cyan.color, | ||
startIcon: paint('# ').bold.magenta.color, | ||
endIcon: paint('*- ').cyan.color | ||
}; | ||
} else { | ||
Logging.prototype.icons = { | ||
logIcon: paint('➠ ').cyan.color, | ||
startIcon: paint('⚑ ').bold.magenta.color, | ||
endIcon: paint('➥ ').cyan.color | ||
}; | ||
Logging.prototype.log = function() { | ||
var level = arguments[0], msg, args; | ||
if (arguments.length === 2 && definedLevels.hasOwnProperty(level)) { | ||
args = Array.prototype.slice.call(arguments[1]); | ||
} else if (definedLevels.hasOwnProperty(level)) { | ||
args = Array.prototype.slice.call(arguments).slice(1); | ||
} else { | ||
level = 'info'; | ||
args = arguments; | ||
} | ||
msg = util.format.apply(this, args); | ||
this.emit('logging-' + level); | ||
if (definedLevels[level] < definedLevels[this.level]) return; | ||
var text = Array(this.indent + 1).join(' '); | ||
var levelScheme = this.scheme[level] || {}; | ||
text += (levelScheme.start || ''); | ||
if (levelScheme.color) { | ||
text += colorize(levelScheme.color, msg); | ||
} else { | ||
text += msg; | ||
} | ||
text += (levelScheme.end || ''); | ||
if (level === 'error') { | ||
process.stderr.write(text + os.EOL); | ||
} else { | ||
process.stdout.write(text + os.EOL); | ||
} | ||
} | ||
Logging.prototype.colors = { | ||
debug: 'grey', | ||
info: 'green', | ||
warn: 'yellow', | ||
error: 'red' | ||
}; | ||
Logging.prototype.start = function() { | ||
if (_levels[this.level] > 25) return; | ||
var text = Array(count + 1).join(' '); | ||
text += this.icons.startIcon; | ||
text += paint(util.format.apply(this, arguments)).bold.color + os.EOL; | ||
process.stdout.write(text); | ||
count += 1; | ||
this.emit('logging-start'); | ||
}; | ||
this.log('start', arguments); | ||
this.indent += 1; | ||
} | ||
Logging.prototype.end = function() { | ||
if (_levels[this.level] > 25) return; | ||
var text = ''; | ||
text += Array(count + 1).join(' '); | ||
if (count) { | ||
text += this.icons.endIcon; | ||
} | ||
text += util.format.apply(this, arguments) + os.EOL; | ||
process.stdout.write(text); | ||
count -= 1; | ||
this.emit('logging-end'); | ||
}; | ||
Logging.prototype.log = function() { | ||
log(this, 'log', arguments); | ||
}; | ||
this.log('end', arguments); | ||
this.indent -= 1; | ||
} | ||
Logging.prototype.debug = function() { | ||
log(this, 'debug', arguments); | ||
}; | ||
this.log('debug', arguments); | ||
} | ||
Logging.prototype.info = function() { | ||
log(this, 'info', arguments); | ||
}; | ||
this.log('info', arguments); | ||
} | ||
Logging.prototype.warn = function() { | ||
log(this, 'warn', arguments); | ||
this.emit('logging-warn'); | ||
}; | ||
this.log('warn', arguments); | ||
} | ||
Logging.prototype.error = function() { | ||
log(this, 'error', arguments); | ||
this.emit('logging-error'); | ||
}; | ||
this.log('error', arguments); | ||
} | ||
Logging.prototype.config = function(obj) { | ||
var self = this; | ||
if (obj.verbose) { | ||
self.level = 'debug'; | ||
this.level = 'debug'; | ||
} | ||
if (obj.quiet) { | ||
self.level = 'warn'; | ||
this.level = 'warn'; | ||
} | ||
if (obj.level) { | ||
self.level = obj.level; | ||
if (obj.level && definedLevels.hasOwnProperty(obj.level)) { | ||
this.level = obj.level; | ||
} | ||
if (obj in _levels) { | ||
self.level = obj; | ||
if (definedLevels.hasOwnProperty(obj)) { | ||
this.level = obj; | ||
} | ||
if (obj.colors) { | ||
Object.keys(obj.colors).forEach(function(key) { | ||
self.colors[key] = obj.colors[key]; | ||
}); | ||
} | ||
if (obj.icons) { | ||
Object.keys(obj.icons).forEach(function(key) { | ||
self.icons[key] = obj.icons[key]; | ||
}); | ||
} | ||
}; | ||
@@ -145,0 +114,0 @@ |
{ | ||
"name": "colorful", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "colorful if a terminal tool for color, logging and command", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -55,2 +55,7 @@ # Colorful | ||
require('colorful').isSupported | ||
// we can disable color | ||
require('colorful').disabled = true | ||
require('colorful').isSupported | ||
// => false | ||
``` | ||
@@ -97,2 +102,33 @@ | ||
## Customize | ||
## Scheme | ||
Define your logging scheme: | ||
```javascript | ||
var scheme = { | ||
debug: { | ||
start: '# ', | ||
color: 'gray', | ||
end: ' #' | ||
}, | ||
info: { | ||
start: 'v ', | ||
color: 'green' | ||
}, | ||
warn: { | ||
start: '! ', | ||
color: 'yellow' | ||
}, | ||
error: { | ||
start: 'x ', | ||
color: 'red' | ||
}, | ||
start: { | ||
start: '> ' | ||
}, | ||
end: { | ||
start: '< ' | ||
} | ||
} | ||
logging.scheme = scheme; | ||
``` |
@@ -25,6 +25,4 @@ var Logging = require('../lib/logging').Logging; | ||
logger.start('Hello world'); | ||
logger.config({icons: {startIcon: '+'}}); | ||
logger.start('Hello world'); | ||
logger.config({colors: {info: null}}); | ||
logger.info('this is info'); | ||
@@ -31,0 +29,0 @@ }); |
Sorry, the diff of this file is not supported yet
133
17246
426