Menu items can now be arrays of objects

* Allows custom members of each item
* 'data' overrides selection (vs returning the index)
* 'text' is the default member for text if no formatters are supplied
* formatters: 'itemFormat' and 'focusItemFormat', e.g. "{member1} - {member2}"
This commit is contained in:
Bryan Ashby 2018-01-27 22:21:48 -07:00
parent 974ee1b389
commit 7a2df56855
4 changed files with 77 additions and 31 deletions

View file

@ -1,12 +1,14 @@
/* jslint node: true */
'use strict';
var MenuView = require('./menu_view.js').MenuView;
var ansi = require('./ansi_term.js');
var strUtil = require('./string_util.js');
const MenuView = require('./menu_view.js').MenuView;
const strUtil = require('./string_util.js');
const formatString = require('./string_format');
const { pipeToAnsi } = require('./color_codes.js');
const { goto } = require('./ansi_term.js');
var assert = require('assert');
var _ = require('lodash');
const assert = require('assert');
const _ = require('lodash');
exports.HorizontalMenuView = HorizontalMenuView;
@ -57,21 +59,29 @@ function HorizontalMenuView(options) {
this.drawItem = function(index) {
assert(!this.positionCacheExpired);
var item = self.items[index];
const item = self.items[index];
if(!item) {
return;
}
var text = strUtil.stylizeString(
item.text,
this.hasFocus && item.focused ? self.focusTextStyle : self.textStyle);
let text;
let sgr;
if(item.focused && self.hasFocusItems()) {
const focusItem = self.focusItems[index];
text = focusItem ? focusItem.text : item.text;
sgr = '';
} else if(this.complexItems) {
text = pipeToAnsi(formatString(item.focused ? this.focusItemFormat : this.itemFormat, item));
sgr = this.focusItemFormat ? '' : (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
} else {
text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle);
sgr = (index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR());
}
var drawWidth = text.length + self.getSpacer().length * 2; // * 2 = sides
const drawWidth = strUtil.renderStringLength(text) + (self.getSpacer().length * 2);
self.client.term.write(
ansi.goto(self.position.row, item.col) +
(index === self.focusedItemIndex ? self.getFocusSGR() : self.getSGR()) +
strUtil.pad(text, drawWidth, self.fillChar, 'center')
`${goto(self.position.row, item.col)}${sgr}${strUtil.pad(text, drawWidth, self.fillChar, 'center')}`
);
};
}