2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2018-12-03 02:33:07 +00:00
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const miscUtil = require('./misc_util.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const aep = require('./ansi_escape_parser.js');
|
|
|
|
const sauce = require('./sauce.js');
|
|
|
|
const { Errors } = require('./enig_error.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const assert = require('assert');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
exports.getArt = getArt;
|
|
|
|
exports.getArtFromPath = getArtFromPath;
|
|
|
|
exports.display = display;
|
|
|
|
exports.defaultEncodingFromExtension = defaultEncodingFromExtension;
|
|
|
|
|
|
|
|
// :TODO: Return MCI code information
|
|
|
|
// :TODO: process SAUCE comments
|
|
|
|
// :TODO: return font + font mapped information from SAUCE
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2016-09-01 04:25:41 +00:00
|
|
|
const SUPPORTED_ART_TYPES = {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: the defualt encoding are really useless if they are all the same ...
|
|
|
|
// perhaps .ansamiga and .ascamiga could be supported as well as overrides via conf
|
|
|
|
'.ans' : { name : 'ANSI', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
'.asc' : { name : 'ASCII', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
'.pcb' : { name : 'PCBoard', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
'.bbs' : { name : 'Wildcat', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
|
|
|
|
'.amiga' : { name : 'Amiga', defaultEncoding : 'amiga', eof : 0x1a },
|
|
|
|
'.txt' : { name : 'Amiga Text', defaultEncoding : 'cp437', eof : 0x1a },
|
|
|
|
// :TODO: extentions for wwiv, renegade, celerity, syncronet, ...
|
|
|
|
// :TODO: extension for atari
|
|
|
|
// :TODO: extension for topaz ansi/ascii.
|
2014-10-17 02:21:06 +00:00
|
|
|
};
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
function getFontNameFromSAUCE(sauce) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(sauce.Character) {
|
|
|
|
return sauce.Character.fontName;
|
|
|
|
}
|
2015-04-17 04:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function sliceAtEOF(data, eofMarker) {
|
2018-06-23 03:26:46 +00:00
|
|
|
let eof = data.length;
|
2020-05-14 02:19:45 +00:00
|
|
|
const stopPos = Math.max(data.length - 256, 0); // 256 = 2 * sizeof(SAUCE)
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
for(let i = eof - 1; i > stopPos; i--) {
|
|
|
|
if(eofMarker === data[i]) {
|
|
|
|
eof = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 02:19:45 +00:00
|
|
|
|
|
|
|
if (eof === data.length) {
|
|
|
|
return data; // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to prevent goofs
|
|
|
|
if (eof < 128 && 'SAUCE00' !== data.slice(eof + 1, eof + 8).toString()) {
|
2019-05-14 03:31:34 +00:00
|
|
|
return data;
|
|
|
|
}
|
2020-05-14 02:19:45 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return data.slice(0, eof);
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getArtFromPath(path, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.readFile(path, (err, data) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Convert from encodedAs -> j
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
const ext = paths.extname(path).toLowerCase();
|
|
|
|
const encoding = options.encodedAs || defaultEncodingFromExtension(ext);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: how are BOM's currently handled if present? Are they removed? Do we need to?
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
function sliceOfData() {
|
|
|
|
if(options.fullFile === true) {
|
|
|
|
return iconv.decode(data, encoding);
|
|
|
|
} else {
|
|
|
|
const eofMarker = defaultEofFromExtension(ext);
|
|
|
|
return iconv.decode(eofMarker ? sliceAtEOF(data, eofMarker) : data, encoding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResult(sauce) {
|
|
|
|
const result = {
|
2018-06-23 03:26:46 +00:00
|
|
|
data : sliceOfData(),
|
|
|
|
fromPath : path,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(sauce) {
|
|
|
|
result.sauce = sauce;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.readSauce === true) {
|
|
|
|
sauce.readSAUCE(data, (err, sauce) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(null, getResult());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If a encoding was not provided & we have a mapping from
|
|
|
|
// the information provided by SAUCE, use that.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(!options.encodedAs) {
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
if(sauce.Character && sauce.Character.fontName) {
|
|
|
|
var enc = SAUCE_FONT_TO_ENCODING_HINT[sauce.Character.fontName];
|
|
|
|
if(enc) {
|
|
|
|
encoding = enc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(null, getResult(sauce));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null, getResult());
|
|
|
|
}
|
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getArt(name, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const ext = paths.extname(name);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
options.basePath = miscUtil.valueWithDefault(options.basePath, Config().paths.art);
|
|
|
|
options.asAnsi = miscUtil.valueWithDefault(options.asAnsi, true);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: make use of asAnsi option and convert from supported -> ansi
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if('' !== ext) {
|
|
|
|
options.types = [ ext.toLowerCase() ];
|
|
|
|
} else {
|
|
|
|
if(_.isUndefined(options.types)) {
|
|
|
|
options.types = Object.keys(SUPPORTED_ART_TYPES);
|
|
|
|
} else if(_.isString(options.types)) {
|
|
|
|
options.types = [ options.types.toLowerCase() ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// If an extension is provided, just read the file now
|
2018-06-22 05:15:04 +00:00
|
|
|
if('' !== ext) {
|
2019-05-14 03:27:59 +00:00
|
|
|
const directPath = paths.isAbsolute(name) ? name : paths.join(options.basePath, name);
|
2018-06-22 05:15:04 +00:00
|
|
|
return getArtFromPath(directPath, options, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readdir(options.basePath, (err, files) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const filtered = files.filter( file => {
|
|
|
|
//
|
|
|
|
// Ignore anything not allowed in |options.types|
|
|
|
|
//
|
|
|
|
const fext = paths.extname(file);
|
|
|
|
if(!options.types.includes(fext.toLowerCase())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bn = paths.basename(file, fext).toLowerCase();
|
|
|
|
if(options.random) {
|
|
|
|
const suppliedBn = paths.basename(name, fext).toLowerCase();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Random selection enabled. We'll allow for
|
|
|
|
// basename1.ext, basename2.ext, ...
|
|
|
|
//
|
|
|
|
if(!bn.startsWith(suppliedBn)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const num = bn.substr(suppliedBn.length);
|
|
|
|
if(num.length > 0) {
|
|
|
|
if(isNaN(parseInt(num, 10))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// We've already validated the extension (above). Must be an exact
|
|
|
|
// match to basename here
|
|
|
|
//
|
|
|
|
if(bn != paths.basename(name, fext).toLowerCase()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(filtered.length > 0) {
|
|
|
|
//
|
|
|
|
// We should now have:
|
|
|
|
// - Exactly (1) item in |filtered| if non-random
|
|
|
|
// - 1:n items in |filtered| to choose from if random
|
|
|
|
//
|
|
|
|
let readPath;
|
|
|
|
if(options.random) {
|
|
|
|
readPath = paths.join(options.basePath, filtered[Math.floor(Math.random() * filtered.length)]);
|
|
|
|
} else {
|
|
|
|
assert(1 === filtered.length);
|
|
|
|
readPath = paths.join(options.basePath, filtered[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getArtFromPath(readPath, options, cb);
|
|
|
|
}
|
|
|
|
|
2018-12-03 02:33:07 +00:00
|
|
|
return cb(Errors.DoesNotExist(`No matching art for supplied criteria: ${name}`));
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function defaultEncodingFromExtension(ext) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const artType = SUPPORTED_ART_TYPES[ext.toLowerCase()];
|
|
|
|
return artType ? artType.defaultEncoding : 'utf8';
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function defaultEofFromExtension(ext) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const artType = SUPPORTED_ART_TYPES[ext.toLowerCase()];
|
|
|
|
if(artType) {
|
|
|
|
return artType.eof;
|
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Implement the following
|
|
|
|
// * Pause (disabled | termHeight | keyPress )
|
|
|
|
// * Cancel (disabled | <keys> )
|
|
|
|
// * Resume from pause -> continous (disabled | <keys>)
|
2016-09-01 04:06:28 +00:00
|
|
|
function display(client, art, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(_.isFunction(options) && !cb) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!art || !art.length) {
|
2018-12-03 02:33:07 +00:00
|
|
|
return cb(Errors.Invalid('No art supplied!'));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
options.mciReplaceChar = options.mciReplaceChar || ' ';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: this is going to be broken into two approaches controlled via options:
|
|
|
|
// 1) Standard - use internal tracking of locations for MCI -- no CPR's/etc.
|
|
|
|
// 2) CPR driven
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(!_.isBoolean(options.iceColors)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// try to detect from SAUCE
|
2018-06-22 05:15:04 +00:00
|
|
|
if(_.has(options, 'sauce.ansiFlags') && (options.sauce.ansiFlags & (1 << 0))) {
|
|
|
|
options.iceColors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ansiParser = new aep.ANSIEscapeParser({
|
2018-06-23 03:26:46 +00:00
|
|
|
mciReplaceChar : options.mciReplaceChar,
|
|
|
|
termHeight : client.term.termHeight,
|
|
|
|
termWidth : client.term.termWidth,
|
|
|
|
trailingLF : options.trailingLF,
|
2022-03-26 00:27:01 +00:00
|
|
|
startRow : options.startRow,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
const mciMap = {};
|
|
|
|
let generatedId = 100;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
ansiParser.on('mci', mciInfo => {
|
|
|
|
// :TODO: ensure generatedId's do not conflict with any existing |id|
|
|
|
|
const id = _.isNumber(mciInfo.id) ? mciInfo.id : generatedId;
|
|
|
|
const mapKey = `${mciInfo.mci}${id}`;
|
|
|
|
const mapEntry = mciMap[mapKey];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
if(mapEntry) {
|
|
|
|
mapEntry.focusSGR = mciInfo.SGR;
|
|
|
|
mapEntry.focusArgs = mciInfo.args;
|
|
|
|
} else {
|
|
|
|
mciMap[mapKey] = {
|
|
|
|
position : mciInfo.position,
|
|
|
|
args : mciInfo.args,
|
|
|
|
SGR : mciInfo.SGR,
|
|
|
|
code : mciInfo.mci,
|
|
|
|
id : id,
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
if(!mciInfo.id) {
|
|
|
|
++generatedId;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-04-26 18:28:15 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
ansiParser.on('literal', literal => client.term.write(literal, false) );
|
|
|
|
ansiParser.on('control', control => client.term.rawWrite(control) );
|
|
|
|
|
|
|
|
ansiParser.on('complete', () => {
|
2022-04-26 18:28:15 +00:00
|
|
|
ansiParser.removeAllListeners();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 18:28:15 +00:00
|
|
|
const extraInfo = {
|
|
|
|
height : ansiParser.row - 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
return cb(null, mciMap, extraInfo);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let initSeq = '';
|
2022-04-26 03:09:57 +00:00
|
|
|
if (client.term.syncTermFontsEnabled) {
|
|
|
|
if(options.font) {
|
|
|
|
initSeq = ansi.setSyncTermFontWithAlias(options.font);
|
|
|
|
} else if(options.sauce) {
|
|
|
|
let fontName = getFontNameFromSAUCE(options.sauce);
|
|
|
|
if(fontName) {
|
|
|
|
fontName = ansi.getSyncTermFontFromAlias(fontName);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-04-26 03:09:57 +00:00
|
|
|
//
|
|
|
|
// Set SyncTERM font if we're switching only. Most terminals
|
|
|
|
// that support this ESC sequence can only show *one* font
|
|
|
|
// at a time. This applies to detection only (e.g. SAUCE).
|
|
|
|
// If explicit, we'll set it no matter what (above)
|
|
|
|
//
|
|
|
|
if(fontName && client.term.currentSyncFont != fontName) {
|
|
|
|
client.term.currentSyncFont = fontName;
|
|
|
|
initSeq = ansi.setSyncTermFont(fontName);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.iceColors) {
|
|
|
|
initSeq += ansi.blinkToBrightIntensity();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(initSeq) {
|
|
|
|
client.term.rawWrite(initSeq);
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiParser.reset(art);
|
|
|
|
return ansiParser.parse();
|
2016-09-01 04:06:28 +00:00
|
|
|
}
|