@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
77 lines • 2.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const terminal_kit_1 = __importDefault(require("terminal-kit"));
const termKit = terminal_kit_1.default;
const termKit_utility_1 = __importDefault(require("./termKit.utility"));
const TkBaseWidget_1 = __importDefault(require("./TkBaseWidget"));
class TkMenuBarWidget extends TkBaseWidget_1.default {
type = 'menuBar';
menu;
constructor(options) {
super(options);
const frame = termKit_utility_1.default.buildFrame(options, options.parent);
const items = this.mapItemsToTkItems(options.items);
this.menu = new termKit.DropDownMenu({
parent: options.parent.getTermKitElement(),
separator: '|',
items,
backgroundAttr: { bgColor: 'black', color: 'white' },
buttonBlurAttr: { bgColor: 'black', color: 'yellow' },
buttonFocusAttr: { bgColor: 'yellow', color: 'black' },
separatorAttr: { bgColor: 'black', color: 'yellow' },
...frame,
});
this.calculateSizeLockDeltas();
this.menu.__widget = this;
this.menu.on('submit', this.handleMenuSubmit.bind(this));
}
setTextForItem(value, text) {
let buttonItem = this.getButtonByValue(value, this.menu.buttons);
if (!buttonItem) {
for (const item of this.menu.itemsDef) {
for (const subItem of item.items ?? []) {
if (subItem.value === value) {
subItem.content = this.buildItemText(text);
return;
}
}
}
}
if (!buttonItem) {
throw new Error(`No menu item with value of ${value}`);
}
buttonItem.setContent(this.buildItemText(text), true);
}
getButtonByValue(value, buttons) {
let button;
for (const button of buttons) {
if (button.def.value === value) {
return button;
}
}
return button;
}
handleMenuSubmit(value) {
this.menu.clearColumnMenu();
void this.emit('select', { value });
}
getTermKitElement() {
return this.menu;
}
mapItemsToTkItems(items) {
return items.map((item) => ({
value: item.value,
content: this.buildItemText(item.label),
topSubmit: !item.items || item.items.length === 0,
items: item.items ? this.mapItemsToTkItems(item.items) : undefined,
}));
}
buildItemText(text) {
return ` ${text} `;
}
}
exports.default = TkMenuBarWidget;
//# sourceMappingURL=TkMenuBarWidget.js.map