Cleaned up issues with length

This commit is contained in:
Nathan Byrd 2022-01-19 13:07:02 -06:00
parent 4fe55e1e1b
commit d77de9c388
2 changed files with 47 additions and 12 deletions

View File

@ -59,7 +59,7 @@ function FullMenuView(options) {
this.clearPage = function() { this.clearPage = function() {
for (var i = 0; i < this.itemsPerRow; i++) { for (var i = 0; i < this.dimens.height; i++) {
let text = `${strUtil.pad(' ', this.dimens.width, this.fillChar, 'left')}`; let text = `${strUtil.pad(' ', this.dimens.width, this.fillChar, 'left')}`;
self.client.term.write(`${ansi.goto(this.position.row + i, this.position.col)}${text}`); self.client.term.write(`${ansi.goto(this.position.row + i, this.position.col)}${text}`);
} }
@ -88,11 +88,7 @@ function FullMenuView(options) {
var spacer = this.getSpacer(); var spacer = this.getSpacer();
var itemInRow = 0; var itemInRow = 0;
var itemInCol = 0;
this.viewWindow = {
start: this.focusedItemIndex,
end: this.items.length - 1, // this may be adjusted later
};
var pageStart = 0; var pageStart = 0;
@ -100,6 +96,7 @@ function FullMenuView(options) {
itemInRow++; itemInRow++;
this.items[i].row = row; this.items[i].row = row;
this.items[i].col = col; this.items[i].col = col;
this.items[i].itemInRow = itemInRow;
row += this.itemSpacing + 1; row += this.itemSpacing + 1;
@ -126,7 +123,8 @@ function FullMenuView(options) {
// Check if we have room for this column // Check if we have room for this column
if (col + maxLength + spacer.length + 1 > this.position.col + this.dimens.width) { // skip for column 0, we need at least one
if (itemInCol != 0 && (col + maxLength + spacer.length + 1 > this.position.col + this.dimens.width)) {
// save previous page // save previous page
this.pages.push({ start: pageStart, end: i - this.itemsPerRow }); this.pages.push({ start: pageStart, end: i - this.itemsPerRow });
@ -166,7 +164,8 @@ function FullMenuView(options) {
} }
// Check if we have room for this column in the current page // Check if we have room for this column in the current page
if (col + maxLength > this.position.col + this.dimens.width) { // skip for first column, we need at least one
if (itemInCol != 0 && (col + maxLength > this.position.col + this.dimens.width)) {
// save previous page // save previous page
this.pages.push({ start: pageStart, end: i - this.itemsPerRow }); this.pages.push({ start: pageStart, end: i - this.itemsPerRow });
@ -183,10 +182,14 @@ function FullMenuView(options) {
} }
} }
// increment the column // increment the column
col += maxLength + spacer.length + 1; col += maxLength + spacer.length + 1;
itemInCol++;
} }
@ -225,6 +228,11 @@ function FullMenuView(options) {
sgr = (index === this.focusedItemIndex ? this.getFocusSGR() : this.getSGR()); sgr = (index === this.focusedItemIndex ? this.getFocusSGR() : this.getSGR());
} }
let renderLength = strUtil.renderStringLength(text);
if (this.hasTextOverflow() && (item.col + renderLength) > this.dimens.width) {
text = strUtil.renderSubstr(text, 0, this.dimens.width - (item.col + this.textOverflow.length)) + this.textOverflow;
}
text = `${sgr}${strUtil.pad(text, this.fixedLength, this.fillChar, this.justify)}`; text = `${sgr}${strUtil.pad(text, this.fixedLength, this.fillChar, this.justify)}`;
this.client.term.write(`${ansi.goto(item.row, item.col)}${text}`); this.client.term.write(`${ansi.goto(item.row, item.col)}${text}`);
this.setRenderCacheItem(index, text, item.focused); this.setRenderCacheItem(index, text, item.focused);
@ -277,6 +285,13 @@ FullMenuView.prototype.setWidth = function(width) {
this.positionCacheExpired = true; this.positionCacheExpired = true;
}; };
FullMenuView.prototype.setTextOverflow = function(overflow) {
FullMenuView.super_.prototype.setTextOverflow.call(this, overflow);
this.positionCacheExpired = true;
}
FullMenuView.prototype.setPosition = function(pos) { FullMenuView.prototype.setPosition = function(pos) {
FullMenuView.super_.prototype.setPosition.call(this, pos); FullMenuView.super_.prototype.setPosition.call(this, pos);
@ -366,13 +381,14 @@ FullMenuView.prototype.focusNext = function() {
FullMenuView.prototype.focusPrevious = function() { FullMenuView.prototype.focusPrevious = function() {
if (0 === this.focusedItemIndex) { if (0 === this.focusedItemIndex) {
this.clearPage();
this.focusedItemIndex = this.items.length - 1; this.focusedItemIndex = this.items.length - 1;
this.currentPage = this.pages.length - 1; this.currentPage = this.pages.length - 1;
} }
else { else {
this.clearPage();
this.focusedItemIndex--; this.focusedItemIndex--;
if (this.focusedItemIndex < this.pages[this.currentPage].start) { if (this.focusedItemIndex < this.pages[this.currentPage].start) {
this.clearPage();
this.currentPage--; this.currentPage--;
} }
} }
@ -384,11 +400,20 @@ FullMenuView.prototype.focusPrevious = function() {
FullMenuView.prototype.focusPreviousColumn = function() { FullMenuView.prototype.focusPreviousColumn = function() {
var currentRow = this.items[this.focusedItemIndex].itemInRow;
this.focusedItemIndex = this.focusedItemIndex - this.itemsPerRow; this.focusedItemIndex = this.focusedItemIndex - this.itemsPerRow;
if (this.focusedItemIndex < 0) { if (this.focusedItemIndex < 0) {
this.clearPage(); this.clearPage();
var lastItemRow = this.items[this.items.length - 1].itemInRow;
if (lastItemRow > currentRow) {
this.focusedItemIndex = this.items.length - (lastItemRow - currentRow) - 1;
}
else {
// can't go to same column, so go to last item
this.focusedItemIndex = this.items.length - 1;
}
// add the negative index to the end of the list // add the negative index to the end of the list
this.focusedItemIndex = this.items.length + this.focusedItemIndex; // this.focusedItemIndex = this.items.length + this.focusedItemIndex;
// set to last page // set to last page
this.currentPage = this.pages.length - 1; this.currentPage = this.pages.length - 1;
} }
@ -407,10 +432,10 @@ FullMenuView.prototype.focusPreviousColumn = function() {
FullMenuView.prototype.focusNextColumn = function() { FullMenuView.prototype.focusNextColumn = function() {
var currentRow = this.items[this.focusedItemIndex].itemInRow;
this.focusedItemIndex = this.focusedItemIndex + this.itemsPerRow; this.focusedItemIndex = this.focusedItemIndex + this.itemsPerRow;
if (this.focusedItemIndex > this.items.length - 1) { if (this.focusedItemIndex > this.items.length - 1) {
// add the overflow to the beginning of the list this.focusedItemIndex = currentRow - 1;
this.focusedItemIndex = this.focusedItemIndex - this.items.length;
this.currentPage = 0; this.currentPage = 0;
this.clearPage(); this.clearPage();
} }

View File

@ -69,6 +69,15 @@ function MenuView(options) {
util.inherits(MenuView, View); util.inherits(MenuView, View);
MenuView.prototype.setTextOverflow = function(overflow) {
this.textOverflow = overflow;
this.invalidateRenderCache();
}
MenuView.prototype.hasTextOverflow = function() {
return this.textOverflow !== undefined;
}
MenuView.prototype.setItems = function(items) { MenuView.prototype.setItems = function(items) {
if (Array.isArray(items)) { if (Array.isArray(items)) {
this.sorted = false; this.sorted = false;
@ -269,6 +278,7 @@ MenuView.prototype.setPropertyValue = function(propName, value) {
case 'items': this.setItems(value); break; case 'items': this.setItems(value); break;
case 'focusItems': this.setFocusItems(value); break; case 'focusItems': this.setFocusItems(value); break;
case 'hotKeys': this.setHotKeys(value); break; case 'hotKeys': this.setHotKeys(value); break;
case 'textOverflow': this.setTextOverflow(value); break;
case 'hotKeySubmit': this.hotKeySubmit = value; break; case 'hotKeySubmit': this.hotKeySubmit = value; break;
case 'justify': this.justify = value; break; case 'justify': this.justify = value; break;
case 'focusItemIndex': this.focusedItemIndex = value; break; case 'focusItemIndex': this.focusedItemIndex = value; break;