@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
97 lines • 3.11 kB
JavaScript
"use strict";
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 TkBaseWidget_1 = __importDefault(require("./TkBaseWidget"));
const termKit = terminal_kit_1.default;
class TkWindowWidget extends TkBaseWidget_1.default {
type = 'window';
document;
constructor(options) {
super(options);
//@ts-ignore
this.document = this.term.createDocument({
palette: new termKit.Palette(),
});
this.document.eventSource.on('resize', async () => {
this.handleParentResize();
const frame = this.getFrame();
await this.emit('resize', {
width: frame.width,
height: frame.height,
});
});
this.document.__widget = this;
options.term.on('key', this.handleKeyPress.bind(this));
process.on('exit', (code) => {
void this.emit('kill', { code });
});
process.on('SIGQUIT', (code) => {
void this.emit('kill', { code });
});
process.on('kill', (code) => {
void this.emit('kill', { code });
});
process.on('SIGINT', (code) => {
void this.emit('kill', { code });
});
process.on('SIGTERM', (code) => {
void this.emit('kill', { code });
});
process.on('SIGUSR1', (code) => {
void this.emit('kill', { code });
});
process.on('SIGUSR2', (code) => {
void this.emit('kill', { code });
});
process.on('uncaughtException', (code) => {
void this.emit('kill', { code });
});
}
getFocusedWidget() {
return this.document.focusElement.__widget;
}
async handleKeyPress(key) {
await this.emit('key', { key });
}
handleParentResize() {
this.sizeLockedChildren();
}
setTitle(title) {
this.term.windowTitle(title);
}
hideCursor() {
this.term.hideCursor(true);
}
showCursor() {
this.term.hideCursor(false);
}
getFrame() {
return {
left: 0,
top: 0,
width: this.document.inputWidth,
height: this.document.inputHeight,
};
}
getTermKitElement() {
return this.document;
}
async destroy() {
this.showCursor();
//@ts-ignore
this.term.removeAllListeners();
this.term.styleReset();
await this.term.grabInput(false, true);
// termkit destroys all children, so we don't need to call for each children
// this may change if destroy is required in other widgets and there appears
// to be no penalty for calling destroy multiple times
this.document.destroy();
this.term.clear();
this.term(`\n`);
}
}
exports.default = TkWindowWidget;
//# sourceMappingURL=TkWindowWidget.js.map