Pretty-cli
Pretty-cli is a lightweight utility that helps you to create nice looking command line interfaces. It forces a well structured output and gives unimited flexibility with its templating system without adding any overhead.
Freely inspired by my experience with bunyan
Check the code examples.
Installation
Just use NPM and you're ready to gonpm install pretty-cli --save
Code Example
Instead of using console.log to output your messages:console.log("First error message");
console.log("Other unformatted error message");
with pretty-cli you can
- Use different type of output
pretty = require('pretty-cli')();
pretty.log("Log Message");
pretty.error("Error Message");
pretty.warning("Warning Message");
pretty.info("Info Message");
Use templates that unify your design
pretty = require('pretty-cli')({
template:
})
pretty.log("Log Message");
pretty.error("Error Message");
pretty.warning("Warning Message");
pretty.info("Info Message");
With custom templates you can unify your styles and use complex objects.
pretty = require('pretty-cli')({
template: require('myTemplate')
})
pretty.error({message:'../files/test.js\n',name:'MODULE', type:'title'})
pretty.error({message:'Test', description:"Long description message or code sample"})
Create custom templates in a breeze, they are pure javascript! In this example I'm using colors, but you can use everything you prefer.
There are no limitations.
var colors = require('colors');
var MY_TEMPLATE = {
'info': function(message){return ' I '.bgBlue.black +" " + message},
'error': function(message){return ' E '.bgRed.white +" " + message},
'log': function(message){return ' L '.bgWhite.black +" " + message},
'warning': function(message){return ' W '.bgYellow.black +" " + message}
}
Advanced Use
Create custom print methods to automate special taskspretty = require('pretty-cli')({
template: require('myTemplate')
})
pretty.addCustomMethod('stats', function(content){
return pretty.error({
type:'title',
name:'STATS',
message: "time: "+ content.time+', errors:'+ content.errors+', warnings:'+content.warnings})
})
pretty.error({type:'title', name:'BUILD', message:'complete with errors'})
pretty.stats({time:'30ms', errors:12, warnings:3})

You can also define custom methods using the template itself.
This feature is very useful when you have to define a new message type (by default pretty-cli ships log, error, success, info).
const TEMPLATE = {
//...
note: (message)=>{return 'NOTE:'+message}
}
pretty = require('pretty-cli')({
template: TEMPLATE
})
pretty.note('my note')
Context
Pretty-cli focuses on semplicity and reusability. You don't have to repeat yourself.When you're looping through items sometimes happens that you want to mantain a similar context while changing just specific parts. For this purpose you can use .context() to achieve that functionality.
var cli = require('pretty-cli')();
cli.context({
type:'looped item'
});
//Shows current context
console.log(cli.context());
// { type:'looped item' }
for(var i=0;i<10;i++){
cli.log({message:'Item '+i})
}
Context is automatically merged for every following message.To clean the context you can simply set it to an empty object.
cli.context({
});
//Shows current context
console.log(cli.context());
// {}