* Did add a tweak to the concept for height only: autoAdjustHeight can be used where it makes sense

* See also: #159
This commit is contained in:
Bryan Ashby 2018-06-23 20:16:44 -06:00
parent 6d4b8abc9c
commit 611a52e946
4 changed files with 29 additions and 105 deletions

View File

@ -31,17 +31,6 @@ function HorizontalMenuView(options) {
return new Array(self.itemSpacing + 1).join(' ');
};
this.performAutoScale = function() {
if(self.autoScale.width) {
var spacer = self.getSpacer();
var width = self.items.join(spacer).length + (spacer.length * 2);
assert(width <= self.client.term.termWidth - self.position.col);
self.dimens.width = width;
}
};
this.performAutoScale();
this.cachePositions = function() {
if(this.positionCacheExpired) {
var col = self.position.col;

View File

@ -183,67 +183,23 @@ TextView.prototype.getData = function() {
TextView.prototype.setText = function(text, redraw) {
redraw = _.isBoolean(redraw) ? redraw : true;
if(!_.isString(text)) {
if(!_.isString(text)) { // allow |text| to be numbers/etc.
text = text.toString();
}
text = pipeToAnsi(stripAllLineFeeds(text), this.client); // expand MCI/etc.
var widthDelta = 0;
if(this.text && this.text !== text) {
widthDelta = Math.abs(renderStringLength(this.text) - renderStringLength(text));
}
this.text = text;
this.text = pipeToAnsi(stripAllLineFeeds(text), this.client); // expand MCI/etc.
if(this.maxLength > 0) {
this.text = renderSubstr(this.text, 0, this.maxLength);
//this.text = this.text.substr(0, this.maxLength);
}
// :TODO: it would be nice to be able to stylize strings with MCI and {special} MCI syntax, e.g. "|BN {UN!toUpper}"
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
if(this.autoScale.width) {
this.dimens.width = renderStringLength(this.text) + widthDelta;
}
if(redraw) {
this.redraw();
}
};
/*
TextView.prototype.setText = function(text) {
if(!_.isString(text)) {
text = text.toString();
}
var widthDelta = 0;
if(this.text && this.text !== text) {
widthDelta = Math.abs(this.text.length - text.length);
}
this.text = text;
if(this.maxLength > 0) {
this.text = this.text.substr(0, this.maxLength);
}
this.text = stylizeString(this.text, this.hasFocus ? this.focusTextStyle : this.textStyle);
//if(this.resizable) {
// this.dimens.width = this.text.length + widthDelta;
//}
if(this.autoScale.width) {
this.dimens.width = this.text.length + widthDelta;
}
this.redraw();
};
*/
TextView.prototype.clearText = function() {
this.setText('');
};

View File

@ -15,11 +15,13 @@ const _ = require('lodash');
exports.VerticalMenuView = VerticalMenuView;
function VerticalMenuView(options) {
options.cursor = options.cursor || 'hide';
options.justify = options.justify || 'left';
options.cursor = options.cursor || 'hide';
options.justify = options.justify || 'left';
MenuView.call(this, options);
this.dimens.width = this.dimens.width || Math.min(15, this.client.term.termWidth - this.position.col);
const self = this;
// we want page up/page down by default
@ -30,24 +32,14 @@ function VerticalMenuView(options) {
});
}
this.performAutoScale = function() {
if(this.autoScale.height) {
this.dimens.height = (self.items.length * (self.itemSpacing + 1)) - (self.itemSpacing);
this.dimens.height = Math.min(self.dimens.height, self.client.term.termHeight - self.position.row);
}
if(self.autoScale.width) {
let maxLen = 0;
self.items.forEach( item => {
if(item.text.length > maxLen) {
maxLen = Math.min(item.text.length, self.client.term.termWidth - self.position.col);
}
});
self.dimens.width = maxLen + 1;
this.autoAdjustHeightIfEnabled = function() {
if(this.autoAdjustHeight) {
this.dimens.height = (this.items.length * (this.itemSpacing + 1)) - (this.itemSpacing);
this.dimens.height = Math.min(this.dimens.height, this.client.term.termHeight - this.position.row);
}
};
this.performAutoScale();
this.autoAdjustHeightIfEnabled();
this.updateViewVisibleItems = function() {
self.maxVisibleItems = Math.ceil(self.dimens.height / (self.itemSpacing + 1));
@ -96,7 +88,7 @@ VerticalMenuView.prototype.redraw = function() {
// :TODO: rename positionCacheExpired to something that makese sense; combine methods for such
if(this.positionCacheExpired) {
this.performAutoScale();
this.autoAdjustHeightIfEnabled();
this.updateViewVisibleItems();
this.positionCacheExpired = false;
@ -133,6 +125,7 @@ VerticalMenuView.prototype.setHeight = function(height) {
VerticalMenuView.super_.prototype.setHeight.call(this, height);
this.positionCacheExpired = true;
this.autoAdjustHeight = false;
};
VerticalMenuView.prototype.setPosition = function(pos) {
@ -158,7 +151,7 @@ VerticalMenuView.prototype.setFocusItemIndex = function(index) {
};
this.positionCacheExpired = false; // skip standard behavior
this.performAutoScale();
this.autoAdjustHeightIfEnabled();
}
this.redraw();

View File

@ -37,21 +37,16 @@ function View(options) {
enigAssert(_.isObject(options));
enigAssert(_.isObject(options.client));
var self = this;
this.client = options.client;
this.cursor = options.cursor || 'show';
this.cursorStyle = options.cursorStyle || 'default';
this.client = options.client;
this.cursor = options.cursor || 'show';
this.cursorStyle = options.cursorStyle || 'default';
this.acceptsFocus = options.acceptsFocus || false;
this.acceptsInput = options.acceptsInput || false;
this.position = { x : 0, y : 0 };
this.dimens = { height : 1, width : 0 };
this.textStyle = options.textStyle || 'normal';
this.focusTextStyle = options.focusTextStyle || this.textStyle;
this.acceptsFocus = options.acceptsFocus || false;
this.acceptsInput = options.acceptsInput || false;
this.autoAdjustHeight = _.get(options, 'dimens.height') ? false : _.get(options, 'autoAdjustHeight', true);
this.position = { x : 0, y : 0 };
this.textStyle = options.textStyle || 'normal';
this.focusTextStyle = options.focusTextStyle || this.textStyle;
if(options.id) {
this.setId(options.id);
@ -61,15 +56,8 @@ function View(options) {
this.setPosition(options.position);
}
if(_.isObject(options.autoScale)) {
this.autoScale = options.autoScale;
} else {
this.autoScale = { height : true, width : true };
}
if(options.dimens) {
this.setDimension(options.dimens);
this.autoScale = { height : false, width : false };
} else {
this.dimens = {
width : options.width || 0,
@ -105,7 +93,7 @@ function View(options) {
};
this.hideCusor = function() {
self.client.term.rawWrite(ansi.hideCursor());
this.client.term.rawWrite(ansi.hideCursor());
};
this.restoreCursor = function() {
@ -148,25 +136,23 @@ View.prototype.setPosition = function(pos) {
View.prototype.setDimension = function(dimens) {
enigAssert(_.isObject(dimens) && _.isNumber(dimens.height) && _.isNumber(dimens.width));
this.dimens = dimens;
this.autoScale = { height : false, width : false };
this.dimens = dimens;
this.autoAdjustHeight = false;
};
View.prototype.setHeight = function(height) {
height = parseInt(height) || 1;
height = Math.min(height, this.client.term.termHeight);
this.dimens.height = height;
this.autoScale.height = false;
this.dimens.height = height;
this.autoAdjustHeight = false;
};
View.prototype.setWidth = function(width) {
width = parseInt(width) || 1;
width = Math.min(width, this.client.term.termWidth);
this.dimens.width = width;
this.autoScale.width = false;
this.dimens.width = width;
};
View.prototype.getSGR = function() {