2014-10-27 04:06:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
var Config = require('./config.js').config;
|
|
|
|
var art = require('./art.js');
|
2015-07-24 04:23:44 +00:00
|
|
|
var ansi = require('./ansi_term.js');
|
2015-05-13 05:04:22 +00:00
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var Log = require('./logger.js').log;
|
2015-09-10 03:31:04 +00:00
|
|
|
var configCache = require('./config_cache.js');
|
2015-07-25 22:10:12 +00:00
|
|
|
var asset = require('./asset.js');
|
|
|
|
var ViewController = require('./view_controller.js').ViewController;
|
2015-05-13 05:04:22 +00:00
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var paths = require('path');
|
|
|
|
var async = require('async');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
exports.loadTheme = loadTheme;
|
2014-10-27 04:06:41 +00:00
|
|
|
exports.getThemeArt = getThemeArt;
|
2014-10-30 04:23:44 +00:00
|
|
|
exports.getRandomTheme = getRandomTheme;
|
2014-10-29 11:30:20 +00:00
|
|
|
exports.initAvailableThemes = initAvailableThemes;
|
2014-11-05 06:50:42 +00:00
|
|
|
exports.displayThemeArt = displayThemeArt;
|
2015-07-25 22:10:12 +00:00
|
|
|
exports.displayThemedPause = displayThemedPause;
|
|
|
|
exports.displayThemedAsset = displayThemedAsset;
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
function refreshThemeHelpers(theme) {
|
|
|
|
//
|
|
|
|
// Create some handy helpers
|
|
|
|
//
|
|
|
|
theme.helpers = {
|
|
|
|
getPasswordChar : function() {
|
|
|
|
var pwChar = Config.defaults.passwordChar;
|
|
|
|
if(_.has(theme, 'customization.defaults.general')) {
|
|
|
|
var themePasswordChar = theme.customization.defaults.general.passwordChar;
|
|
|
|
if(_.isString(themePasswordChar)) {
|
|
|
|
pwChar = themePasswordChar.substr(0, 1);
|
|
|
|
} else if(_.isNumber(themePasswordChar)) {
|
|
|
|
pwChar = String.fromCharCode(themePasswordChar);
|
2015-05-13 05:04:22 +00:00
|
|
|
}
|
2015-09-10 03:31:04 +00:00
|
|
|
}
|
|
|
|
return pwChar;
|
|
|
|
},
|
|
|
|
getDateFormat : function(style) {
|
|
|
|
style = style || 'short';
|
2015-05-13 05:04:22 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
var format = Config.defaults.dateFormat[style] || 'MM/DD/YYYY';
|
2015-07-23 03:35:35 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
if(_.has(theme, 'customization.defaults.dateFormat')) {
|
|
|
|
return theme.customization.defaults.dateFormat[style] || format;
|
|
|
|
}
|
|
|
|
return format;
|
|
|
|
},
|
|
|
|
getTimeFormat : function(style) {
|
|
|
|
style = style || 'short';
|
2015-07-24 04:23:44 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
var format = Config.defaults.timeFormat[style] || 'h:mm a';
|
2015-07-24 04:23:44 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
if(_.has(theme, 'customization.defaults.timeFormat')) {
|
|
|
|
return theme.customization.defaults.timeFormat[style] || format;
|
|
|
|
}
|
|
|
|
return format;
|
|
|
|
},
|
|
|
|
getDateTimeFormat : function(style) {
|
|
|
|
style = style || 'short';
|
2015-07-24 04:23:44 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
var format = Config.defaults.dateTimeFormat[style] || 'MM/DD/YYYY h:mm a';
|
2015-09-02 04:42:54 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
if(_.has(theme, 'customization.defaults.dateTimeFormat')) {
|
|
|
|
return theme.customization.defaults.dateTimeFormat[style] || format;
|
|
|
|
}
|
2015-09-02 04:42:54 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 04:42:54 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
function loadTheme(themeID, cb) {
|
2015-04-17 04:29:53 +00:00
|
|
|
|
2015-09-10 03:31:04 +00:00
|
|
|
var path = paths.join(Config.paths.themes, themeID, 'theme.hjson');
|
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
configCache.getConfigWithOptions( { filePath : path, forceReCache : true }, function loaded(err, theme) {
|
2015-09-10 03:31:04 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
if(!_.isObject(theme.info)) {
|
|
|
|
cb(new Error('Invalid theme or missing \'info\' section'));
|
|
|
|
return;
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
2015-09-10 03:31:04 +00:00
|
|
|
|
|
|
|
refreshThemeHelpers(theme);
|
|
|
|
|
|
|
|
cb(null, theme, path);
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
var availableThemes = {};
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initAvailableThemes(cb) {
|
2014-10-30 04:23:44 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getDir(callback) {
|
2014-10-31 04:59:21 +00:00
|
|
|
fs.readdir(Config.paths.themes, function onReadDir(err, files) {
|
2014-10-30 04:23:44 +00:00
|
|
|
callback(err, files);
|
|
|
|
});
|
|
|
|
},
|
2014-10-31 04:59:21 +00:00
|
|
|
function filterFiles(files, callback) {
|
2014-10-30 04:23:44 +00:00
|
|
|
var filtered = files.filter(function onFilter(file) {
|
2014-10-29 11:30:20 +00:00
|
|
|
return fs.statSync(paths.join(Config.paths.themes, file)).isDirectory();
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
|
|
|
callback(null, filtered);
|
|
|
|
},
|
|
|
|
function populateAvailable(filtered, callback) {
|
|
|
|
filtered.forEach(function onTheme(themeId) {
|
2015-09-10 03:31:04 +00:00
|
|
|
loadTheme(themeId, function themeLoaded(err, theme, themePath) {
|
2014-10-30 04:23:44 +00:00
|
|
|
if(!err) {
|
2015-05-13 05:04:22 +00:00
|
|
|
availableThemes[themeId] = theme;
|
2015-09-10 03:31:04 +00:00
|
|
|
|
|
|
|
configCache.on('recached', function recached(path) {
|
|
|
|
if(themePath === path) {
|
|
|
|
loadTheme(themeId, function reloaded(err, reloadedTheme) {
|
|
|
|
Log.debug( { info : theme.info }, 'Theme recached' );
|
|
|
|
|
|
|
|
availableThemes[themeId] = reloadedTheme;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-05-13 05:04:22 +00:00
|
|
|
Log.debug( { info : theme.info }, 'Theme loaded');
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
});
|
2015-05-13 05:04:22 +00:00
|
|
|
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
2014-10-31 04:59:21 +00:00
|
|
|
callback(null);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
cb(null, availableThemes.length);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
function getRandomTheme() {
|
|
|
|
if(Object.getOwnPropertyNames(availableThemes).length > 0) {
|
|
|
|
var themeIds = Object.keys(availableThemes);
|
|
|
|
return themeIds[Math.floor(Math.random() * themeIds.length)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-28 04:05:40 +00:00
|
|
|
function getThemeArt(options, cb) {
|
|
|
|
//
|
|
|
|
// options - required:
|
|
|
|
// name
|
|
|
|
// client
|
|
|
|
//
|
|
|
|
// options - optional
|
|
|
|
// themeId
|
|
|
|
// asAnsi
|
|
|
|
// readSauce
|
|
|
|
// random
|
|
|
|
//
|
|
|
|
if(!options.themeId && _.has(options.client, 'user.properties.theme_id')) {
|
|
|
|
options.themeId = options.client.user.properties.theme_id;
|
|
|
|
} else {
|
|
|
|
options.themeId = Config.defaults.theme;
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
|
2015-06-09 04:41:57 +00:00
|
|
|
// :TODO: replace asAnsi stuff with something like retrieveAs = 'ansi' | 'pipe' | ...
|
|
|
|
// :TODO: Some of these options should only be set if not provided!
|
2015-09-28 04:05:40 +00:00
|
|
|
options.asAnsi = true; // always convert to ANSI
|
|
|
|
options.readSauce = true; // read SAUCE, if avail
|
|
|
|
options.random = _.isBoolean(options.random) ? options.random : true; // FILENAME<n>.EXT support
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2015-09-28 04:05:40 +00:00
|
|
|
//
|
|
|
|
// We look for themed art in the following manor:
|
|
|
|
// * Supplied theme via |themeId|
|
|
|
|
// * Fallback 1: Default theme (if different than |themeId|)
|
|
|
|
// * General art directory
|
|
|
|
//
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function fromSuppliedTheme(callback) {
|
|
|
|
options.basePath = paths.join(Config.paths.themes, options.themeId);
|
|
|
|
|
|
|
|
art.getArt(options.name, options, function artLoaded(err, artInfo) {
|
|
|
|
callback(null, artInfo);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fromDefaultTheme(artInfo, callback) {
|
|
|
|
if(artInfo || Config.defaults.theme === options.themeId) {
|
|
|
|
callback(null, artInfo);
|
2014-10-27 04:06:41 +00:00
|
|
|
} else {
|
2015-09-28 04:05:40 +00:00
|
|
|
options.basePath = paths.join(Config.paths.themes, Config.defaults.theme);
|
|
|
|
|
|
|
|
art.getArt(options.name, options, function artLoaded(err, artInfo) {
|
|
|
|
callback(null, artInfo);
|
|
|
|
});
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
2015-09-28 04:05:40 +00:00
|
|
|
},
|
|
|
|
function fromGeneralArtDir(artInfo, callback) {
|
|
|
|
if(artInfo) {
|
|
|
|
callback(null, artInfo);
|
|
|
|
} else {
|
|
|
|
options.basePath = Config.paths.art;
|
|
|
|
|
|
|
|
art.getArt(options.name, options, function artLoaded(err, artInfo) {
|
|
|
|
callback(err, artInfo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2015-10-06 21:22:49 +00:00
|
|
|
function complete(err, artInfo) {
|
|
|
|
if(err) {
|
|
|
|
options.client.log.debug( { error : err }, 'Cannot find art');
|
|
|
|
}
|
|
|
|
cb(err, artInfo);
|
|
|
|
}
|
2015-09-28 04:05:40 +00:00
|
|
|
);
|
2014-11-05 06:50:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 04:29:53 +00:00
|
|
|
function displayThemeArt(options, cb) {
|
|
|
|
assert(_.isObject(options));
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
assert(_.isString(options.name));
|
|
|
|
|
2015-09-28 04:05:40 +00:00
|
|
|
getThemeArt(options, function themeArt(err, artInfo) {
|
2014-11-05 06:50:42 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2015-09-27 21:35:24 +00:00
|
|
|
// :TODO: just use simple merge of options -> displayOptions
|
2015-04-17 04:29:53 +00:00
|
|
|
var dispOptions = {
|
2015-07-25 22:10:12 +00:00
|
|
|
art : artInfo.data,
|
|
|
|
sauce : artInfo.sauce,
|
|
|
|
client : options.client,
|
|
|
|
font : options.font,
|
2015-09-27 21:35:24 +00:00
|
|
|
trailingLF : options.trailingLF,
|
2015-04-17 04:29:53 +00:00
|
|
|
};
|
|
|
|
|
2015-06-26 04:34:33 +00:00
|
|
|
art.display(dispOptions, function displayed(err, mciMap, extraInfo) {
|
|
|
|
cb(err, { mciMap : mciMap, artInfo : artInfo, extraInfo : extraInfo } );
|
2014-11-05 06:50:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-07-06 01:05:55 +00:00
|
|
|
}
|
2015-07-24 04:23:44 +00:00
|
|
|
|
2015-07-25 00:33:59 +00:00
|
|
|
//
|
|
|
|
// Pause prompts are a special prompt by the name 'pause'.
|
|
|
|
//
|
2015-07-25 22:10:12 +00:00
|
|
|
function displayThemedPause(options, cb) {
|
2015-07-24 04:23:44 +00:00
|
|
|
//
|
|
|
|
// options.client
|
|
|
|
// options clearPrompt
|
|
|
|
//
|
|
|
|
assert(_.isObject(options.client));
|
|
|
|
|
|
|
|
if(!_.isBoolean(options.clearPrompt)) {
|
|
|
|
options.clearPrompt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: Support animated pause prompts. Probably via MCI with AnimatedView
|
2015-07-25 00:33:59 +00:00
|
|
|
|
|
|
|
var artInfo;
|
2015-07-25 22:10:12 +00:00
|
|
|
var vc;
|
|
|
|
var promptConfig;
|
2015-07-24 04:23:44 +00:00
|
|
|
|
2015-07-25 22:10:12 +00:00
|
|
|
async.series(
|
2015-07-24 04:23:44 +00:00
|
|
|
[
|
|
|
|
function loadPromptJSON(callback) {
|
2015-09-10 03:31:04 +00:00
|
|
|
configCache.getModConfig('prompt.hjson', function loaded(err, promptJson) {
|
2015-07-24 04:23:44 +00:00
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
if(_.has(promptJson, [ 'prompts', 'pause' ] )) {
|
2015-07-25 22:10:12 +00:00
|
|
|
promptConfig = promptJson.prompts.pause;
|
|
|
|
callback(_.isObject(promptConfig) ? null : new Error('Invalid prompt config block!'));
|
2015-07-24 04:23:44 +00:00
|
|
|
} else {
|
2015-11-01 20:32:52 +00:00
|
|
|
callback(new Error('Missing standard \'pause\' prompt'));
|
2015-07-24 04:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 04:31:28 +00:00
|
|
|
});
|
2015-07-24 04:23:44 +00:00
|
|
|
},
|
2015-07-25 22:10:12 +00:00
|
|
|
function displayPausePrompt(callback) {
|
2015-11-01 20:32:52 +00:00
|
|
|
//
|
|
|
|
// Override .font so it doesn't change from current setting
|
|
|
|
//
|
|
|
|
var dispOptions = promptConfig.options;
|
|
|
|
dispOptions.font = 'not_really_a_font!';
|
|
|
|
|
2015-07-25 22:10:12 +00:00
|
|
|
displayThemedAsset(
|
|
|
|
promptConfig.art,
|
|
|
|
options.client,
|
2015-11-01 20:32:52 +00:00
|
|
|
dispOptions,
|
2015-07-25 22:10:12 +00:00
|
|
|
function displayed(err, artData) {
|
|
|
|
artInfo = artData;
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
);
|
2015-07-25 00:33:59 +00:00
|
|
|
},
|
|
|
|
function discoverCursorPosition(callback) {
|
|
|
|
options.client.once('cursor position report', function cpr(pos) {
|
2015-07-25 22:10:12 +00:00
|
|
|
artInfo.startRow = pos[0] - artInfo.height;
|
2015-07-24 04:23:44 +00:00
|
|
|
callback(null);
|
|
|
|
});
|
2015-07-25 00:33:59 +00:00
|
|
|
options.client.term.rawWrite(ansi.queryPos());
|
2015-07-24 04:23:44 +00:00
|
|
|
},
|
2015-07-25 22:10:12 +00:00
|
|
|
function createMCIViews(callback) {
|
|
|
|
vc = new ViewController( { client : options.client, noInput : true } );
|
|
|
|
vc.loadFromPromptConfig( { promptName : 'pause', mciMap : artInfo.mciMap, config : promptConfig }, function loaded(err) {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
},
|
2015-07-24 04:23:44 +00:00
|
|
|
function pauseForUserInput(callback) {
|
|
|
|
options.client.waitForKeyPress(function keyPressed() {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function clearPauseArt(callback) {
|
|
|
|
if(options.clearPrompt) {
|
2015-07-25 00:33:59 +00:00
|
|
|
if(artInfo.startRow) {
|
|
|
|
options.client.term.rawWrite(ansi.goto(artInfo.startRow, 1));
|
2015-07-25 22:10:12 +00:00
|
|
|
options.client.term.rawWrite(ansi.deleteLine(artInfo.height));
|
2015-07-25 00:33:59 +00:00
|
|
|
} else {
|
|
|
|
options.client.term.rawWrite(ansi.up(1) + ansi.deleteLine());
|
|
|
|
}
|
2015-07-24 04:23:44 +00:00
|
|
|
}
|
|
|
|
callback(null);
|
|
|
|
}
|
2015-07-25 00:33:59 +00:00
|
|
|
/*
|
2015-07-24 04:23:44 +00:00
|
|
|
, function debugPause(callback) {
|
|
|
|
setTimeout(function to() {
|
|
|
|
callback(null);
|
|
|
|
}, 4000);
|
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
*/
|
2015-07-24 04:23:44 +00:00
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
Log.error(err);
|
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
|
|
|
|
if(vc) {
|
|
|
|
vc.detachClientEvents();
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:23:44 +00:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2015-07-25 22:10:12 +00:00
|
|
|
|
|
|
|
function displayThemedAsset(assetSpec, client, options, cb) {
|
|
|
|
assert(_.isObject(client));
|
|
|
|
|
|
|
|
// options are... optional
|
|
|
|
if(3 === arguments.length) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
var artAsset = asset.getArtAsset(assetSpec);
|
|
|
|
if(!artAsset) {
|
|
|
|
cb(new Error('Asset not found: ' + assetSpec));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
// :TODO: just use simple merge of options -> displayOptions
|
2015-07-25 22:10:12 +00:00
|
|
|
var dispOpts = {
|
|
|
|
name : artAsset.asset,
|
|
|
|
client : client,
|
|
|
|
font : options.font,
|
2015-09-27 21:35:24 +00:00
|
|
|
trailingLF : options.trailingLF,
|
2015-07-25 22:10:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
switch(artAsset.type) {
|
|
|
|
case 'art' :
|
|
|
|
displayThemeArt(dispOpts, function displayed(err, artData) {
|
|
|
|
cb(err, err ? null : { mciMap : artData.mciMap, height : artData.extraInfo.height } );
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'method' :
|
|
|
|
// :TODO: fetch & render via method
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'inline ' :
|
|
|
|
// :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]
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
|
|
|
cb(new Error('Unsupported art asset type: ' + artAsset.type));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|