2015-07-16 10:19:45 +00:00
|
|
|
/**
|
|
|
|
* ansiimage.js - render PNGS/GIFS as ANSI
|
|
|
|
* Copyright (c) 2013-2015, Christopher Jeffrey and contributors (MIT License).
|
|
|
|
* https://github.com/chjj/blessed
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modules
|
|
|
|
*/
|
|
|
|
|
|
|
|
var cp = require('child_process')
|
|
|
|
, path = require('path')
|
|
|
|
, fs = require('fs');
|
|
|
|
|
|
|
|
var helpers = require('../helpers');
|
|
|
|
var colors = require('../colors');
|
|
|
|
|
|
|
|
var Node = require('./node');
|
|
|
|
var Box = require('./box');
|
|
|
|
|
|
|
|
var tng = require('../../vendor/tng');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ANSIImage
|
|
|
|
*/
|
|
|
|
|
|
|
|
function ANSIImage(options) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (!(this instanceof Node)) {
|
|
|
|
return new ANSIImage(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
options.shrink = true;
|
|
|
|
|
|
|
|
Box.call(this, options);
|
|
|
|
|
|
|
|
this.scale = this.options.scale || 1.0;
|
|
|
|
this.options.animate = this.options.animate !== false;
|
|
|
|
this._noFill = true;
|
|
|
|
|
|
|
|
if (this.options.file) {
|
|
|
|
this.setImage(this.options.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.screen.on('prerender', function() {
|
|
|
|
var lpos = self.lpos;
|
|
|
|
if (!lpos) return;
|
|
|
|
// prevent image from blending with itself if there are alpha channels
|
|
|
|
self.screen.clearRegion(lpos.xi, lpos.xl, lpos.yi, lpos.yl);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ANSIImage.prototype.__proto__ = Box.prototype;
|
|
|
|
|
|
|
|
ANSIImage.prototype.type = 'ansiimage';
|
|
|
|
|
|
|
|
ANSIImage.curl = function(url) {
|
|
|
|
try {
|
|
|
|
return cp.execFileSync('curl',
|
|
|
|
['-s', '-A', '', url],
|
|
|
|
{ stdio: ['ignore', 'pipe', 'ignore'] });
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return cp.execFileSync('wget',
|
|
|
|
['-U', '', '-O', '-', url],
|
|
|
|
{ stdio: ['ignore', 'pipe', 'ignore'] });
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
throw new Error('curl or wget failed.');
|
|
|
|
};
|
|
|
|
|
|
|
|
ANSIImage.prototype.setImage = function(file) {
|
|
|
|
this.file = typeof file === 'string' ? file : null;
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
if (/^https?:/.test(file)) {
|
|
|
|
file = ANSIImage.curl(file);
|
|
|
|
}
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
var width = this.position.width;
|
|
|
|
var height = this.position.height;
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
if (width != null) {
|
|
|
|
width = this.width;
|
|
|
|
}
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
if (height != null) {
|
|
|
|
height = this.height;
|
|
|
|
}
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
try {
|
|
|
|
this.setContent('');
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
this.img = tng(file, {
|
|
|
|
colors: colors,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
scale: this.scale,
|
|
|
|
ascii: this.options.ascii,
|
|
|
|
speed: this.options.speed,
|
|
|
|
filename: this.file
|
|
|
|
});
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
if (width == null || height == null) {
|
|
|
|
this.width = this.img.cellmap[0].length;
|
|
|
|
this.height = this.img.cellmap.length;
|
|
|
|
}
|
2015-07-17 10:17:59 +00:00
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
if (this.img.frames && this.options.animate) {
|
2015-07-17 10:17:59 +00:00
|
|
|
this.play();
|
2015-07-16 10:19:45 +00:00
|
|
|
} else {
|
2015-07-17 10:17:59 +00:00
|
|
|
this.cellmap = this.img.cellmap;
|
2015-07-16 10:19:45 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
this.setContent('Image Error: ' + e.message);
|
|
|
|
this.img = null;
|
|
|
|
this.cellmap = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-17 10:17:59 +00:00
|
|
|
ANSIImage.prototype.play = function(callback) {
|
|
|
|
var self = this;
|
|
|
|
return this.img.play(callback || function(bmp, cellmap) {
|
|
|
|
self.cellmap = cellmap;
|
|
|
|
self.screen.render();
|
|
|
|
});
|
2015-07-17 10:06:33 +00:00
|
|
|
};
|
2015-07-17 10:14:45 +00:00
|
|
|
|
2015-07-17 10:06:33 +00:00
|
|
|
ANSIImage.prototype.pause = function() {
|
|
|
|
return this.img.pause();
|
|
|
|
};
|
2015-07-17 10:14:45 +00:00
|
|
|
|
2015-07-17 10:06:33 +00:00
|
|
|
ANSIImage.prototype.stop = function() {
|
|
|
|
return this.img.stop();
|
|
|
|
};
|
|
|
|
|
2015-07-16 10:19:45 +00:00
|
|
|
ANSIImage.prototype.clearImage = function() {
|
2015-07-17 10:17:59 +00:00
|
|
|
if (this.img) {
|
2015-07-17 10:06:33 +00:00
|
|
|
this.stop();
|
|
|
|
}
|
2015-07-16 10:19:45 +00:00
|
|
|
this.setContent('');
|
|
|
|
this.img = null;
|
|
|
|
this.cellmap = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
ANSIImage.prototype.render = function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var coords = this._render();
|
|
|
|
if (!coords) return;
|
|
|
|
|
|
|
|
if (this.img) {
|
|
|
|
this.img.renderElement(this.cellmap, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return coords;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = ANSIImage;
|