2015-04-19 03:27:53 +00:00
|
|
|
/**
|
2015-04-19 06:58:41 +00:00
|
|
|
* ansi-viewer
|
2015-04-19 03:27:53 +00:00
|
|
|
* ANSI art viewer for node.
|
|
|
|
* Copyright (c) 2015, Christopher Jeffrey and contributors (MIT License).
|
|
|
|
* https://github.com/chjj/blessed
|
|
|
|
*/
|
|
|
|
|
2015-04-19 19:09:05 +00:00
|
|
|
var blessed = require('blessed')
|
2015-04-19 03:27:53 +00:00
|
|
|
, request = require('request')
|
2015-04-21 14:07:11 +00:00
|
|
|
, singlebyte = require('./singlebyte')
|
|
|
|
, fs = require('fs');
|
2015-04-19 03:27:53 +00:00
|
|
|
|
|
|
|
// $ wget -r -o log --tries=10 'http://artscene.textfiles.com/ansi/'
|
2015-04-19 05:14:51 +00:00
|
|
|
// $ grep 'http.*\.ans$' log | awk '{ print $3 }' > ansi-art.list
|
2015-04-19 03:27:53 +00:00
|
|
|
|
|
|
|
var urls = fs.readFileSync(__dirname + '/ansi-art.list', 'utf8').trim().split('\n');
|
|
|
|
|
|
|
|
var map = urls.reduce(function(map, url) {
|
|
|
|
map[/([^.\/]+\/[^.\/]+)\.ans$/.exec(url)[1]] = url;
|
|
|
|
return map;
|
|
|
|
}, {});
|
|
|
|
|
2015-04-19 06:58:41 +00:00
|
|
|
var max = Object.keys(map).reduce(function(out, text) {
|
|
|
|
return Math.max(out, text.length);
|
|
|
|
}, 0) + 6;
|
|
|
|
|
2015-04-20 21:09:01 +00:00
|
|
|
var screen = blessed.screen({
|
2015-04-19 03:27:53 +00:00
|
|
|
smartCSR: true,
|
|
|
|
dockBorders: true
|
|
|
|
});
|
|
|
|
|
|
|
|
var art = blessed.terminal({
|
|
|
|
parent: screen,
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
2015-04-21 00:40:17 +00:00
|
|
|
height: 60,
|
|
|
|
// some are 78/80, some are 80/82
|
|
|
|
width: 82,
|
|
|
|
border: 'line',
|
|
|
|
tags: true,
|
|
|
|
label: ' {bold}{cyan-fg}ANSI Art{/cyan-fg}{/bold} (Drag Me) ',
|
|
|
|
handler: function() {},
|
|
|
|
draggable: true
|
2015-04-19 03:27:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var list = blessed.list({
|
|
|
|
parent: screen,
|
2015-04-21 00:40:17 +00:00
|
|
|
label: ' {bold}{cyan-fg}Art List{/cyan-fg}{/bold} (Drag Me) ',
|
2015-04-19 03:27:53 +00:00
|
|
|
tags: true,
|
|
|
|
draggable: true,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
2015-04-19 06:58:41 +00:00
|
|
|
width: max,
|
|
|
|
height: '50%',
|
2015-04-19 03:27:53 +00:00
|
|
|
keys: true,
|
|
|
|
vi: true,
|
|
|
|
mouse: true,
|
|
|
|
border: 'line',
|
|
|
|
scrollbar: {
|
|
|
|
ch: ' ',
|
|
|
|
track: {
|
|
|
|
bg: 'cyan'
|
|
|
|
},
|
|
|
|
style: {
|
|
|
|
inverse: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
style: {
|
|
|
|
item: {
|
|
|
|
hover: {
|
|
|
|
bg: 'blue'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
selected: {
|
|
|
|
bg: 'blue',
|
|
|
|
bold: true
|
|
|
|
}
|
2015-04-19 05:14:51 +00:00
|
|
|
},
|
|
|
|
search: function(callback) {
|
|
|
|
prompt.input('Search:', '', function(err, value) {
|
|
|
|
if (err) return;
|
2015-04-19 05:25:13 +00:00
|
|
|
return callback(null, value);
|
2015-04-19 05:14:51 +00:00
|
|
|
});
|
2015-04-19 03:27:53 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var status = blessed.box({
|
|
|
|
parent: screen,
|
|
|
|
bottom: 0,
|
|
|
|
right: 0,
|
|
|
|
height: 1,
|
|
|
|
width: 'shrink',
|
|
|
|
style: {
|
|
|
|
bg: 'blue'
|
2015-04-19 05:14:51 +00:00
|
|
|
},
|
|
|
|
content: 'Select your piece of ANSI art (`/` to search).'
|
|
|
|
});
|
|
|
|
|
|
|
|
var loader = blessed.loading({
|
|
|
|
parent: screen,
|
|
|
|
top: 'center',
|
|
|
|
left: 'center',
|
|
|
|
height: 5,
|
|
|
|
align: 'center',
|
|
|
|
width: '50%',
|
|
|
|
tags: true,
|
|
|
|
hidden: true,
|
|
|
|
border: 'line'
|
|
|
|
});
|
|
|
|
|
|
|
|
var msg = blessed.message({
|
|
|
|
parent: screen,
|
|
|
|
top: 'center',
|
|
|
|
left: 'center',
|
|
|
|
height: 'shrink',
|
|
|
|
width: '50%',
|
|
|
|
align: 'center',
|
|
|
|
tags: true,
|
|
|
|
hidden: true,
|
|
|
|
border: 'line'
|
|
|
|
});
|
|
|
|
|
|
|
|
var prompt = blessed.prompt({
|
|
|
|
parent: screen,
|
|
|
|
top: 'center',
|
|
|
|
left: 'center',
|
|
|
|
height: 'shrink',
|
|
|
|
width: 'shrink',
|
|
|
|
keys: true,
|
|
|
|
vi: true,
|
|
|
|
mouse: true,
|
|
|
|
tags: true,
|
|
|
|
border: 'line',
|
|
|
|
hidden: true
|
2015-04-19 03:27:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
list.setItems(Object.keys(map));
|
|
|
|
|
2015-04-21 11:19:50 +00:00
|
|
|
list.on('select', function(el, selected) {
|
2015-04-19 05:14:51 +00:00
|
|
|
if (list._.rendering) return;
|
|
|
|
|
2015-04-21 11:19:50 +00:00
|
|
|
var name = el.getText();
|
|
|
|
var url = map[name];
|
|
|
|
|
2015-04-19 03:27:53 +00:00
|
|
|
status.setContent(url);
|
2015-04-19 05:14:51 +00:00
|
|
|
|
|
|
|
list._.rendering = true;
|
|
|
|
loader.load('Loading...');
|
|
|
|
|
2015-04-20 21:09:01 +00:00
|
|
|
request({
|
|
|
|
uri: url,
|
|
|
|
encoding: null
|
|
|
|
}, function(err, res, body) {
|
2015-04-19 05:14:51 +00:00
|
|
|
list._.rendering = false;
|
|
|
|
loader.stop();
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
return msg.error(err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!body) {
|
|
|
|
return msg.error('No body.');
|
|
|
|
}
|
|
|
|
|
2015-04-20 21:09:01 +00:00
|
|
|
return cp437ToUtf8(body, function(err, body) {
|
|
|
|
if (err) {
|
|
|
|
return msg.error(err.message);
|
|
|
|
}
|
|
|
|
|
2015-04-20 22:04:39 +00:00
|
|
|
if (process.argv[2] === '--debug') {
|
2015-04-21 11:19:50 +00:00
|
|
|
var filename = name.replace(/\//g, '.') + '.ans';
|
|
|
|
fs.writeFileSync(__dirname + '/' + filename, body);
|
2015-04-20 22:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove text:
|
|
|
|
body = body.replace('Downloaded From P-80 International Information Systems 304-744-2253', '');
|
|
|
|
|
2015-04-20 21:09:01 +00:00
|
|
|
// Remove MCI codes:
|
2015-04-20 22:04:39 +00:00
|
|
|
body = body.replace(/%[A-Z0-9]{2}/g, '');
|
|
|
|
|
2015-04-21 00:40:17 +00:00
|
|
|
// ^A (SOH) seems to need to produce CRLF in some cases??
|
|
|
|
// body = body.replace(/\x01/g, '\r\n');
|
2015-04-19 05:14:51 +00:00
|
|
|
|
2015-04-20 21:09:01 +00:00
|
|
|
// Reset and write the art:
|
|
|
|
art.term.reset();
|
|
|
|
art.term.write(body);
|
|
|
|
art.term.cursorHidden = true;
|
2015-04-19 05:14:51 +00:00
|
|
|
|
2015-04-20 21:09:01 +00:00
|
|
|
screen.render();
|
2015-04-21 11:19:50 +00:00
|
|
|
|
|
|
|
if (process.argv[2] === '--debug' || process.argv[2] === '--save') {
|
2015-04-21 14:07:11 +00:00
|
|
|
takeScreenshot(name);
|
2015-04-21 11:19:50 +00:00
|
|
|
}
|
2015-04-20 21:09:01 +00:00
|
|
|
});
|
2015-04-19 03:27:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-29 19:11:11 +00:00
|
|
|
list.items.forEach(function(item, i) {
|
|
|
|
var text = item.getText();
|
|
|
|
item.setHover(map[text]);
|
|
|
|
});
|
|
|
|
|
2015-04-19 03:27:53 +00:00
|
|
|
list.focus();
|
2015-04-21 14:07:11 +00:00
|
|
|
list.enterSelected(0);
|
2015-04-19 03:27:53 +00:00
|
|
|
|
|
|
|
screen.key('h', function() {
|
|
|
|
list.toggle();
|
|
|
|
if (list.visible) list.focus();
|
|
|
|
});
|
|
|
|
|
2015-04-21 14:07:11 +00:00
|
|
|
screen.key('r', function() {
|
|
|
|
shuffle();
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.key('S-s', function() {
|
|
|
|
takeScreenshot(list.ritems[list.selected]);
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.key('s', function() {
|
|
|
|
slideshow();
|
|
|
|
});
|
|
|
|
|
|
|
|
screen.key('q', function() {
|
|
|
|
return process.exit(0);
|
|
|
|
});
|
|
|
|
|
2015-04-19 03:27:53 +00:00
|
|
|
screen.render();
|
2015-04-20 21:09:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
|
|
|
// https://github.com/chjj/blessed/issues/127
|
|
|
|
// https://github.com/Mithgol/node-singlebyte
|
|
|
|
|
|
|
|
function cp437ToUtf8(buf, callback) {
|
|
|
|
try {
|
|
|
|
return callback(null, singlebyte.bufToStr(buf, 'cp437'));
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 14:07:11 +00:00
|
|
|
|
|
|
|
// Animating ANSI art doesn't work for screenshots.
|
|
|
|
var ANIMATING = [
|
|
|
|
'bbs/void3',
|
|
|
|
'holiday/xmasfwks',
|
|
|
|
'unsorted/diver',
|
|
|
|
'unsorted/mash-chp',
|
|
|
|
'unsorted/ryans47',
|
|
|
|
'unsorted/xmasfwks'
|
|
|
|
];
|
|
|
|
|
|
|
|
function takeScreenshot(name) {
|
|
|
|
var filename = name.replace(/\//g, '.') + '.ans.sgr';
|
2015-04-24 05:25:09 +00:00
|
|
|
var image;
|
2015-04-21 14:07:11 +00:00
|
|
|
// Animating art hangs terminal during screenshot as of right now.
|
|
|
|
if (~ANIMATING.indexOf(name)) {
|
2015-04-24 05:25:09 +00:00
|
|
|
image = blessed.element.prototype.screenshot.call(art,
|
2015-04-21 14:07:11 +00:00
|
|
|
0 - art.ileft, art.width - art.iright,
|
|
|
|
0 - art.itop, art.height - art.ibottom);
|
|
|
|
} else {
|
2015-04-24 05:25:09 +00:00
|
|
|
image = art.screenshot();
|
2015-04-21 14:07:11 +00:00
|
|
|
}
|
2015-04-24 05:25:09 +00:00
|
|
|
fs.writeFileSync(__dirname + '/' + filename, image);
|
|
|
|
msg.display('Screenshot taken.');
|
2015-04-21 14:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function slideshow() {
|
|
|
|
if (!screen._.slideshow) {
|
|
|
|
screen._.slideshow = setInterval(function slide() {
|
|
|
|
if (screen.lockKeys) return;
|
|
|
|
var i = (list.items.length - 1) * Math.random() | 0;
|
|
|
|
list.enterSelected(i);
|
|
|
|
return slide;
|
|
|
|
}(), 3000);
|
2015-04-24 05:25:09 +00:00
|
|
|
msg.display('Slideshow started.');
|
2015-04-21 14:07:11 +00:00
|
|
|
} else {
|
|
|
|
clearInterval(screen._.slideshow);
|
|
|
|
delete screen._.slideshow;
|
2015-04-24 05:25:09 +00:00
|
|
|
msg.display('Slideshow stopped.');
|
2015-04-21 14:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function shuffle() {
|
|
|
|
var items = Object.keys(map).sort(function(key) {
|
|
|
|
return Math.random() > 0.5 ? 1 : -1;
|
|
|
|
});
|
|
|
|
list.setItems(items);
|
|
|
|
screen.render();
|
2015-04-24 05:25:09 +00:00
|
|
|
msg.display('Shuffled items.');
|
2015-04-21 14:07:11 +00:00
|
|
|
}
|