• Stars
    star
    194
  • Rank 193,210 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Your CLI formatting friend

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

Author: Charlie Robbins

More Repositories

1

director

a tiny and isomorphic URL router for JavaScript
JavaScript
5,622
star
2

prompt

a beautiful command-line prompt for node.js
JavaScript
1,860
star
3

cradle

a high-level CouchDB client for Node.js
JavaScript
1,377
star
4

flatiron

framework components for node.js and the browser
JavaScript
1,340
star
5

plates

Light-weight, logic-less, DSL-free, templates for all javascript environments!
JavaScript
830
star
6

revalidator

A cross-browser / node.js validator powered by JSON Schema
JavaScript
592
star
7

blacksmith

A generic static site generator built using flatiron, plates, and marked.
JavaScript
556
star
8

resourceful

an isomorphic Resource engine for JavaScript
JavaScript
352
star
9

restful

reflect RESTful Director routers from Resourceful resources
JavaScript
236
star
10

union

A hybrid buffered / streaming middleware kernel backwards compatible with connect.
JavaScript
136
star
11

utile

A drop-in replacement for `util` with some additional advantageous functions
JavaScript
95
star
12

socketful

reflect socket.io event maps from Resourceful resources
JavaScript
44
star
13

director-reflector

reflect isomorphic HTTP client API wrappers from Director routers
JavaScript
21
star
14

viewful

a tiny and isomorphic consolidated view engine for JavaScript
JavaScript
19
star
15

cli-config

Encapsulated commands for managing configuration in flatiron CLI apps
JavaScript
17
star
16

www

The public web presence for flatiron
CSS
12
star
17

director-explorer

HTML / Text view of `Director.router` instances
JavaScript
12
star
18

commandful

reflect Command Line Interfaces from Resourceful resources
JavaScript
12
star
19

http-users

Encapsulated routes and resources for managing users in flatiron HTTP apps
JavaScript
11
star
20

cli-users

Encapsulated commands for managing users in flatiron CLI apps
JavaScript
10
star
21

formful

reflect HTML forms from Resourceful resources
JavaScript
10
star
22

blacksmith-sites

a collection of pre-built site generators for the blacksmith engine
JavaScript
7
star
23

hello-world-flatiron-api

Sample Flatironjs Application - Hello World API
JavaScript
7
star
24

cortex.js

A front-end framework
JavaScript
4
star
25

broadway-godot

Plugin that enables godot on the broadway instance through magic
JavaScript
1
star