* Lots of code cleanup

* New standard MCI codes for labels
* WIP MaskEditTextView
* Extra styles for EditTextView
This commit is contained in:
Bryan Ashby 2015-05-14 23:01:00 -06:00
parent 1a1dd53ca1
commit a96af34a20
12 changed files with 150 additions and 62 deletions

View File

@ -169,8 +169,6 @@ function startListening() {
});
client.on('end', function onClientEnd() {
logger.log.info({ clientId : client.runtime.id }, 'Client disconnected');
removeClient(client);
});
@ -192,7 +190,20 @@ function startListening() {
function addNewClient(client) {
var id = client.runtime.id = clientConnections.push(client) - 1;
logger.log.debug('Connection count is now %d', clientConnections.length);
var connInfo = {
connectionCount : clientConnections.length,
clientId : client.runtime.id,
};
if(logger.log.debug()) {
connInfo.address = client.address();
} else {
connInfo.ip = client.address().address;
}
logger.log.info(connInfo, 'Client connected');
return id;
}
@ -200,7 +211,14 @@ function removeClient(client) {
var i = clientConnections.indexOf(client);
if(i > -1) {
clientConnections.splice(i, 1);
logger.log.debug('Connection count is now %d', clientConnections.length);
logger.log.info(
{
connectionCount : clientConnections.length,
clientId : client.runtime.id
},
'Client disconnected'
);
}
}

View File

@ -79,7 +79,7 @@ function getDefaultConfig() {
users : {
usernameMin : 2,
usernameMax : 22,
usernameMax : 16,
usernamePattern : '^[A-Za-z0-9~!@#$%^&*()\\-\\_+]+$',
passwordMin : 6,
passwordMax : 128,

View File

@ -23,7 +23,8 @@ function EditTextView(options) {
this.cursorPos = { x : 0 };
this.clientBackspace = function() {
this.client.term.write('\b' + this.getSGR() + this.fillChar + '\b' + this.getFocusSGR());
var fillCharSGR = this.getStyleSGR(1) || this.getSGR();
this.client.term.write('\b' + fillCharSGR + this.fillChar + '\b' + this.getFocusSGR());
};
}

View File

@ -7,29 +7,54 @@ var miscUtil = require('./misc_util.js');
var util = require('util');
var assert = require('assert');
function MaskEditTextView(client, options) {
exports.MaskEditTextView = MaskEditTextView;
// ##/##/#### <--styleSGR2 if fillChar
// ^- styleSGR1
// buildPattern -> [ RE, RE, '/', RE, RE, '/', RE, RE, RE, RE ]
// patternIndex -----^
function MaskEditTextView(options) {
options.acceptsFocus = miscUtil.valueWithDefault(options.acceptsFocus, true);
options.acceptsInput = miscUtil.valueWithDefault(options.acceptsInput, true);
options.cursorStyle = miscUtil.valueWithDefault(options.cursorStyle, 'steady block');
options.resizable = false;
TextView.call(this, client, options);
TextView.call(this, options);
this.cursorPos = { x : 0 };
var self = this;
this.mask = options.mask || '';
this.maskPattern = options.maskPattern || '';
this.buildPattern = function(pattern) {
this.patternArray = [];
for(var i = 0; i < pattern.length; i++) {
if(pattern[i] in MaskEditTextView.maskPatternCharacterRegEx) {
this.patternArray.push(MaskEditTextView.maskPatternCharacterRegEx[pattern[i]]);
} else {
this.patternArray.push(pattern[i]);
}
}
console.log(this.patternArray)
};
this.buildPattern(this.maskPattern);
}
util.inherits(MaskEditTextView, TextView);
MaskEditTextView.MaskCharacterRegEx = {
MaskEditTextView.maskPatternCharacterRegEx = {
'#' : /[0-9]/,
'?' : /[a-zA-Z]/,
'&' : /[\w\d\s]/, // 32-126, 128-255
'A' : /[0-9a-zA-Z]/,
};
MaskEditTextView.prototype.setMask = function(mask) {
this.mask = mask;
MaskEditTextView.prototype.setMaskPattern = function(pattern) {
this.buildPattern(pattern);
};
MaskEditTextView.prototype.onKeyPress = function(key, isSpecial) {
@ -39,6 +64,25 @@ MaskEditTextView.prototype.onKeyPress = function(key, isSpecial) {
assert(1 === key.length);
if(this.text.length < this.maxLength) {
key = strUtil.stylizeString(key, this.textStyle);
/*this.text += key;
if(this.text.length > this.dimens.width) {
// no shortcuts - redraw the view
this.redraw();
} else {
this.cursorPos.x += 1;
if(this.maskPatternChar) {
this.client.term.write(this.maskPatternChar);
} else {
this.client.term.write(key);
}
}
*/
}
MaskEditTextView.super_.prototype.onKeyPress(this, key, isSpecial);

View File

@ -7,6 +7,7 @@ var ButtonView = require('./button_view.js').ButtonView;
var VerticalMenuView = require('./vertical_menu_view.js').VerticalMenuView;
var SpinnerMenuView = require('./spinner_menu_view.js').SpinnerMenuView;
var ToggleMenuView = require('./toggle_menu_view.js').ToggleMenuView;
var MaskEditTextView = require('./mask_edit_text_view.js').MaskEditTextView;
var Config = require('./config.js').config;
var ansi = require('./ansi_term.js');
@ -23,32 +24,37 @@ function MCIViewFactory(client) {
}
MCIViewFactory.prototype.getPredefinedViewLabel = function(code) {
var label;
switch(code) {
// :TODO: Fix conflict with ButtonView (BN); chagne to BT
case 'BN' : label = Config.general.boardName; break;
case 'VL' : label = 'ENiGMA½ v' + packageJson.version; break;
case 'VN' : label = packageJson.version; break;
case 'UN' : label = this.client.user.username; break;
case 'UR' : label = this.client.user.properties.real_name; break;
case 'LO' : label = this.client.user.properties.location; break;
return {
BN : Config.general.boardName,
VL : 'ENiGMA½ v' + packageJson.version,
VN : packageJson.version,
case 'OS' :
switch(os.platform()) {
case 'linux' : label = 'Linux'; break;
case 'darwin' : label = 'OS X'; break;
case 'win32' : label = 'Windows'; break;
case 'sunos' : label = 'SunOS'; break;
default : label = os.type(); break;
}
break;
UN : this.client.user.username,
UI : this.client.user.userId,
UG : _.values(this.client.user.groups).join(', '),
UR : this.client.user.properties.real_name,
LO : this.client.user.properties.location,
UA : this.client.user.properties.age,
US : this.client.user.properties.sex,
UE : this.client.user.properties.email_address,
UW : this.client.user.properties.web_address,
UF : this.client.user.properties.affiliation,
UT : this.client.user.properties.theme_id,
case 'OA' : label = os.arch(); break;
case 'SC' : label = os.cpus()[0].model; break;
}
OS : {
linux : 'Linux',
darwin : 'Mac OS X',
win32 : 'Windows',
sunos : 'SunOS',
freebsd : 'FreeBSD',
}[os.platform()] || os.type(),
return label;
OA : os.arch(),
SC : os.cpus()[0].model,
IP : this.client.address().address,
}[code];
};
MCIViewFactory.prototype.createFromMCI = function(mci) {
@ -111,6 +117,14 @@ MCIViewFactory.prototype.createFromMCI = function(mci) {
view = new EditTextView(options);
break;
// Masked Edit Text
case 'ME' :
setOption(0, 'textStyle');
setFocusOption(0, 'focusTextStyle');
view = new MaskEditTextView(options);
break;
// Pre-defined Label (Text View)
case 'PL' :
if(mci.args.length > 0) {

View File

@ -80,11 +80,11 @@ function MenuModule(options) {
mciData.menu = mciMap;
callback(err);
});
} else {
} else {
callback(null);
}
},
function moveToPromptLocation(callback) {
function moveToPromptLocation(callback) {
if(self.menuConfig.prompt) {
// :TODO: fetch and move cursor to prompt location, if supplied. See notes/etc. on placements
}
@ -106,7 +106,7 @@ function MenuModule(options) {
mciData.prompt = mciMap;
callback(err);
});
} else {
} else {
callback(null);
}
},

View File

@ -80,7 +80,7 @@ function TextView(options) {
this.fillChar,
this.justify,
this.hasFocus ? this.getFocusSGR() : this.getSGR(),
this.getSGR() // :TODO: use extended style color if avail
this.getStyleSGR(1) || this.getSGR()
));
};

View File

@ -191,6 +191,11 @@ View.prototype.getSGR = function() {
return this.ansiSGR;
};
View.prototype.getStyleSGR = function(x) {
assert(_.isNumber(x));
return this['styleSGR' + x];
}
View.prototype.getFocusSGR = function() {
return this.ansiFocusSGR;
};

View File

@ -189,6 +189,8 @@ function ViewController(options) {
setViewProp('textMaskChar', function(v) { view.textMaskChar = v.substr(0, 1); });
setViewProp('justify');
setViewProp('textOverflow');
setViewProp('maskPattern', function(v) { view.setMaskPattern(v); });
setViewProp('maxLength');

Binary file not shown.

View File

@ -10,12 +10,7 @@
// @method:scriptName[.js]/methodName (foreign .js)
// @art:artName
// @method:/methodName (local to module.js)
// ... pass isFocused/etc. into draw method
"draw" : {
"normal" : ...,
"focus" : ...
}
// ... pass isFocused/etc. into draw method
NOte that @draw & @art should check theme first.
@draw:myMethod -> theme/draw.js::myMethod(opts)
@ -57,7 +52,6 @@
"focus" : true,
// :TODO: need a good way to localize these ... Standard Orig->Lookup seems good.
"items" : [ "Login", "Apply", "Log Off" ]//,
//"itemSpacing" : 1
}
},
"submit" : {
@ -116,21 +110,14 @@
"form" : {
"0" : {
"BT12BT13ET1ET10ET2ET3ET4ET5ET6ET7ET8ET9TL11" : {
//
// :TODO: defaults { width : XX, ... } kinda thing would be nice
//
// "beforeViewsDraw" : "@method:location.js/myBeforeViewsDraw" -> myBeforeViewsDraw(views)
"mci" : {
"ET1" : {
"focus" : true,
"argName" : "username",
//"width" : 15,
"maxLength" : "@config:users.usernameMax"
},
"ET2" : {
"width" : 15,
"argName" : "realName",
//"width" : 15,
"maxLength" : 32
},
"ET3" : {
@ -145,34 +132,28 @@
},
"ET5" : {
"argName" : "location",
//"width" : 15,
"maxLength" : 32
},
"ET6" : {
"argName" : "affils",
//"width" : 15,
"maxLength" : 32
},
"ET7" : {
"argName" : "email",
//"width" : 15,
"maxLength" : 255
},
"ET8" : {
"argName" : "web",
//"width" : 15,
"maxLength" : 255
},
"ET9" : {
"argName" : "password",
"password" : true,
//"width" : 15,
"maxLength" : "@config:users.passwordMax"
},
"ET10" : {
"argName" : "passwordConfirm",
"password" : true,
//"width" : 15,
"maxLength" : "@config:users.passwordMax"
},
"BT12" : {
@ -210,8 +191,8 @@
}
},
"newUserActive" : {
"art" : "STATS",
"prompt" : "pause",
"art" : "userstats",
//"prompt" : "pause",
"options" : {
// :TODO: implement MCI codes for this
"cls" : true
@ -228,6 +209,7 @@
"items" : [
"Single Line Text Editing Views",
"Spinner & Toggle Views",
"Mask Edit Views",
"Vertical Menu Views",
"Horizontal Menu Views",
"Art Display",
@ -252,6 +234,10 @@
},
{
"value" : { "1" : 2 },
"action" : "@menu:demoMaskEditView"
},
{
"value" : { "1" : 5 },
"action" : "@menu:demoArtDisplay"
}
]
@ -279,7 +265,7 @@
"ET3" : {
"width" : 20,
"fillChar" : "-",
// :TODO: fillColor
"styleSGR1" : "|00|36",
"maxLength" : 20
},
"ET4" : {
@ -340,6 +326,21 @@
}
}
},
"demoMaskEditView" : {
"art" : "demo_mask_edit_text_view1.ans",
"options" : { "cls" : true },
"form" : {
"0" : {
"BT5ME1ME2" : {
"mci" : {
"ME1" : {
"maskPattern" : "##/##/##"
}
}
}
}
}
},
"demoArtDisplay" : {
"art" : "demo_selection_vm.ans",
"options" : { "cls" : true },

View File

@ -17,7 +17,10 @@
"menus" : {
"matrix" : {
"VM1" : {
"itemSpacing" : 1
"itemSpacing" : 1,
"justify" : "center",
"width" : 12,
"focusTextStyle" : "l33t"
}
},
"apply" : {