Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 58 additions & 42 deletions index.js
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");

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.


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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be this.push(null) to correctly end the stream.

Copy link
Owner Author

Choose a reason for hiding this comment

The 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 this.push(null); when c streams ends but that just doesn't draw anything at all.

To add context:
Both variations of this.push(null) and this.emit('end') work well with this picture-tuber branch if you test it out with the CLI util: node bin/tube.js /Users/lirantal/projects/forks/blessed-contrib/examples/media/flower.png:

image

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:

  • cd picture-tuber
  • git checkout feat/refactor-streams-use
  • npm link
  • cd blessed-contrib
  • npm link picture-tuber
  • cd examples
  • node picture.js

});
}

_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);
};