Skip to content

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.

Notifications You must be signed in to change notification settings

MichaelCereda/pretty-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

out

Installation

Just use NPM and you're ready to go

npm 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 tasks

pretty = 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})

pretty-cli

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());
// {}

Utils

Motivation

This library has been created to help the js community in creating nice looking command line interfaces with the objective of improving current practices and standardize the output.

Tests

Tests can be run with mocha

mocha test

Contributors

Contributors are very very welcome.
The project needs:

  • People who can create nice looking templates
  • New ideas to help the workflow of developers
  • Pull requests

License

The MIT License (MIT) Copyright (c) 2016 Michael Cereda http://michaelcereda.com

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.

About

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.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published