* Start work on allowing pipe codes & custom drawing using items vs focusItems for menus. EXPERIMENTAL.

This commit is contained in:
Bryan Ashby
2015-08-25 23:17:09 -06:00
parent 64c8d83559
commit 9442760679
10 changed files with 171 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ var assert = require('assert');
exports.pipeToAnsi = exports.enigmaToAnsi = enigmaToAnsi;
exports.stripPipeCodes = exports.stripEnigmaCodes = stripEnigmaCodes;
exports.pipeStrLen = exports.enigmaStrLen = enigmaStrLen;
exports.renegadeToAnsi = renegadeToAnsi;
// :TODO: Not really happy with the module name of "color_codes". Would like something better
@@ -63,6 +64,14 @@ function enigmaToAnsi(s) {
return result;
}
function stripEnigmaCodes(s) {
return s.replace(/\|[\d]{2}/g, '');
}
function enigmaStrLen(s) {
return stripEnigmaCodes(s).length;
}
function renegadeToAnsi(s) {
if(-1 == s.indexOf('|')) {
return s; // no pipe codes present
@@ -124,8 +133,3 @@ if(-1 == s.indexOf('|')) {
return result;
}
function stripEnigmaCodes(s) {
return s.replace(/\|[\d]{2}/g, '');
}

View File

@@ -23,7 +23,7 @@ function Door(client, exeInfo) {
// exeInfo.cwd
// exeInfo.encoding
};
}
require('util').inherits(Door, events.EventEmitter);

View File

@@ -40,6 +40,10 @@ function MenuView(options) {
this.fillChar = miscUtil.valueWithDefault(options.fillChar, ' ').substr(0, 1);
this.justify = options.justify || 'none';
this.hasFocusItems = function() {
return !_.isUndefined(self.focusItems);
};
}
util.inherits(MenuView, View);
@@ -48,14 +52,23 @@ MenuView.prototype.setItems = function(items) {
var self = this;
if(items) {
this.items = []; // :TODO: better way?
items.forEach(function onItem(itemText) {
self.items.push({
text : itemText
});
items.forEach(function item(itemText) {
self.items.push( { text : itemText } );
});
}
};
MenuView.prototype.setFocusItems = function(items) {
var self = this;
if(items) {
this.focusItems = [];
items.forEach(function item(itemText) {
self.focusItems.push( { text : itemText } );
});
}
}
MenuView.prototype.setItemSpacing = function(itemSpacing) {
itemSpacing = parseInt(itemSpacing);
assert(_.isNumber(itemSpacing));
@@ -68,6 +81,7 @@ MenuView.prototype.setPropertyValue = function(propName, value) {
switch(propName) {
case 'itemSpacing' : this.setItemSpacing(value); break;
case 'items' : this.setItems(value); break;
case 'focusItems' : this.setFocusItems(value); break;
case 'hotKeys' : this.setHotKeys(value); break;
}

View File

@@ -128,7 +128,7 @@ function logoff(callingMenu, formData, extraArgs) {
//
client.term.write(
ansi.normal() + '\n' +
require('crypto').randomBytes(Math.floor(Math.random() * 25) + 5).toString() +
require('crypto').randomBytes(Math.floor(Math.random() * 35) + 10).toString(client.term.outputEncoding) +
'NO CARRIER');
client.end();

View File

@@ -5,6 +5,7 @@ var MenuView = require('./menu_view.js').MenuView;
var ansi = require('./ansi_term.js');
var strUtil = require('./string_util.js');
var miscUtil = require('./misc_util.js');
var colorCodes = require('./color_codes.js');
var util = require('util');
var assert = require('assert');
@@ -48,6 +49,7 @@ function VerticalMenuView(options) {
};
};
/*
this.drawItem = function(index) {
var item = self.items[index];
if(!item) {
@@ -61,6 +63,47 @@ function VerticalMenuView(options) {
strUtil.pad(text, this.dimens.width, this.fillChar, this.justify)
);
};
*/
this.drawItem = function(index) {
var item = self.items[index];
if(!item) {
return;
}
var focusItem;
var text;
if(self.hasFocusItems()) {
focusItem = self.focusItems[index];
}
if(focusItem) {
if(item.focused) {
text = strUtil.stylizeString(focusItem.text, self.focusTextStyle);
} else {
text = strUtil.stylizeString(item.text, self.textStyle);
}
// :TODO: Need to support pad()
// :TODO: shoudl we detect if pipe codes are used?
self.client.term.write(
ansi.goto(item.row, self.position.col) +
colorCodes.enigmaToAnsi(text)
);
} else {
text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);
self.client.term.write(
ansi.goto(item.row, self.position.col) +
(index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()) +
strUtil.pad(text, this.dimens.width, this.fillChar, this.justify)
);
}
};
}
util.inherits(VerticalMenuView, MenuView);
@@ -161,6 +204,12 @@ VerticalMenuView.prototype.setItems = function(items) {
this.positionCacheExpired = true;
};
VerticalMenuView.prototype.setFocusItems = function(items) {
VerticalMenuView.super_.prototype.setFocusItems.call(this, items);
this.positionCacheExpired = true;
}
VerticalMenuView.prototype.setItemSpacing = function(itemSpacing) {
VerticalMenuView.super_.prototype.setItemSpacing.call(this, itemSpacing);