Git Product home page Git Product logo

ansi-diff's Introduction

ansi-diff

A module that diffs an input buffer with the previous one provided to it and outputs the diff as ANSI. If you write out the diff to the terminal it will update the previous output with minimal changes

npm install ansi-diff

Build Status

Usage

var diff = require('ansi-diff')({
  // if you want to support word wrapping, provide the terminal width
  width: process.stdout.columns
})

// render now
render()

// render every 1s
setInterval(render, 1000)

function render () {
  // will update the terminal with minimal changes
  process.stdout.write(diff.update('hello world...\n' + new Date()))
}

It also has terminal resize support

process.stdout.on('resize', function () {
  diff.resize({width: process.stdout.columns})
  render()
})

API

var diff = ansiDiff([options])

Create a new differ. Options include:

{
  width: terminalWidth,
  height: terminalHeight
}

If you provide the terminal width ansi-diff will be able to support word wrapping done by the terminal. That means that if you print out a line that is too long to fit in the terminal the diff will still work.

var changes = diff.update(buffer, opts)

Update the buffer and return the diff between this and the previous one. The diff is returned in ANSI as a buffer so you can write it out to the terminal.

  • opts.moveTo - array of [column,row] to move the cursor after diffing the screen (in absolute coordinates)

diff.resize([options])

Update the terminal width / height. If you are writing to stdout you should update the width when process.stdout.on('resize') is fired.

Options should look like this:

{
  width: terminalWidth,
  height: terminalHeight
}

diff.toString()

Returns the last diffed string.

diff.width

Property containing the last set terminal width.

diff.height

Property containing the last set terminal height.

License

ansi-diff's People

Contributors

mafintosh avatar zkochan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ansi-diff's Issues

New lines are removed on update

Code to repro:
https://gist.github.com/zkochan/426aa7265c509fb72bbe7ede7e6c2747

I'd expect an output like this in the end:

registry.npmjs.org/dtrace-provider/0.8.6 |     install:   TOUCH Release/obj.target/DTraceProviderStub.stamp
registry.npmjs.org/dtrace-provider/0.8.6 |     install: make: Leaving directory '/home/zkochan/src/pnpm/headless/node_modules/.registry.npmjs.org/dtrace-provider/0.8.6/node_modules/dtrace-provider/build'
registry.npmjs.org/dtrace-provider/0.8.6 |     install: gyp info ok 

registry.npmjs.org/fsevents/1.1.3        |     install$ node install
registry.npmjs.org/fsevents/1.1.3        |     install: done

registry.npmjs.org/@zkochan/husky/0.0.0  |     install$ node ./bin/install.js
registry.npmjs.org/@zkochan/husky/0.0.0  |     install: husky
registry.npmjs.org/@zkochan/husky/0.0.0  |     install: setting up Git hooks
registry.npmjs.org/@zkochan/husky/0.0.0  |     install: trying to install from sub 'node_module' directory, skipping Git hooks installation

registry.npmjs.org/sinon/4.5.0           | postinstall$ node scripts/support-sinon.js
registry.npmjs.org/sinon/4.5.0           | postinstall: Have some ❤️ for Sinon? You can support the project via Open Collective:
registry.npmjs.org/sinon/4.5.0           | postinstall:  > https://opencollective.com/sinon/donate
registry.npmjs.org/sinon/4.5.0           | postinstall: 

But this is printed by ansi-diff:

registry.npmjs.org/dtrace-provider/0.8.6 |     install:   TOUCH Release/obj.target/DTraceProviderStub.stamp
registry.npmjs.org/dtrace-provider/0.8.6 |     install: make: Leaving directory '/home/zkochan/src/pnpm/headless/node_modules/.registry.npmjs.org/dtrace-provider/0.8.6/node_modules/dtrace-provider/build'
registry.npmjs.org/dtrace-provider/0.8.6 |     install: gyp info ok 
registry.npmjs.org/fsevents/1.1.3        |     install$ node install
registry.npmjs.org/fsevents/1.1.3        |     install: done
registry.npmjs.org/@zkochan/husky/0.0.0  |     install$ node ./bin/install.js
registry.npmjs.org/@zkochan/husky/0.0.0  |     install: husky
registry.npmjs.org/@zkochan/husky/0.0.0  |     install: setting up Git hooks
registry.npmjs.org/@zkochan/husky/0.0.0  |     install: trying to install from sub 'node_module' directory, skipping Git hooks installation
registry.npmjs.org/sinon/4.5.0           | postinstall$ node scripts/support-sinon.js
registry.npmjs.org/sinon/4.5.0           | postinstall: Have some ❤️ for Sinon? You can support the project via Open Collective:
registry.npmjs.org/sinon/4.5.0           | postinstall:  > https://opencollective.com/sinon/donate
registry.npmjs.org/sinon/4.5.0           | postinstall: 

Environment:
Running on Linux (Elementary OS)
The Elementary OS terminal. Also reproducible in the hyper terminal.

Add support for windows (No, not the operating system ;-) )

In terminals, a window is a sub-region of the screen, similar to boxes in blessed. I've review the code and it's focused on generating ANSI diffs to be directly printed in the screen, but seems everything is in place to add support for windows. To do so, seems the only missing parts are:

  • top and left options in addition to width and height. By default they can be set to zero, but if set, all the newlines and clear screen moves should be set to their values instead of zero, so it's easy.
  • add right and bottom options, or replace width and height with them, because if a windows have non-zero top and left values, then its width and height are not anymore the window right and bottom ones.
  • hard wrapping. At this moment, if a line is longer than screen width, it's left to the terminal itself to do the wrapping to the new line, but with non-zero left value, then it would print at the left side of the screen instead of the window. Hard wrapping would be done just by inserting moveTo() calls when the end of a window is reached.
  • windows inception, allowing to have windows relatives to another parent windows :-) Although maybe this could be done in another module, but having a test for that would not hurt...

If you don't have the time to work on that, maybe I could be able to start a pull-request and start discussing it... :-)

No License

There is not a license on this module =)

Messages from Node break the output

When node prints a message, the output gets broken. For instance, when pnpm runs child processes, sometimes Node prints:

(node:21820) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 SIGINT listeners added. Use emitter.setMaxListeners() to increase limit

This message breaks the output written by ansi-diff. ansi-diff should either skip lines printed by other tools or rewrite them.

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.