2014-10-27 04:06:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const art = require('./art.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const ConfigCache = require('./config_cache.js');
|
|
|
|
const getFullConfig = require('./config_util.js').getFullConfig;
|
|
|
|
const asset = require('./asset.js');
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const ErrorReasons = require('./enig_error.js').ErrorReasons;
|
|
|
|
const Events = require('./events.js');
|
2018-07-05 00:43:40 +00:00
|
|
|
const AnsiPrep = require('./ansi_prep.js');
|
2018-11-24 00:41:16 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2018-11-24 00:41:16 +00:00
|
|
|
// deps
|
2018-06-23 03:26:46 +00:00
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const paths = require('path');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
exports.getThemeArt = getThemeArt;
|
|
|
|
exports.getAvailableThemes = getAvailableThemes;
|
|
|
|
exports.getRandomTheme = getRandomTheme;
|
2016-01-15 05:44:33 +00:00
|
|
|
exports.setClientTheme = setClientTheme;
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.initAvailableThemes = initAvailableThemes;
|
2019-05-25 04:26:27 +00:00
|
|
|
exports.displayPreparedArt = displayPreparedArt;
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.displayThemeArt = displayThemeArt;
|
|
|
|
exports.displayThemedPause = displayThemedPause;
|
|
|
|
exports.displayThemedPrompt = displayThemedPrompt;
|
|
|
|
exports.displayThemedAsset = displayThemedAsset;
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
function refreshThemeHelpers(theme) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Create some handy helpers
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
theme.helpers = {
|
|
|
|
getPasswordChar : function() {
|
|
|
|
let pwChar = _.get(
|
|
|
|
theme,
|
2018-11-16 03:32:08 +00:00
|
|
|
'customization.defaults.passwordChar',
|
2018-11-08 01:33:07 +00:00
|
|
|
Config().theme.passwordChar
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if(_.isString(pwChar)) {
|
|
|
|
pwChar = pwChar.substr(0, 1);
|
|
|
|
} else if(_.isNumber(pwChar)) {
|
|
|
|
pwChar = String.fromCharCode(pwChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pwChar;
|
|
|
|
},
|
|
|
|
getDateFormat : function(style = 'short') {
|
2018-11-08 01:33:07 +00:00
|
|
|
const format = Config().theme.dateFormat[style] || 'MM/DD/YYYY';
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.get(theme, `customization.defaults.dateFormat.${style}`, format);
|
|
|
|
},
|
|
|
|
getTimeFormat : function(style = 'short') {
|
2018-11-08 01:33:07 +00:00
|
|
|
const format = Config().theme.timeFormat[style] || 'h:mm a';
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.get(theme, `customization.defaults.timeFormat.${style}`, format);
|
|
|
|
},
|
|
|
|
getDateTimeFormat : function(style = 'short') {
|
2018-11-08 01:33:07 +00:00
|
|
|
const format = Config().theme.dateTimeFormat[style] || 'MM/DD/YYYY h:mm a';
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.get(theme, `customization.defaults.dateTimeFormat.${style}`, format);
|
|
|
|
}
|
|
|
|
};
|
2015-09-10 03:31:04 +00:00
|
|
|
}
|
2015-09-02 04:42:54 +00:00
|
|
|
|
2018-06-14 03:02:00 +00:00
|
|
|
function loadTheme(themeId, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const path = paths.join(Config().paths.themes, themeId, 'theme.hjson');
|
|
|
|
|
|
|
|
const changed = ( { fileName, fileRoot } ) => {
|
|
|
|
const reCachedPath = paths.join(fileRoot, fileName);
|
|
|
|
if(reCachedPath === path) {
|
|
|
|
reloadTheme(themeId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
filePath : path,
|
|
|
|
forceReCache : true,
|
|
|
|
callback : changed,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ConfigCache.getConfigWithOptions(getOpts, (err, theme) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isObject(theme.info) ||
|
2018-06-23 03:26:46 +00:00
|
|
|
!_.isString(theme.info.name) ||
|
|
|
|
!_.isString(theme.info.author))
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
return cb(Errors.Invalid('Invalid or missing "info" section'));
|
|
|
|
}
|
2015-09-10 03:31:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(false === _.get(theme, 'info.enabled')) {
|
2019-01-05 23:55:37 +00:00
|
|
|
return cb(Errors.General('Theme is not enabled', ErrorReasons.ErrNotEnabled));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-09-04 15:57:10 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
refreshThemeHelpers(theme);
|
2017-09-04 15:57:10 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null, theme, path);
|
|
|
|
});
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 03:02:00 +00:00
|
|
|
const availableThemes = new Map();
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2017-01-28 19:33:06 +00:00
|
|
|
const IMMUTABLE_MCI_PROPERTIES = [
|
2018-06-22 05:15:04 +00:00
|
|
|
'maxLength', 'argName', 'submit', 'validate'
|
2016-01-15 05:44:33 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
function getMergedTheme(menuConfig, promptConfig, theme) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(menuConfig));
|
|
|
|
assert(_.isObject(theme));
|
|
|
|
|
|
|
|
// :TODO: merge in defaults (customization.defaults{} )
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: apply generic stuff, e.g. "VM" (vs "VM1")
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Create a *clone* of menuConfig (menu.hjson) then bring in
|
|
|
|
// promptConfig (prompt.hjson)
|
|
|
|
//
|
|
|
|
const mergedTheme = _.cloneDeep(menuConfig);
|
|
|
|
|
|
|
|
if(_.isObject(promptConfig.prompts)) {
|
|
|
|
mergedTheme.prompts = _.cloneDeep(promptConfig.prompts);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Add in data we won't be altering directly from the theme
|
|
|
|
//
|
2019-01-05 23:55:37 +00:00
|
|
|
mergedTheme.info = theme.info;
|
|
|
|
mergedTheme.helpers = theme.helpers;
|
|
|
|
mergedTheme.achievements = _.get(theme, 'customization.achievements');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// merge customizer to disallow immutable MCI properties
|
|
|
|
//
|
|
|
|
const mciCustomizer = function(objVal, srcVal, key) {
|
|
|
|
return IMMUTABLE_MCI_PROPERTIES.indexOf(key) > -1 ? objVal : srcVal;
|
|
|
|
};
|
|
|
|
|
|
|
|
function getFormKeys(fromObj) {
|
2018-06-26 00:09:32 +00:00
|
|
|
// remove all non-numbers
|
|
|
|
return _.remove(_.keys(fromObj), k => !isNaN(k));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mergeMciProperties(dest, src) {
|
2018-06-26 00:09:32 +00:00
|
|
|
Object.keys(src).forEach(mci => {
|
|
|
|
if(dest[mci]) {
|
|
|
|
_.mergeWith(dest[mci], src[mci], mciCustomizer);
|
|
|
|
} else {
|
|
|
|
// theme contains MCI not in menu; bring in as-is
|
|
|
|
dest[mci] = src[mci];
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyThemeMciBlock(dest, src, formKey) {
|
|
|
|
if(_.isObject(src.mci)) {
|
|
|
|
mergeMciProperties(dest, src.mci);
|
|
|
|
} else {
|
|
|
|
if(_.has(src, [ formKey, 'mci' ])) {
|
|
|
|
mergeMciProperties(dest, src[formKey].mci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// menu.hjson can have a couple different structures:
|
|
|
|
// 1) Explicit declaration of expected MCI code(s) under 'form:<id>' before a 'mci' block
|
|
|
|
// (this allows multiple layout types defined by one menu for example)
|
|
|
|
//
|
|
|
|
// 2) Non-explicit declaration: 'mci' directly under 'form:<id>'
|
|
|
|
//
|
|
|
|
// theme.hjson has it's own mix:
|
|
|
|
// 1) Explicit: Form ID before 'mci' (generally used where there are > 1 forms)
|
|
|
|
//
|
|
|
|
// 2) Non-explicit: 'mci' directly under an entry
|
|
|
|
//
|
|
|
|
// Additionally, #1 or #2 may be under an explicit key of MCI code(s) to match up
|
|
|
|
// with menu.hjson in #1.
|
|
|
|
//
|
|
|
|
// * When theming an explicit menu.hjson entry (1), we will use a matching explicit
|
|
|
|
// entry with a matching MCI code(s) key in theme.hjson (e.g. menu="ETVM"/theme="ETVM"
|
|
|
|
// and fall back to generic if a match is not found.
|
|
|
|
//
|
|
|
|
// * If theme.hjson provides form ID's, use them. Otherwise, we'll apply directly assuming
|
|
|
|
// there is a generic 'mci' block.
|
|
|
|
//
|
|
|
|
function applyToForm(form, menuTheme, formKey) {
|
|
|
|
if(_.isObject(form.mci)) {
|
|
|
|
// non-explicit: no MCI code(s) key assumed since we found 'mci' directly under form ID
|
|
|
|
applyThemeMciBlock(form.mci, menuTheme, formKey);
|
|
|
|
} else {
|
2018-06-26 00:09:32 +00:00
|
|
|
// remove anything not uppercase
|
|
|
|
const menuMciCodeKeys = _.remove(_.keys(form), k => k === k.toUpperCase());
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
menuMciCodeKeys.forEach(function mciKeyEntry(mciKey) {
|
|
|
|
let applyFrom;
|
|
|
|
if(_.has(menuTheme, [ mciKey, 'mci' ])) {
|
|
|
|
applyFrom = menuTheme[mciKey];
|
|
|
|
} else {
|
|
|
|
applyFrom = menuTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
applyThemeMciBlock(form[mciKey].mci, applyFrom, formKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[ 'menus', 'prompts' ].forEach(function areaEntry(sectionName) {
|
|
|
|
_.keys(mergedTheme[sectionName]).forEach(function menuEntry(menuName) {
|
|
|
|
let createdFormSection = false;
|
|
|
|
const mergedThemeMenu = mergedTheme[sectionName][menuName];
|
|
|
|
|
|
|
|
if(_.has(theme, [ 'customization', sectionName, menuName ])) {
|
|
|
|
const menuTheme = theme.customization[sectionName][menuName];
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// config block is direct assign/overwrite
|
2018-06-22 05:15:04 +00:00
|
|
|
// :TODO: should probably be _.merge()
|
|
|
|
if(menuTheme.config) {
|
|
|
|
mergedThemeMenu.config = _.assign(mergedThemeMenu.config || {}, menuTheme.config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if('menus' === sectionName) {
|
|
|
|
if(_.isObject(mergedThemeMenu.form)) {
|
|
|
|
getFormKeys(mergedThemeMenu.form).forEach(function formKeyEntry(formKey) {
|
|
|
|
applyToForm(mergedThemeMenu.form[formKey], menuTheme, formKey);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if(_.isObject(menuTheme.mci)) {
|
|
|
|
//
|
|
|
|
// Not specified at menu level means we apply anything from the
|
|
|
|
// theme to form.0.mci{}
|
|
|
|
//
|
|
|
|
mergedThemeMenu.form = { 0 : { mci : { } } };
|
|
|
|
mergeMciProperties(mergedThemeMenu.form[0], menuTheme);
|
|
|
|
createdFormSection = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if('prompts' === sectionName) {
|
|
|
|
// no 'form' or form keys for prompts -- direct to mci
|
|
|
|
applyToForm(mergedThemeMenu, menuTheme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Finished merging for this menu/prompt
|
|
|
|
//
|
|
|
|
// If the following conditions are true, set runtime.autoNext to true:
|
|
|
|
// * This is a menu
|
|
|
|
// * There is/was no explicit 'form' section
|
|
|
|
// * There is no 'prompt' specified
|
|
|
|
//
|
|
|
|
if('menus' === sectionName && !_.isString(mergedThemeMenu.prompt) &&
|
2018-06-23 03:26:46 +00:00
|
|
|
(createdFormSection || !_.isObject(mergedThemeMenu.form)))
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
mergedThemeMenu.runtime = _.merge(mergedThemeMenu.runtime || {}, { autoNext : true } );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-01-15 05:44:33 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return mergedTheme;
|
2016-01-15 05:44:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 03:02:00 +00:00
|
|
|
function reloadTheme(themeId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function loadMenuConfig(callback) {
|
|
|
|
getFullConfig(config.general.menuFile, (err, menuConfig) => {
|
|
|
|
return callback(err, menuConfig);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadPromptConfig(menuConfig, callback) {
|
|
|
|
getFullConfig(config.general.promptFile, (err, promptConfig) => {
|
|
|
|
return callback(err, menuConfig, promptConfig);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadIt(menuConfig, promptConfig, callback) {
|
|
|
|
loadTheme(themeId, (err, theme) => {
|
|
|
|
if(err) {
|
|
|
|
if(ErrorReasons.NotEnabled !== err.reasonCode) {
|
|
|
|
Log.warn( { themeId : themeId, err : err.message }, 'Failed loading theme');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(theme.info, { themeId } );
|
|
|
|
availableThemes.set(themeId, getMergedTheme(menuConfig, promptConfig, theme));
|
|
|
|
|
|
|
|
Events.emit(
|
|
|
|
Events.getSystemEvents().ThemeChanged,
|
|
|
|
{ themeId }
|
|
|
|
);
|
|
|
|
|
|
|
|
return callback(null, theme);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, theme) => {
|
|
|
|
if(err) {
|
|
|
|
Log.warn( { themeId, error : err.message }, 'Failed to reload theme');
|
|
|
|
} else {
|
|
|
|
Log.debug( { info : theme.info }, 'Theme recached' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2018-06-14 03:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function reloadAllThemes()
|
|
|
|
{
|
2018-06-22 05:15:04 +00:00
|
|
|
async.each([ ...availableThemes.keys() ], themeId => reloadTheme(themeId));
|
2018-06-14 03:02:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initAvailableThemes(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function loadMenuConfig(callback) {
|
|
|
|
getFullConfig(config.general.menuFile, (err, menuConfig) => {
|
|
|
|
return callback(err, menuConfig);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function loadPromptConfig(menuConfig, callback) {
|
|
|
|
getFullConfig(config.general.promptFile, (err, promptConfig) => {
|
|
|
|
return callback(err, menuConfig, promptConfig);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getThemeDirectories(menuConfig, promptConfig, callback) {
|
|
|
|
fs.readdir(config.paths.themes, (err, files) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
menuConfig,
|
|
|
|
promptConfig,
|
|
|
|
files.filter( f => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// sync normally not allowed -- initAvailableThemes() is a startup-only method, however
|
2018-06-22 05:15:04 +00:00
|
|
|
return fs.statSync(paths.join(config.paths.themes, f)).isDirectory();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function populateAvailable(menuConfig, promptConfig, themeDirectories, callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
async.each(themeDirectories, (themeId, nextThemeDir) => { // theme dir = theme ID
|
2018-06-22 05:15:04 +00:00
|
|
|
loadTheme(themeId, (err, theme) => {
|
|
|
|
if(err) {
|
|
|
|
if(ErrorReasons.NotEnabled !== err.reasonCode) {
|
|
|
|
Log.warn( { themeId : themeId, err : err.message }, 'Failed loading theme');
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
return nextThemeDir(null); // try next
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.assign(theme.info, { themeId } );
|
|
|
|
availableThemes.set(themeId, getMergedTheme(menuConfig, promptConfig, theme));
|
|
|
|
return nextThemeDir(null);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function initEvents(callback) {
|
|
|
|
Events.on(Events.getSystemEvents().MenusChanged, () => {
|
|
|
|
return reloadAllThemes();
|
|
|
|
});
|
|
|
|
Events.on(Events.getSystemEvents().PromptsChanged, () => {
|
|
|
|
return reloadAllThemes();
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err, availableThemes.size);
|
|
|
|
}
|
|
|
|
);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 18:56:04 +00:00
|
|
|
function getAvailableThemes() {
|
2018-06-22 05:15:04 +00:00
|
|
|
return availableThemes;
|
2015-12-24 18:56:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
function getRandomTheme() {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(availableThemes.size > 0) {
|
|
|
|
const themeIds = [ ...availableThemes.keys() ];
|
|
|
|
return themeIds[Math.floor(Math.random() * themeIds.length)];
|
|
|
|
}
|
2015-04-21 05:24:15 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 05:44:33 +00:00
|
|
|
function setClientTheme(client, themeId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const availThemes = getAvailableThemes();
|
|
|
|
|
|
|
|
let msg;
|
|
|
|
let setThemeId;
|
|
|
|
const config = Config();
|
|
|
|
if(availThemes.has(themeId)) {
|
|
|
|
msg = 'Set client theme';
|
|
|
|
setThemeId = themeId;
|
2018-11-08 01:33:07 +00:00
|
|
|
} else if(availThemes.has(config.theme.default)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg = 'Failed setting theme by supplied ID; Using default';
|
2018-11-08 01:33:07 +00:00
|
|
|
setThemeId = config.theme.default;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
msg = 'Failed setting theme by system default ID; Using the first one we can find';
|
|
|
|
setThemeId = availThemes.keys().next().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.currentTheme = availThemes.get(setThemeId);
|
|
|
|
client.log.debug( { setThemeId, requestedThemeId : themeId, info : client.currentTheme.info }, msg);
|
2016-01-15 05:44:33 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 04:05:40 +00:00
|
|
|
function getThemeArt(options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// options - required:
|
|
|
|
// name
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// options - optional
|
|
|
|
// client - needed for user's theme/etc.
|
|
|
|
// themeId
|
|
|
|
// asAnsi
|
|
|
|
// readSauce
|
|
|
|
// random
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const config = Config();
|
2018-11-24 00:41:16 +00:00
|
|
|
if(!options.themeId && _.has(options, [ 'client', 'user', 'properties', UserProps.ThemeId ])) {
|
|
|
|
options.themeId = options.client.user.properties[UserProps.ThemeId];
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-11-08 01:33:07 +00:00
|
|
|
options.themeId = config.theme.default;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: replace asAnsi stuff with something like retrieveAs = 'ansi' | 'pipe' | ...
|
|
|
|
// :TODO: Some of these options should only be set if not provided!
|
|
|
|
options.asAnsi = true; // always convert to ANSI
|
2019-05-14 03:27:59 +00:00
|
|
|
options.readSauce = _.get(options, 'readSauce', true); // read SAUCE, if avail
|
2018-06-23 03:26:46 +00:00
|
|
|
options.random = _.get(options, 'random', true); // FILENAME<n>.EXT support
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We look for themed art in the following order:
|
|
|
|
// 1) Direct/relative path
|
|
|
|
// 2) Via theme supplied by |themeId|
|
|
|
|
// 3) Via default theme
|
|
|
|
// 4) General art directory
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function fromPath(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We allow relative (to enigma-bbs) or full paths
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if('/' === options.name.charAt(0)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// just take the path as-is
|
2018-06-22 05:15:04 +00:00
|
|
|
options.basePath = paths.dirname(options.name);
|
|
|
|
} else if(options.name.indexOf('/') > -1) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// make relative to base BBS dir
|
2018-06-22 05:15:04 +00:00
|
|
|
options.basePath = paths.join(__dirname, '../', paths.dirname(options.name));
|
|
|
|
} else {
|
|
|
|
return callback(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
art.getArt(options.name, options, (err, artInfo) => {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fromSuppliedTheme(artInfo, callback) {
|
|
|
|
if(artInfo) {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.basePath = paths.join(config.paths.themes, options.themeId);
|
|
|
|
art.getArt(options.name, options, (err, artInfo) => {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fromDefaultTheme(artInfo, callback) {
|
2018-11-08 01:33:07 +00:00
|
|
|
if(artInfo || config.theme.default === options.themeId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
|
2018-11-08 01:33:07 +00:00
|
|
|
options.basePath = paths.join(config.paths.themes, config.theme.default);
|
2018-06-22 05:15:04 +00:00
|
|
|
art.getArt(options.name, options, (err, artInfo) => {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fromGeneralArtDir(artInfo, callback) {
|
|
|
|
if(artInfo) {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.basePath = config.paths.art;
|
|
|
|
art.getArt(options.name, options, (err, artInfo) => {
|
|
|
|
return callback(err, artInfo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, artInfo) {
|
|
|
|
if(err) {
|
|
|
|
const logger = _.get(options, 'client.log') || Log;
|
|
|
|
logger.debug( { reason : err.message }, 'Cannot find theme art');
|
|
|
|
}
|
|
|
|
return cb(err, artInfo);
|
|
|
|
}
|
|
|
|
);
|
2014-11-05 06:50:42 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 04:26:27 +00:00
|
|
|
function displayPreparedArt(options, artInfo, cb) {
|
|
|
|
const displayOpts = {
|
|
|
|
sauce : artInfo.sauce,
|
|
|
|
font : options.font,
|
|
|
|
trailingLF : options.trailingLF,
|
|
|
|
};
|
|
|
|
art.display(options.client, artInfo.data, displayOpts, (err, mciMap, extraInfo) => {
|
|
|
|
return cb(err, { mciMap : mciMap, artInfo : artInfo, extraInfo : extraInfo } );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
function displayThemeArt(options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
assert(_.isString(options.name));
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getArt(callback) {
|
|
|
|
return getThemeArt(options, callback);
|
|
|
|
},
|
|
|
|
function prepWork(artInfo, callback) {
|
|
|
|
if(_.isObject(options.ansiPrepOptions)) {
|
|
|
|
AnsiPrep(
|
|
|
|
artInfo.data,
|
|
|
|
options.ansiPrepOptions,
|
|
|
|
(err, prepped) => {
|
|
|
|
if(!err && prepped) {
|
|
|
|
artInfo.data = prepped;
|
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function disp(artInfo, callback) {
|
2019-05-25 04:26:27 +00:00
|
|
|
return displayPreparedArt(options, artInfo, callback);
|
2018-07-05 00:43:40 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, artData) => {
|
|
|
|
return cb(err, artData);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-07-05 00:43:40 +00:00
|
|
|
);
|
2015-07-06 01:05:55 +00:00
|
|
|
}
|
2015-07-24 04:23:44 +00:00
|
|
|
|
2017-02-08 03:20:10 +00:00
|
|
|
function displayThemedPrompt(name, client, options, cb) {
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
const usingTempViewController = _.isUndefined(options.viewController);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function display(callback) {
|
|
|
|
const promptConfig = client.currentTheme.prompts[name];
|
|
|
|
if(!promptConfig) {
|
|
|
|
return callback(Errors.DoesNotExist(`Missing "${name}" prompt configuration!`));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.clearScreen) {
|
|
|
|
client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If we did *not* clear the screen, don't let the font change
|
|
|
|
// doing so messes things up -- most terminals that support font
|
|
|
|
// changing can only display a single font at at time.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-08-05 20:06:30 +00:00
|
|
|
const dispOptions = Object.assign( {}, options, promptConfig.config );
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: We can use term detection to do nifty things like avoid this kind of kludge:
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!options.clearScreen) {
|
2018-06-23 03:26:46 +00:00
|
|
|
dispOptions.font = 'not_really_a_font!'; // kludge :)
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayThemedAsset(
|
|
|
|
promptConfig.art,
|
|
|
|
client,
|
|
|
|
dispOptions,
|
|
|
|
(err, artInfo) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, promptConfig, artInfo);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function discoverCursorPosition(promptConfig, artInfo, callback) {
|
|
|
|
if(!options.clearPrompt) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// no need to query cursor - we're not gonna use it
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, promptConfig, artInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
client.once('cursor position report', pos => {
|
|
|
|
artInfo.startRow = pos[0] - artInfo.height;
|
|
|
|
return callback(null, promptConfig, artInfo);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.term.rawWrite(ansi.queryPos());
|
|
|
|
},
|
|
|
|
function createMCIViews(promptConfig, artInfo, callback) {
|
2018-07-05 00:43:40 +00:00
|
|
|
const assocViewController = usingTempViewController ? new ViewController( { client : client } ) : options.viewController;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const loadOpts = {
|
2018-07-05 00:43:40 +00:00
|
|
|
promptName : name,
|
|
|
|
mciMap : artInfo.mciMap,
|
|
|
|
config : promptConfig,
|
|
|
|
submitNotify : options.submitNotify,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
assocViewController.loadFromPromptConfig(loadOpts, () => {
|
|
|
|
return callback(null, artInfo, assocViewController);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
2018-07-05 00:43:40 +00:00
|
|
|
function pauseForUserInput(artInfo, assocViewController, callback) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!options.pause) {
|
2018-07-05 00:43:40 +00:00
|
|
|
return callback(null, artInfo, assocViewController);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client.waitForKeyPress( () => {
|
2018-07-05 00:43:40 +00:00
|
|
|
return callback(null, artInfo, assocViewController);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
2018-07-05 00:43:40 +00:00
|
|
|
function clearPauseArt(artInfo, assocViewController, callback) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(options.clearPrompt) {
|
|
|
|
if(artInfo.startRow && artInfo.height) {
|
|
|
|
client.term.rawWrite(ansi.goto(artInfo.startRow, 1));
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Note: Does not work properly in NetRunner < 2.0b17:
|
2018-06-22 05:15:04 +00:00
|
|
|
client.term.rawWrite(ansi.deleteLine(artInfo.height));
|
|
|
|
} else {
|
|
|
|
client.term.rawWrite(ansi.eraseLine(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
return callback(null, assocViewController, artInfo);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
2018-07-05 00:43:40 +00:00
|
|
|
(err, assocViewController, artInfo) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
|
|
|
client.log.warn( { error : err.message }, `Failed displaying "${name}" prompt` );
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
if(assocViewController && usingTempViewController) {
|
|
|
|
assocViewController.detachClientEvents();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
return cb(null, artInfo);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
2015-07-24 04:23:44 +00:00
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
|
2017-02-08 03:20:10 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Pause prompts are a special prompt by the name 'pause'.
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2017-02-08 03:20:10 +00:00
|
|
|
function displayThemedPause(client, options, cb) {
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!cb && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isBoolean(options.clearPrompt)) {
|
|
|
|
options.clearPrompt = true;
|
|
|
|
}
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const promptOptions = Object.assign( {}, options, { pause : true } );
|
|
|
|
return displayThemedPrompt('pause', client, promptOptions, cb);
|
2017-02-08 03:20:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 22:10:12 +00:00
|
|
|
function displayThemedAsset(assetSpec, client, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(client));
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// options are... optional
|
2018-06-22 05:15:04 +00:00
|
|
|
if(3 === arguments.length) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2018-11-18 01:56:09 +00:00
|
|
|
if(Array.isArray(assetSpec)) {
|
|
|
|
const acsCondMember = options.acsCondMember || 'art';
|
|
|
|
assetSpec = client.acs.getConditionalValue(assetSpec, acsCondMember);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const artAsset = asset.getArtAsset(assetSpec);
|
|
|
|
if(!artAsset) {
|
|
|
|
return cb(new Error('Asset not found: ' + assetSpec));
|
|
|
|
}
|
|
|
|
|
2018-07-05 00:43:40 +00:00
|
|
|
const dispOpts = Object.assign( {}, options, { client, name : artAsset.asset } );
|
2018-06-22 05:15:04 +00:00
|
|
|
switch(artAsset.type) {
|
|
|
|
case 'art' :
|
|
|
|
displayThemeArt(dispOpts, function displayed(err, artData) {
|
|
|
|
return cb(err, err ? null : { mciMap : artData.mciMap, height : artData.extraInfo.height } );
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'method' :
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: fetch & render via method
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'inline ' :
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: think about this more in relation to themes, etc. How can this come
|
|
|
|
// from a theme (with override from menu.json) ???
|
|
|
|
// look @ client.currentTheme.inlineArt[name] -> menu/prompt[name]
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
return cb(new Error('Unsupported art asset type: ' + artAsset.type));
|
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
}
|