-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(streams): refactor streams and buffer usage #1
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,60 @@ | ||
var PNG = require('png-js'); | ||
var charm = require('charm'); | ||
var x256 = require('x256'); | ||
var buffers = require('buffers'); | ||
var es = require('event-stream'); | ||
|
||
var Stream = require('stream').Stream; | ||
|
||
module.exports = function (opts) { | ||
if (!opts) opts = {}; | ||
if (!opts.cols) opts.cols = 80; | ||
|
||
var c = charm(); | ||
var bufs = buffers(); | ||
|
||
var ws = es.writeArray(function (err, bufs) { | ||
var data = buffers(bufs).slice(); | ||
var png = new PNG(data); | ||
|
||
png.decode(function (pixels) { | ||
var dx = png.width / opts.cols; | ||
var dy = 2 * dx; | ||
|
||
for (var y = 0; y < png.height; y += dy) { | ||
for (var x = 0; x < png.width; x += dx) { | ||
var i = (Math.floor(y) * png.width + Math.floor(x)) * 4; | ||
|
||
var ix = x256([ pixels[i], pixels[i+1], pixels[i+2] ]); | ||
if (pixels[i+3] > 0) { | ||
c.background(ix).write(' '); | ||
} | ||
else { | ||
c.display('reset').write(' '); | ||
} | ||
} | ||
c.display('reset').write('\r\n'); | ||
} | ||
|
||
c.display('reset').end(); | ||
}); | ||
var PNG = require("png-js"); | ||
var charm = require("charm"); | ||
var x256 = require("x256"); | ||
|
||
var { Duplex } = require("stream"); | ||
|
||
class PictureTube extends Duplex { | ||
constructor(opts = {}) { | ||
super(); | ||
this.data = []; | ||
this.cols = opts.cols || 80; | ||
|
||
this.c = charm(); | ||
|
||
this.c.on("data", chunk => { | ||
this.push(chunk); | ||
}); | ||
|
||
return es.duplex(ws, c); | ||
|
||
this.c.on("end", () => { | ||
this.emit("end"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be this.push(null) to correctly end the stream. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for jumping in Matteo! I saw that in the docs as well for a readable stream implementation and had tried To add context: The issue is when I use this branch of picture-tuber in blessed-contrib (its used in the picture widget). To re-construct that setup:
|
||
}); | ||
} | ||
|
||
_read() {} | ||
|
||
_write(chunk, encoding, callback) { | ||
this.data.push(chunk); | ||
callback(); | ||
} | ||
|
||
_final(callback) { | ||
var png = new PNG(Buffer.concat(this.data)); | ||
|
||
png.decode(pixels => { | ||
var dx = png.width / this.cols; | ||
var dy = 2 * dx; | ||
|
||
for (var y = 0; y < png.height; y += dy) { | ||
for (var x = 0; x < png.width; x += dx) { | ||
var i = (Math.floor(y) * png.width + Math.floor(x)) * 4; | ||
|
||
var ix = x256([pixels[i], pixels[i + 1], pixels[i + 2]]); | ||
if (pixels[i + 3] > 0) { | ||
this.c.background(ix).write(" "); | ||
} else { | ||
this.c.display("reset").write(" "); | ||
} | ||
} | ||
this.c.display("reset").write("\r\n"); | ||
} | ||
|
||
this.c.display("reset").end(); | ||
callback(); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = opts => { | ||
return new PictureTube(opts); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should really use readable-stream.