2014-10-27 04:06:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +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 asset = require('./asset.js');
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const AnsiPrep = require('./ansi_prep.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
|
|
|
|
const ConfigLoader = require('./config_loader');
|
2020-06-17 06:10:51 +00:00
|
|
|
const { getConfigPath } = require('./config_util');
|
|
|
|
|
2018-11-24 00:41:16 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +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;
|
|
|
|
exports.setClientTheme = setClientTheme;
|
|
|
|
exports.displayPreparedArt = displayPreparedArt;
|
|
|
|
exports.displayThemeArt = displayThemeArt;
|
|
|
|
exports.displayThemedPause = displayThemedPause;
|
|
|
|
exports.displayThemedPrompt = displayThemedPrompt;
|
|
|
|
exports.displayThemedAsset = displayThemedAsset;
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// global instance of ThemeManager; see ThemeManager.create()
|
|
|
|
let themeManagerInstance;
|
|
|
|
|
|
|
|
exports.ThemeManager = class ThemeManager {
|
|
|
|
constructor() {
|
|
|
|
this.availableThemes = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
static create(cb) {
|
|
|
|
themeManagerInstance = new ThemeManager();
|
|
|
|
themeManagerInstance.init(err => {
|
|
|
|
if (!err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
themeManagerInstance
|
|
|
|
.getAvailableThemes()
|
|
|
|
.forEach((themeConfig, themeId) => {
|
|
|
|
const { name, author, group } = themeConfig.get().info;
|
|
|
|
Log.info(
|
|
|
|
{ themeId, themeName: name, author, group },
|
|
|
|
'Theme loaded'
|
|
|
|
);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
2015-09-02 04:42:54 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
getAvailableThemes() {
|
|
|
|
return this.availableThemes;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
init(cb) {
|
|
|
|
this.menuConfig = new ConfigLoader({
|
2022-06-05 20:04:25 +00:00
|
|
|
onReload: err => {
|
2020-06-17 06:10:51 +00:00
|
|
|
if (!err) {
|
|
|
|
// menu.hjson/includes have changed; this could affect
|
|
|
|
// all themes, so they must be reloaded
|
|
|
|
Events.emit(Events.getSystemEvents().MenusChanged);
|
|
|
|
|
|
|
|
this._reloadAllThemes();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
this.menuConfig.init(getConfigPath(Config().general.menuFile), err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
return this._loadThemes(cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadThemes(cb) {
|
|
|
|
const themeDir = Config().paths.themes;
|
|
|
|
|
|
|
|
fs.readdir(themeDir, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.filter(
|
|
|
|
files,
|
|
|
|
(filename, nextFilename) => {
|
|
|
|
const fullPath = paths.join(themeDir, filename);
|
|
|
|
fs.stat(fullPath, (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
return nextFilename(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextFilename(null, stats.isDirectory());
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(err, themeIds) => {
|
2020-06-17 06:10:51 +00:00
|
|
|
if (err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(err);
|
2020-06-17 06:10:51 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
themeIds,
|
|
|
|
(themeId, nextThemeId) => {
|
|
|
|
return this._loadTheme(themeId, nextThemeId);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2020-06-17 06:10:51 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2020-06-17 06:10:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadTheme(themeId, cb) {
|
|
|
|
const themeConfig = new ConfigLoader({
|
2022-06-05 20:04:25 +00:00
|
|
|
onReload: err => {
|
2020-06-17 06:10:51 +00:00
|
|
|
if (!err) {
|
|
|
|
// this particular theme has changed
|
2020-07-05 20:06:10 +00:00
|
|
|
this._themeLoaded(themeId, themeConfig, err => {
|
2020-06-17 06:10:51 +00:00
|
|
|
if (!err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().ThemeChanged, {
|
|
|
|
themeId,
|
|
|
|
});
|
2020-06-17 06:10:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const themeConfigPath = paths.join(Config().paths.themes, themeId, 'theme.hjson');
|
|
|
|
|
|
|
|
themeConfig.init(themeConfigPath, err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
this._themeLoaded(themeId, themeConfig);
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_themeLoaded(themeId, themeConfig) {
|
|
|
|
const theme = themeConfig.get();
|
|
|
|
|
|
|
|
// do some basic validation
|
|
|
|
// :TODO: schema validation here
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!_.isObject(theme.info) ||
|
2018-06-23 03:26:46 +00:00
|
|
|
!_.isString(theme.info.name) ||
|
2022-06-05 20:04:25 +00:00
|
|
|
!_.isString(theme.info.author)
|
|
|
|
) {
|
|
|
|
return Log.warn(
|
|
|
|
{ themeId },
|
|
|
|
'Theme contains invalid or missing "info" section'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-09-10 03:31:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (false === _.get(theme, 'info.enabled')) {
|
2020-06-17 06:10:51 +00:00
|
|
|
Log.info({ themeId }, 'Theme is disabled');
|
|
|
|
return this.availableThemes.delete(themeId);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-09-04 15:57:10 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// merge menu.hjson+theme.hjson/etc. to the final usable theme
|
2020-06-19 02:46:24 +00:00
|
|
|
this._finalizeTheme(themeId, themeConfig);
|
2017-09-04 15:57:10 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// Theme is valid and enabled; update it in available themes
|
|
|
|
this.availableThemes.set(themeId, themeConfig);
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().ThemeChanged, { themeId });
|
2020-06-17 06:10:51 +00:00
|
|
|
}
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2020-06-19 02:46:24 +00:00
|
|
|
_finalizeTheme(themeId, themeConfig) {
|
2020-06-17 06:10:51 +00:00
|
|
|
// These TODOs are left over from the old system - decide what/if to do with them:
|
|
|
|
// :TODO: merge in defaults (customization.defaults{} )
|
|
|
|
// :TODO: apply generic stuff, e.g. "VM" (vs "VM1")
|
2016-01-15 05:44:33 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// start out with menu.hjson
|
|
|
|
const mergedTheme = _.cloneDeep(this.menuConfig.get());
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
const theme = themeConfig.get();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// some data brought directly over
|
2022-06-05 20:04:25 +00:00
|
|
|
mergedTheme.info = Object.assign({}, theme.info, { themeId });
|
|
|
|
mergedTheme.achievements = _.get(theme, 'customization.achievements');
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// Create some helpers for this theme
|
|
|
|
this._setThemeHelpers(mergedTheme);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
// merge customizer to disallow immutable MCI properties
|
2022-06-05 20:04:25 +00:00
|
|
|
const ImmutableMCIProperties = ['maxLength', 'argName', 'submit', 'validate'];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
const mciCustomizer = (objVal, srcVal, key) => {
|
|
|
|
return ImmutableMCIProperties.indexOf(key) > -1 ? objVal : srcVal;
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const getFormKeys = obj => {
|
2020-06-17 06:10:51 +00:00
|
|
|
// remove all non-numbers
|
|
|
|
return _.remove(Object.keys(obj), k => !isNaN(k));
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
const mergeMciProperties = (dst, src) => {
|
|
|
|
Object.keys(src).forEach(mci => {
|
|
|
|
if (dst[mci]) {
|
|
|
|
_.mergeWith(dst[mci], src[mci], mciCustomizer);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2020-06-17 06:10:51 +00:00
|
|
|
// theme contains a MCI that's not found in menu
|
|
|
|
dst[mci] = src[mci];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
2020-06-17 06:10:51 +00:00
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
const applyThemeMciBlock = (dst, src, formKey) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isObject(src.mci)) {
|
2020-06-17 06:10:51 +00:00
|
|
|
mergeMciProperties(dst, src.mci);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.has(src, [formKey, 'mci'])) {
|
2020-06-17 06:10:51 +00:00
|
|
|
mergeMciProperties(dst, 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.
|
|
|
|
//
|
|
|
|
const 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 {
|
|
|
|
// remove anything not uppercase
|
2022-06-05 20:04:25 +00:00
|
|
|
const menuMciCodeKeys = _.remove(
|
|
|
|
_.keys(form),
|
|
|
|
k => k === k.toUpperCase()
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
menuMciCodeKeys.forEach(mciKey => {
|
2022-06-05 20:04:25 +00:00
|
|
|
const src = _.has(menuTheme, [mciKey, 'mci'])
|
|
|
|
? menuTheme[mciKey]
|
|
|
|
: menuTheme;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
applyThemeMciBlock(form[mciKey].mci, src, formKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
['menus', 'prompts'].forEach(sectionName => {
|
2020-07-08 01:47:51 +00:00
|
|
|
if (!_.isObject(mergedTheme[sectionName])) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return Log.error({ sectionName }, 'Merged theme is missing section');
|
2020-07-08 01:41:17 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
Object.keys(mergedTheme[sectionName]).forEach(entryName => {
|
|
|
|
let createdFormSection = false;
|
|
|
|
const mergedThemeMenu = mergedTheme[sectionName][entryName];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const menuTheme = _.get(theme, ['customization', sectionName, entryName]);
|
2020-06-17 06:10:51 +00:00
|
|
|
if (menuTheme) {
|
|
|
|
if (menuTheme.config) {
|
|
|
|
// :TODO: should this be _.merge() ?
|
2022-06-05 20:04:25 +00:00
|
|
|
mergedThemeMenu.config = _.assign(
|
|
|
|
mergedThemeMenu.config || {},
|
|
|
|
menuTheme.config
|
|
|
|
);
|
2020-06-17 06:10:51 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('menus' === sectionName) {
|
|
|
|
if (_.isObject(mergedThemeMenu.form)) {
|
2020-06-17 06:10:51 +00:00
|
|
|
getFormKeys(mergedThemeMenu.form).forEach(formKey => {
|
2022-06-05 20:04:25 +00:00
|
|
|
applyToForm(
|
|
|
|
mergedThemeMenu.form[formKey],
|
|
|
|
menuTheme,
|
|
|
|
formKey
|
|
|
|
);
|
2020-06-17 06:10:51 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isObject(menuTheme.mci)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
// Not specified at menu level means we apply anything from the
|
|
|
|
// theme to form.0.mci{}
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
mergedThemeMenu.form = { 0: { mci: {} } };
|
2018-06-22 05:15:04 +00:00
|
|
|
mergeMciProperties(mergedThemeMenu.form[0], menuTheme);
|
|
|
|
createdFormSection = true;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('prompts' === sectionName) {
|
2020-06-17 06:10:51 +00:00
|
|
|
// no 'form' or form keys for prompts -- direct to mci
|
|
|
|
applyToForm(mergedThemeMenu, menuTheme);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
'menus' === sectionName &&
|
2020-06-17 06:10:51 +00:00
|
|
|
!_.isString(mergedThemeMenu.prompt) &&
|
2022-06-05 20:04:25 +00:00
|
|
|
(createdFormSection || !_.isObject(mergedThemeMenu.form))
|
|
|
|
) {
|
|
|
|
mergedThemeMenu.runtime = _.merge(mergedThemeMenu.runtime || {}, {
|
|
|
|
autoNext: true,
|
|
|
|
});
|
2020-06-17 06:10:51 +00:00
|
|
|
}
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2016-01-15 05:44:33 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
themeConfig.current = mergedTheme;
|
|
|
|
}
|
2018-06-14 03:02:00 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
_setThemeHelpers(theme) {
|
|
|
|
theme.helpers = {
|
2022-06-05 20:04:25 +00:00
|
|
|
getPasswordChar: function () {
|
2020-06-17 06:10:51 +00:00
|
|
|
let pwChar = _.get(
|
|
|
|
theme,
|
|
|
|
'customization.defaults.passwordChar',
|
|
|
|
Config().theme.passwordChar
|
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(pwChar)) {
|
2020-06-17 06:10:51 +00:00
|
|
|
pwChar = pwChar.substr(0, 1);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (_.isNumber(pwChar)) {
|
2020-06-17 06:10:51 +00:00
|
|
|
pwChar = String.fromCharCode(pwChar);
|
|
|
|
}
|
2018-06-14 03:02:00 +00:00
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
return pwChar;
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
getDateFormat: function (style = 'short') {
|
2020-06-17 06:10:51 +00:00
|
|
|
const format = Config().theme.dateFormat[style] || 'MM/DD/YYYY';
|
|
|
|
return _.get(theme, `customization.defaults.dateFormat.${style}`, format);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
getTimeFormat: function (style = 'short') {
|
2020-06-17 06:10:51 +00:00
|
|
|
const format = Config().theme.timeFormat[style] || 'h:mm a';
|
|
|
|
return _.get(theme, `customization.defaults.timeFormat.${style}`, format);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
getDateTimeFormat: function (style = 'short') {
|
|
|
|
const format =
|
|
|
|
Config().theme.dateTimeFormat[style] || 'MM/DD/YYYY h:mm a';
|
|
|
|
return _.get(
|
|
|
|
theme,
|
|
|
|
`customization.defaults.dateTimeFormat.${style}`,
|
|
|
|
format
|
|
|
|
);
|
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_reloadAllThemes() {
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each([...this.availableThemes.keys()], (themeId, nextThemeId) => {
|
2020-06-17 06:10:51 +00:00
|
|
|
this._loadTheme(themeId, err => {
|
|
|
|
if (!err) {
|
|
|
|
Log.info({ themeId }, 'Theme reloaded');
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return nextThemeId(null); // always proceed
|
2020-06-17 06:10:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2015-12-24 18:56:04 +00:00
|
|
|
function getAvailableThemes() {
|
2020-06-17 06:10:51 +00:00
|
|
|
return themeManagerInstance.getAvailableThemes();
|
2015-12-24 18:56:04 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
function getRandomTheme() {
|
2020-06-17 06:10:51 +00:00
|
|
|
const avail = getAvailableThemes();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (avail.size > 0) {
|
|
|
|
const themeIds = [...avail.keys()];
|
2018-06-22 05:15:04 +00:00
|
|
|
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();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (availThemes.has(themeId)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg = 'Set client theme';
|
|
|
|
setThemeId = themeId;
|
2022-06-05 20:04:25 +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 {
|
2022-06-05 20:04:25 +00:00
|
|
|
msg =
|
|
|
|
'Failed setting theme by system default ID; Using the first one we can find';
|
2018-06-22 05:15:04 +00:00
|
|
|
setThemeId = availThemes.keys().next().value;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.currentTheme = availThemes.get(setThemeId);
|
2022-06-05 20:04:25 +00:00
|
|
|
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();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!options.themeId &&
|
|
|
|
_.has(options, ['client', 'user', 'properties', UserProps.ThemeId])
|
|
|
|
) {
|
2018-11-24 00:41:16 +00:00
|
|
|
options.themeId = options.client.user.properties[UserProps.ThemeId];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 03:21:31 +00:00
|
|
|
options.themeId = options.themeId || config.theme.default;
|
|
|
|
|
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!
|
2022-06-05 20:04:25 +00:00
|
|
|
options.asAnsi = true; // always convert to ANSI
|
|
|
|
options.readSauce = _.get(options, 'readSauce', true); // read SAUCE, if avail
|
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (options.name.indexOf('/') > -1) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// make relative to base BBS dir
|
2022-06-05 20:04:25 +00:00
|
|
|
options.basePath = paths.join(
|
|
|
|
__dirname,
|
|
|
|
'../',
|
|
|
|
paths.dirname(options.name)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
return callback(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
art.getArt(options.name, options, (err, artInfo) => {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fromSuppliedTheme(artInfo, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (artInfo) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (artInfo) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.basePath = config.paths.art;
|
|
|
|
art.getArt(options.name, options, (err, artInfo) => {
|
|
|
|
return callback(err, artInfo);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
function complete(err, artInfo) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const logger = _.get(options, 'client.log') || Log;
|
2022-06-05 20:04:25 +00:00
|
|
|
logger.debug({ reason: err.message }, 'Cannot find theme art');
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
sauce: artInfo.sauce,
|
|
|
|
font: options.font,
|
|
|
|
trailingLF: options.trailingLF,
|
|
|
|
startRow: options.startRow,
|
2019-05-25 04:26:27 +00:00
|
|
|
};
|
|
|
|
art.display(options.client, artInfo.data, displayOpts, (err, mciMap, extraInfo) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(err, { mciMap: mciMap, artInfo: artInfo, extraInfo: extraInfo });
|
2019-05-25 04:26:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isObject(options.ansiPrepOptions)) {
|
|
|
|
AnsiPrep(artInfo.data, options.ansiPrepOptions, (err, prepped) => {
|
|
|
|
if (!err && prepped) {
|
|
|
|
artInfo.data = prepped;
|
|
|
|
return callback(null, artInfo);
|
2018-07-05 00:43:40 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
});
|
2018-07-05 00:43:40 +00:00
|
|
|
} else {
|
|
|
|
return callback(null, artInfo);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function disp(artInfo, callback) {
|
2019-05-25 04:26:27 +00:00
|
|
|
return displayPreparedArt(options, artInfo, callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
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];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!promptConfig) {
|
|
|
|
return callback(
|
|
|
|
Errors.DoesNotExist(`Missing "${name}" prompt configuration!`)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.clearScreen) {
|
2018-06-22 05:15:04 +00:00
|
|
|
client.term.rawWrite(ansi.resetScreen());
|
2022-06-05 20:04:25 +00:00
|
|
|
options.position = { row: 1, column: 1 };
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
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
|
|
|
//
|
2022-06-05 20:04:25 +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:
|
2021-02-10 03:25:54 +00:00
|
|
|
// if(!options.clearScreen) {
|
|
|
|
// dispOptions.font = 'not_really_a_font!'; // kludge :)
|
|
|
|
// }
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
displayThemedAsset(
|
|
|
|
promptConfig.art,
|
|
|
|
client,
|
|
|
|
dispOptions,
|
|
|
|
(err, artInfo) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, promptConfig, artInfo);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function discoverCursorPosition(promptConfig, artInfo, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isNumber(options?.position?.row)) {
|
2022-03-28 19:41:12 +00:00
|
|
|
artInfo.startRow = options.position.row;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
client.term.termHeight > 0 &&
|
|
|
|
artInfo.startRow + artInfo.height > client.term.termHeight
|
|
|
|
) {
|
2022-03-26 00:27:01 +00:00
|
|
|
// in this case, we will have scrolled
|
|
|
|
artInfo.startRow = client.term.termHeight - artInfo.height;
|
|
|
|
}
|
2022-03-25 01:01:07 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-03-25 01:01:07 +00:00
|
|
|
return callback(null, promptConfig, artInfo);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function createMCIViews(promptConfig, artInfo, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const assocViewController = usingTempViewController
|
|
|
|
? new ViewController({ client: client })
|
|
|
|
: options.viewController;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const loadOpts = {
|
2022-06-05 20:04:25 +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) {
|
2022-06-05 20:04:25 +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
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +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) {
|
2022-03-26 00:27:01 +00:00
|
|
|
// Only clear with height if clearPrompt is true and if we were able
|
2022-03-25 01:01:07 +00:00
|
|
|
// to determine the row
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.clearPrompt && artInfo.startRow) {
|
|
|
|
if (artInfo.startRow && artInfo.height) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
2018-07-05 00:43:40 +00:00
|
|
|
(err, assocViewController, artInfo) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
client.log.warn(
|
|
|
|
{ error: err.message },
|
|
|
|
`Failed displaying "${name}" prompt`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (assocViewController && usingTempViewController) {
|
2018-07-05 00:43:40 +00:00
|
|
|
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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(options)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isBoolean(options.clearPrompt)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.clearPrompt = true;
|
|
|
|
}
|
2017-02-08 03:20:10 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const promptOptions = Object.assign({}, options, { pause: true });
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
if (3 === arguments.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(assetSpec)) {
|
2018-11-18 01:56:09 +00:00
|
|
|
const acsCondMember = options.acsCondMember || 'art';
|
|
|
|
assetSpec = client.acs.getConditionalValue(assetSpec, acsCondMember);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const artAsset = asset.getArtAsset(assetSpec);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!artAsset) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(new Error('Asset not found: ' + assetSpec));
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const dispOpts = Object.assign({}, options, { client, name: artAsset.asset });
|
|
|
|
switch (artAsset.type) {
|
|
|
|
case 'art':
|
2018-06-22 05:15:04 +00:00
|
|
|
displayThemeArt(dispOpts, function displayed(err, artData) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(
|
|
|
|
err,
|
|
|
|
err
|
|
|
|
? null
|
|
|
|
: { mciMap: artData.mciMap, height: artData.extraInfo.height }
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'method':
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: fetch & render via method
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(new Error('Unsupported art asset type: ' + artAsset.type));
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|