Spinner Menu now supports itemFormat and focusItemFormat

This commit is contained in:
Bryan Ashby 2018-11-12 22:05:21 -07:00
parent 14095d8f03
commit 516116f83e
1 changed files with 24 additions and 28 deletions

View File

@ -1,9 +1,11 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
const MenuView = require('./menu_view.js').MenuView; const MenuView = require('./menu_view.js').MenuView;
const ansi = require('./ansi_term.js'); const ansi = require('./ansi_term.js');
const strUtil = require('./string_util.js'); const strUtil = require('./string_util.js');
const { pipeToAnsi } = require('./color_codes.js');
const formatString = require('./string_format');
const util = require('util'); const util = require('util');
const assert = require('assert'); const assert = require('assert');
@ -36,40 +38,47 @@ function SpinnerMenuView(options) {
this.emit('index update', this.focusedItemIndex); this.emit('index update', this.focusedItemIndex);
}; };
this.drawItem = function() { this.drawItem = function(index) {
var item = self.items[this.focusedItemIndex]; const item = this.items[index];
if(!item) { if(!item) {
return; return;
} }
this.client.term.write(ansi.goto(this.position.row, this.position.col)); const cached = this.getRenderCacheItem(index, this.hasFocus);
this.client.term.write(self.hasFocus ? self.getFocusSGR() : self.getSGR()); if(cached) {
return this.client.term.write(`${ansi.goto(this.position.row, this.position.col)}${cached}`);
}
var text = strUtil.stylizeString(item.text, item.focused ? self.focusTextStyle : self.textStyle); let text;
let sgr;
if(this.complexItems) {
text = pipeToAnsi(formatString(this.hasFocus && this.focusItemFormat ? this.focusItemFormat : this.itemFormat, item));
sgr = this.focusItemFormat ? '' : (this.hasFocus ? this.getFocusSGR() : self.getSGR());
} else {
text = strUtil.stylizeString(item.text, this.hasFocus ? self.focusTextStyle : self.textStyle);
sgr = this.hasFocus ? this.getFocusSGR() : this.getSGR();
}
self.client.term.write( text = `${sgr}${strUtil.pad(text, this.dimens.width, this.fillChar, this.justify)}`;
strUtil.pad(text, this.dimens.width + 1, this.fillChar, this.justify)); this.client.term.write(`${ansi.goto(this.position.row, this.position.col)}${text}`);
}; this.setRenderCacheItem(index, text, this.hasFocus);
}
} }
util.inherits(SpinnerMenuView, MenuView); util.inherits(SpinnerMenuView, MenuView);
SpinnerMenuView.prototype.redraw = function() { SpinnerMenuView.prototype.redraw = function() {
SpinnerMenuView.super_.prototype.redraw.call(this); SpinnerMenuView.super_.prototype.redraw.call(this);
//this.cachePositions();
this.drawItem(this.focusedItemIndex); this.drawItem(this.focusedItemIndex);
}; };
SpinnerMenuView.prototype.setFocus = function(focused) { SpinnerMenuView.prototype.setFocus = function(focused) {
SpinnerMenuView.super_.prototype.setFocus.call(this, focused); SpinnerMenuView.super_.prototype.setFocus.call(this, focused);
this.redraw(); this.redraw();
}; };
SpinnerMenuView.prototype.setFocusItemIndex = function(index) { SpinnerMenuView.prototype.setFocusItemIndex = function(index) {
SpinnerMenuView.super_.prototype.setFocusItemIndex.call(this, index); // sets this.focusedItemIndex SpinnerMenuView.super_.prototype.setFocusItemIndex.call(this, index); // sets this.focusedItemIndex
this.updateSelection(); // will redraw this.updateSelection(); // will redraw
}; };
@ -103,16 +112,3 @@ SpinnerMenuView.prototype.getData = function() {
const item = this.getItem(this.focusedItemIndex); const item = this.getItem(this.focusedItemIndex);
return _.isString(item.data) ? item.data : this.focusedItemIndex; return _.isString(item.data) ? item.data : this.focusedItemIndex;
}; };
SpinnerMenuView.prototype.setItems = function(items) {
SpinnerMenuView.super_.prototype.setItems.call(this, items);
var longest = 0;
for(var i = 0; i < this.items.length; ++i) {
if(longest < this.items[i].text.length) {
longest = this.items[i].text.length;
}
}
this.dimens.width = longest;
};