2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const assert = require('assert');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const _ = require('lodash');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.getArt = getArt;
|
|
|
|
exports.getArtFromPath = getArtFromPath;
|
|
|
|
exports.display = display;
|
|
|
|
exports.defaultEncodingFromExtension = defaultEncodingFromExtension;
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// :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
|
2022-06-05 20:04:25 +00:00
|
|
|
'.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 },
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
'.amiga': { name: 'Amiga', defaultEncoding: 'amiga', eof: 0x1a },
|
|
|
|
'.txt': { name: 'Amiga Text', defaultEncoding: 'cp437', eof: 0x1a },
|
2018-06-23 03:26:46 +00:00
|
|
|
// :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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (sauce.Character) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return sauce.Character.fontName;
|
|
|
|
}
|
2015-04-17 04:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-10-17 02:21:06 +00:00
|
|
|
function sliceAtEOF(data, eofMarker) {
|
2022-06-05 20:04:25 +00:00
|
|
|
let eof = data.length;
|
|
|
|
const stopPos = Math.max(data.length - 256, 0); // 256 = 2 * sizeof(SAUCE)
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
for (let i = eof - 1; i > stopPos; i--) {
|
|
|
|
if (eofMarker === data[i]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
eof = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 02:19:45 +00:00
|
|
|
|
|
|
|
if (eof === data.length) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return data; // nothing to do
|
2020-05-14 02:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Convert from encodedAs -> j
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +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() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.fullFile === true) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return iconv.decode(data, encoding);
|
|
|
|
} else {
|
|
|
|
const eofMarker = defaultEofFromExtension(ext);
|
2022-06-05 20:04:25 +00:00
|
|
|
return iconv.decode(
|
|
|
|
eofMarker ? sliceAtEOF(data, eofMarker) : data,
|
|
|
|
encoding
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResult(sauce) {
|
|
|
|
const result = {
|
2022-06-05 20:04:25 +00:00
|
|
|
data: sliceOfData(),
|
|
|
|
fromPath: path,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (sauce) {
|
2018-06-22 05:15:04 +00:00
|
|
|
result.sauce = sauce;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.readSauce === true) {
|
2018-06-22 05:15:04 +00:00
|
|
|
sauce.readSAUCE(data, (err, sauce) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.encodedAs) {
|
2018-06-22 05:15:04 +00:00
|
|
|
/*
|
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);
|
|
|
|
|
2022-06-05 20:04:25 +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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('' !== ext) {
|
|
|
|
options.types = [ext.toLowerCase()];
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isUndefined(options.types)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.types = Object.keys(SUPPORTED_ART_TYPES);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isString(options.types)) {
|
|
|
|
options.types = [options.types.toLowerCase()];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// If an extension is provided, just read the file now
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('' !== ext) {
|
|
|
|
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) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const filtered = files.filter(file => {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
// Ignore anything not allowed in |options.types|
|
|
|
|
//
|
|
|
|
const fext = paths.extname(file);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.types.includes(fext.toLowerCase())) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bn = paths.basename(file, fext).toLowerCase();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.random) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const suppliedBn = paths.basename(name, fext).toLowerCase();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Random selection enabled. We'll allow for
|
|
|
|
// basename1.ext, basename2.ext, ...
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!bn.startsWith(suppliedBn)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const num = bn.substr(suppliedBn.length);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (num.length > 0) {
|
|
|
|
if (isNaN(parseInt(num, 10))) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// We've already validated the extension (above). Must be an exact
|
|
|
|
// match to basename here
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (bn != paths.basename(name, fext).toLowerCase()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (filtered.length > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
// We should now have:
|
|
|
|
// - Exactly (1) item in |filtered| if non-random
|
|
|
|
// - 1:n items in |filtered| to choose from if random
|
|
|
|
//
|
|
|
|
let readPath;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.random) {
|
|
|
|
readPath = paths.join(
|
|
|
|
options.basePath,
|
|
|
|
filtered[Math.floor(Math.random() * filtered.length)]
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} 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()];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (artType) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isFunction(options) && !cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isBoolean(options.iceColors)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// try to detect from SAUCE
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.has(options, 'sauce.ansiFlags') && options.sauce.ansiFlags & (1 << 0)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.iceColors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ansiParser = new aep.ANSIEscapeParser({
|
2022-06-05 20:04:25 +00:00
|
|
|
mciReplaceChar: options.mciReplaceChar,
|
|
|
|
termHeight: client.term.termHeight,
|
|
|
|
termWidth: client.term.termWidth,
|
|
|
|
trailingLF: options.trailingLF,
|
|
|
|
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|
|
2022-06-05 20:04:25 +00:00
|
|
|
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-06-05 20:04:25 +00:00
|
|
|
if (mapEntry) {
|
|
|
|
mapEntry.focusSGR = mciInfo.SGR;
|
|
|
|
mapEntry.focusArgs = mciInfo.args;
|
2022-04-26 18:28:15 +00:00
|
|
|
} else {
|
|
|
|
mciMap[mapKey] = {
|
2022-06-05 20:04:25 +00:00
|
|
|
position: mciInfo.position,
|
|
|
|
args: mciInfo.args,
|
|
|
|
SGR: mciInfo.SGR,
|
|
|
|
code: mciInfo.mci,
|
|
|
|
id: id,
|
2022-04-26 18:28:15 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!mciInfo.id) {
|
2022-04-26 18:28:15 +00:00
|
|
|
++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-06-05 20:04:25 +00:00
|
|
|
ansiParser.on('literal', literal => client.term.write(literal, false));
|
|
|
|
ansiParser.on('control', control => client.term.rawWrite(control));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
height: ansiParser.row - 1,
|
2022-04-26 18:28:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.font) {
|
2022-04-26 03:09:57 +00:00
|
|
|
initSeq = ansi.setSyncTermFontWithAlias(options.font);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (options.sauce) {
|
2022-04-26 03:09:57 +00:00
|
|
|
let fontName = getFontNameFromSAUCE(options.sauce);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (fontName) {
|
2022-04-26 03:09:57 +00:00
|
|
|
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)
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (fontName && client.term.currentSyncFont != fontName) {
|
2022-04-26 03:09:57 +00:00
|
|
|
client.term.currentSyncFont = fontName;
|
|
|
|
initSeq = ansi.setSyncTermFont(fontName);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.iceColors) {
|
2018-06-22 05:15:04 +00:00
|
|
|
initSeq += ansi.blinkToBrightIntensity();
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (initSeq) {
|
2018-06-22 05:15:04 +00:00
|
|
|
client.term.rawWrite(initSeq);
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiParser.reset(art);
|
|
|
|
return ansiParser.parse();
|
2016-09-01 04:06:28 +00:00
|
|
|
}
|