* 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');