Coder Social home page Coder Social logo

diffy's Introduction

diffy

A tiny framework for building diff based interactive command line tools.

npm install diffy

Build Status

Basically React, but in the terminal powered by ansi-diff and neat-input.

Usage

var diffy = require('diffy')()
var trim = require('diffy/trim')

diffy.render(function () {
  return trim(`
    Hello user. The time is:
      ${new Date()}
    That is all for now
  `)
})

// re-render every 1s
setInterval(() => diffy.render(), 1000)

You can also use diffy to query input from a user

var input = require('diffy/input')({style: style})
var names = []

input.on('update', () => diffy.render())
input.on('enter', (line) => names.push(line))

diffy.render(function () {
  return trim(`
    Enter your name: ${input.line()}
    List of names: ${names.join(' ')}
  `)
})

function style (start, cursor, end) {
  return start + '[' + (cursor || ' ') + ']' + end
}

See the examples folder for more.

API

var diffy = require('diffy')()

Make a new diffy instance. Writes to stdout.

diffy.render([function])

Trigger a render and/or update the default render function. A render function should simply return a string containing the output you wish to display and then diffy will make sure to only print the diff.

diffy.width

Property containing the width of the terminal.

diffy.height

Property containing the height of the terminal.

diffy.on('resize')

Emitted when the terminal is resized. Triggers a render as well.

diffy.on('render')

Emitted just before a render happens.

var input = require('diffy/input')()

Get a neat-input instance. Use this if you want to accept interactive input.

var trim = require('diffy/trim')

Helper function that trims the indentation of a multiline string. Useful if you have a render function that returns an indented string like in the above example.

Credits

Thank you to @Fouad for donating the module name.

License

MIT

diffy's People

Contributors

mafintosh avatar

Watchers

James Cloos avatar  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.