3 * Accepts any node Stream instance and hijacks its "write()" function,
4 * so that it can count any newlines that get written to the output.
6 * When a '\n' byte is encountered, then a "newline" event will be emitted
7 * on the stream, with no arguments. It is up to the listeners to determine
8 * any necessary deltas required for their use-case.
12 * var cursor = ansi(process.stdout)
14 * process.stdout.on('newline', function () {
20 * Module dependencies.
23 var assert = require('assert')
24 var NEWLINE = '\n'.charCodeAt(0)
26 function emitNewlineEvents (stream) {
27 if (stream._emittingNewlines) {
28 // already emitting newline events
32 var write = stream.write
34 stream.write = function (data) {
35 // first write the data
36 var rtn = write.apply(stream, arguments)
38 if (stream.listeners('newline').length > 0) {
41 // now try to calculate any deltas
42 if (typeof data == 'string') {
44 processByte(stream, data.charCodeAt(i))
49 processByte(stream, data[i])
57 stream._emittingNewlines = true
59 module.exports = emitNewlineEvents
63 * Processes an individual byte being written to a stream
66 function processByte (stream, b) {
67 assert.equal(typeof b, 'number')
69 stream.emit('newline')