* Start on new theme re-work

* theme_info.json -> theme.json
* Allow pipe codes for styleSGRx
This commit is contained in:
Bryan Ashby 2015-05-12 23:04:22 -06:00
parent 58746ca9a9
commit 2aa43295e3
10 changed files with 68 additions and 61 deletions

View File

@ -211,8 +211,8 @@ function prepareClient(client, cb) {
client.user.properties.theme_id = theme.getRandomTheme() || ''; client.user.properties.theme_id = theme.getRandomTheme() || '';
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) { theme.loadTheme(client.user.properties.theme_id, function themeLoaded(err, theme) {
client.currentThemeInfo = info; client.currentTheme = theme;
cb(null); cb(null);
}); });
} else { } else {

View File

@ -77,7 +77,6 @@ function getDefaultConfig() {
preLoginTheme : '*', preLoginTheme : '*',
// :TODO: change to nua
users : { users : {
usernameMin : 2, usernameMin : 2,
usernameMax : 22, usernameMax : 22,

View File

@ -25,9 +25,9 @@ function login(callingMenu, formData, extraArgs) {
async.parallel( async.parallel(
[ [
function loadThemeConfig(callback) { function loadThemeConfig(callback) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) { theme.loadTheme(client.user.properties.theme_id, function themeLoaded(err, theme) {
client.currentThemeInfo = info; client.currentTheme = theme;
callback(null); callback(null); // always non-fatal
}); });
} }
], ],

View File

@ -1,50 +1,60 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var Config = require('./config.js').config; var Config = require('./config.js').config;
var art = require('./art.js'); var art = require('./art.js');
var miscUtil = require('./misc_util.js'); var miscUtil = require('./misc_util.js');
var Log = require('./logger.js').log; var Log = require('./logger.js').log;
var fs = require('fs'); var fs = require('fs');
var paths = require('path'); var paths = require('path');
var async = require('async'); var async = require('async');
var _ = require('lodash'); var _ = require('lodash');
var assert = require('assert'); var assert = require('assert');
var stripJsonComments = require('strip-json-comments');
exports.getThemeInfo = getThemeInfo; exports.loadTheme = loadTheme;
exports.getThemeArt = getThemeArt; exports.getThemeArt = getThemeArt;
exports.getRandomTheme = getRandomTheme; exports.getRandomTheme = getRandomTheme;
exports.initAvailableThemes = initAvailableThemes; exports.initAvailableThemes = initAvailableThemes;
exports.displayThemeArt = displayThemeArt; exports.displayThemeArt = displayThemeArt;
function getThemeInfo(themeID, cb) { function loadTheme(themeID, cb) {
var path = paths.join(Config.paths.themes, themeID, 'theme_info.json'); var path = paths.join(Config.paths.themes, themeID, 'theme.json');
fs.readFile(path, function onData(err, data) { fs.readFile(path, { encoding : 'utf8' }, function onData(err, data) {
if(err) { if(err) {
cb(err); cb(err);
} else { } else {
try { try {
// :TODO: strip comments/etc. ala menu.json var theme = JSON.parse(stripJsonComments(data));
var info = JSON.parse(data);
if(!_.isObject(theme.info)) {
cb(new Error('Invalid theme JSON'));
return;
}
assert(!_.isObject(theme.helpers)); // we create this on the fly!
// //
// Create some handy helpers // Create some handy helpers
// //
info.getPasswordChar = function() { theme.helpers = {
var pwChar = Config.defaults.passwordChar; getPasswordChar : function() {
if(_.isObject(info.config)) { var pwChar = Config.defaults.passwordChar;
if(_.isString(info.config.passwordChar)) { if(_.isObject(theme.defaults) && _.isObject(theme.defaults.general)) {
pwChar = info.config.passwordChar.substr(0, 1); var themePasswordChar = theme.defaults.general.passwordChar;
} else if(_.isNumber(info.config.passwordChar)) { if(_.isString(themePasswordChar)) {
pwChar = String.fromCharCode(info.config.passwordChar); pwChar = themePasswordChar.substr(0, 1);
} else if(_.isNumber(themePasswordChar)) {
pwChar = String.fromCharCode(themePasswordChar);
}
} }
return pwChar;
} }
return pwChar; }
};
cb(null, info); cb(null, theme);
} catch(e) { } catch(e) {
cb(err); cb(err);
} }
@ -70,15 +80,13 @@ function initAvailableThemes(cb) {
}, },
function populateAvailable(filtered, callback) { function populateAvailable(filtered, callback) {
filtered.forEach(function onTheme(themeId) { filtered.forEach(function onTheme(themeId) {
getThemeInfo(themeId, function onThemeInfo(err, info) { loadTheme(themeId, function themeLoaded(err, theme) {
if(!err) { if(!err) {
if(!availableThemes) { availableThemes[themeId] = theme;
availableThemes = {}; Log.debug( { info : theme.info }, 'Theme loaded');
}
availableThemes[themeId] = info;
Log.debug( { info : info }, 'Theme loaded');
} }
}); });
}); });
callback(null); callback(null);
} }
@ -101,17 +109,6 @@ function getRandomTheme() {
} }
} }
/*
function getRandomTheme(cb) {
if(Object.getOwnPropertyNames(availableThemes).length > 0) {
var themeIds = Object.keys(availableThemes);
cb(null, themeIds[Math.floor(Math.random() * themeIds.length)]);
} else {
cb(new Error('No themes available'));
}
}
*/
function getThemeArt(name, themeID, options, cb) { function getThemeArt(name, themeID, options, cb) {
// allow options to be optional // allow options to be optional
if(_.isUndefined(cb)) { if(_.isUndefined(cb)) {

View File

@ -22,7 +22,7 @@ function User() {
this.groups = {}; // id:name this.groups = {}; // id:name
this.isValid = function() { this.isValid = function() {
if(self.userId <= 0 || self.username.length < 2) { if(self.userId <= 0 || self.username.length < Config.users.usernameMin) {
return false; return false;
} }

View File

@ -196,9 +196,9 @@ function ViewController(options) {
['styleSGR1', 'styleSGR2'].forEach(function styleSgr(style) { ['styleSGR1', 'styleSGR2'].forEach(function styleSgr(style) {
setViewProp(style, function(v) { setViewProp(style, function(v) {
if(_.isObject(v)) { if(_.isObject(v)) {
view.styleSGR1 = ansi.getSGRFromGraphicRendition(v, true); view[style] = ansi.getSGRFromGraphicRendition(v, true);
} else if(_.isString(v)) { } else if(_.isString(v)) {
view.styleSGR1 = v; view[style] = ansi.fromPipeCode(v);
} }
}); });
}); });
@ -213,7 +213,7 @@ function ViewController(options) {
setViewProp('password', function(v) { setViewProp('password', function(v) {
if(true === v) { if(true === v) {
view.textMaskChar = self.client.currentThemeInfo.getPasswordChar(); view.textMaskChar = self.client.currentTheme.helpers.getPasswordChar();
} }
}); });

View File

@ -0,0 +1,18 @@
{
"info" : {
"name" : "Nu Mayan",
"author" : "NuSkooler"
},
"defaults" : {
"general" : {
"passwordChar" : "φ"
}
},
"menus" : {
"matrix" : {
"VM1" : {
"itemSpacing" : 0
}
}
}
}

View File

@ -1,7 +0,0 @@
{
"name" : "Nu Mayan",
"author" : "NuSkooler",
"config" : {
"passwordChar" : "φ"
}
}

View File

@ -24,7 +24,7 @@ function login(callingMenu, formData, extraArgs) {
[ [
function loadThemeConfig(callback) { function loadThemeConfig(callback) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) { theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info; client.currentTheme = info;
callback(null); callback(null);
}); });
} }

View File

@ -320,7 +320,7 @@
}, },
"TM3" : { "TM3" : {
"items" : [ "Yarly", "Nowaii" ], "items" : [ "Yarly", "Nowaii" ],
"styleSGR1" : { "fg" : 30, "intensity" : 1 }, "styleSGR1" : "|00|30|01",
"hotkeys" : { "Y" : 0, "N" : 1 } "hotkeys" : { "Y" : 0, "N" : 1 }
}, },
"BT8" : { "BT8" : {