GithubHelp home page GithubHelp logo

kschoche / cliff Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flatiron/cliff

0.0 0.0 0.0 393 KB

Your CLI formatting friend

Home Page: http://github.com/nodejitsu/cliff

License: MIT License

JavaScript 100.00%

cliff's Introduction

cliff

CLI output formatting tools: "Your CLI Formatting Friend".

Installation

Installing npm (node package manager)

  curl http://npmjs.org/install.sh | sh

Installing cliff

  [sudo] npm install cliff

Usage

There are a number of methods available in Cliff for common logging tasks in command-line tools. If you're looking for more usage, checkout the examples in this repository:

  1. Logging rows of data
  2. Inspecting Objects

Logging rows of data

cliff.stringifyRows(rows[, colors])

Takes a set of Arrays and row headers and returns properly formatted and padded rows. Here's a sample:

  var cliff = require('../lib/cliff');

  var rows = [
    ['Name',  'Flavor',    'Dessert'],
    ['Alice', 'cherry',    'yogurt'],
    ['Bob',   'carmel',    'apples'],
    ['Joe',   'chocolate', 'cake'],
    ['Nick',  'vanilla',   'ice cream']
  ];

  console.log(cliff.stringifyRows(rows, ['red', 'blue', 'green']));

output from string-rows.js

cliff.putRows(level, rows[, colors])

The putRows method is a simple helper that takes a set of Arrays and row headers and logs properly formatted and padded rows (logs stringifyRows to winston). Here's a quick sample:

  var cliff = require('../lib/cliff');

  var rows = [
    ['Name',  'Flavor',    'Dessert'],
    ['Alice', 'cherry',    'yogurt'],
    ['Bob',   'carmel',    'apples'],
    ['Joe',   'chocolate', 'cake'],
    ['Nick',  'vanilla',   'ice cream']
  ];

  cliff.putRows('data', rows, ['red', 'blue', 'green']);

The resulting output on the command-line would be:

output from put-rows.js

cliff.stringifyObjectRows(objs, properties[, colors]) used to be: cliff.rowifyObjects(objs, properties, colors)

Takes a set of Objects and the properties to extract from them and returns properly formatted and padded rows. Here's a sample:

  var cliff = require('../lib/cliff');

  var objs = [], obj = {
    name: "bazz",
    address: "1234 Nowhere Dr.",
  };

  for (var i = 0; i < 10; i++) {
    objs.push({
      name: obj.name,
      address: obj.address,
      id: Math.random().toString()
    });
  }

  console.log(cliff.stringifyObjectRows(objs, ['id', 'name', 'address'], ['red', 'blue', 'green']));

output from string-object-rows.js

cliff.putObjectRows(level, objs, properties[, colors])

Takes a set of Objects and the properties to extract from them and it will log to the console. (it prints stringifyObjectRows with winston). Here's a sample:

  var cliff = require('../lib/cliff');

  var objs = [], obj = {
    name: "bazz",
    address: "1234 Nowhere Dr.",
  };

  for (var i = 0; i < 10; i++) {
    objs.push({
      name: obj.name,
      address: obj.address,
      id: Math.random().toString()
    });
  }

  cliff.putObjectRows('data', objs, ['id', 'name', 'address']);

output from string-object-rows.js

Colors Parameter

The colors parameter is an array that colors the first row. It uses the colors.js. You can use any of those.

  var cliff = require('../lib/cliff');

  var rows = [
    ['Name',        'Flavor',              'Dessert'],
    ['Alice'.grey,  'cherry'.cyan,         'yogurt'.yellow],
    ['Bob'.magenta, 'carmel'.rainbow,      'apples'.white],
    ['Joe'.italic,  'chocolate'.underline, 'cake'.inverse],
    ['Nick'.bold,   'vanilla',             'ice cream']
  ];

  cliff.putRows('data', rows, ['red', 'blue', 'green']);

The resulting output on the command-line would be:

output from puts-rows-colors.js

Inspecting Objects

cliff.inspect(obj)

The inspect method is a lightweight wrapper to a pre-configured eyes inspector. If you wish to change the coloring of objects that are logged using cliff you only need to override cliff.inspect with a new eyes inspector. Here is how to use it:

  var cliff = require('../lib/cliff');

  console.log(cliff.inspect({
    literal: "bazz",
    arr: [
      "one",
      2,
    ],
    obj: {
      host: "localhost",
      port: 5984,
      auth: {
        username: "admin",
        password: "password"
      }
    }
  }));

output from inspect.js

cliff.putObject(obj, [rewriters, padding])

The putObject method is a simple helper function for prefixing and styling inspected object output from eyes. Here's a quick sample:

var cliff = require('cliff');

cliff.putObject({
  literal: "bazz",
  arr: [
    "one",
    2,
  ],
  obj: {
    host: "localhost",
    port: 5984,
    auth: {
      username: "admin",
      password: "password"
    }
  }
});

The resulting output on the command-line would be:

output from put-object.js

Run Tests

All of the cliff tests are written in vows, and cover all of the use cases described above.

  npm test

Motivation

Cliff is the swiss army knife of CLI formatting tools. It is based on highly flexible and powerful libraries:

  • winston: A multi-transport async logging library for node.js
  • eyes: A customizable value inspector for node.js
  • colors: Get colors in your node.js console like what

cliff's People

Contributors

indexzero avatar pksunkara avatar indutny avatar jcrugzz avatar coderarity avatar marak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.