node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

neat-log

neat-log

npm travis standard

A neat logger for command line tools, inspired by Choo! I've heard so much about how awesome front-end javascript has gotten, I wanted to bring some of that magic to command line applications. Used in the Dat command line tool.

  • Use tagged template literals to output to console log.
  • Automatically diff template output with existing console (via ansi-diff-stream) (pretty much React for your terminal).
  • Update console output in any order with event based output.
  • Pretty neat!

Example

Each neat logger has two components: a view and functions that modify state and emit events.

var neatLog = require('neat-log')
var output = require('neat-log/output')
 
var neat = neatLog(view)
neat.use(countTheSeconds)
 
function view (state) {
  // This gets printed to the console! Wow. So neat.
  return output`
    Hello World!
    I've been running for ${state.seconds} second${state.seconds === 1 ? '' : 's'}.
  `
}
 
function countTheSeconds (state, bus) {
  state.seconds = 0
  setInterval(function () {
    state.seconds++
    bus.emit('render')
  }, 1000)
  bus.emit('render')
}

This example will print to the console:

Hello world!
I've been running for 0 seconds.

And update every second!

More examples:

  • example folder
  • count-files - basic example with a single view and use function.
  • dat - full command line application with multiple views, components, etc.

Install

npm install neat-log

API

Heavily inspired by Choo! If you get confused here, check docs there because they are much nicer =).

var neat = neatLog(view(state), [opts])

  • views is a single function or array of functions that return string(s) to output to the console. views are passed the state as the first argument.
  • opts.logspeed (default 250ms) - throttles how often output is rendered.
  • opts.quiet - shhhh.... do not print anything
  • opts.debug - print the whole view every time render is called.

Both opts.quiet and opts.debug are handled in status-logger.

neat.use(callback(state, bus))

Use a function (state, bus) to change state and emit events via the bus. You can call bus.emit() to emit a new event and bus.on() to listen. Emitter is an instance of nanobus.

bus also has some special functions exposed:

bus.render()

Cause an immediate render (normally it'd be throttled). This is helpful to do before exiting.

bus.clear()

Clear all existing output. This is useful to do before throwing error messages:

bus.clear()
console.error('my error')
process.exit(1)

neat.render()

For an immediate render. Helpful outside of a use function.

neat.reset()

Reset the ansi-diff-stream instance. Can help fix broken lines or other weird printing output. Happens automatically on terminal resize.

var output = require('neat-log/output')

Get a tagger for your template literals to make them nicer. Removes spaces and stuff™.

TODO

  • Use bus.on('*') to make a cool debug option
  • Be smarter about when lines break to multiple lines.
  • Other neat things

License

MIT