UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

97 lines 3.73 kB
"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 widget_utilities_1 = __importDefault(require("../widget.utilities")); const termKit_utility_1 = __importDefault(require("./termKit.utility")); const TkBaseWidget_1 = __importDefault(require("./TkBaseWidget")); const termKit = terminal_kit_1.default; class TkTextWidget extends TkBaseWidget_1.default { type = 'text'; text; shouldAutoScrollWhenAppendingContent; constructor(options) { super(options); const { parent, text, isScrollEnabled: enableScroll = false, shouldAutoScrollWhenAppendingContent = true, ...rest } = options; this.shouldAutoScrollWhenAppendingContent = shouldAutoScrollWhenAppendingContent; const frame = termKit_utility_1.default.buildFrame(options, parent); this.text = new termKit.TextBox({ parent: parent ? parent.getTermKitElement() : undefined, scrollable: enableScroll, vScrollBar: enableScroll, hScrollBar: enableScroll && !rest.wordWrap, content: text, wordWrap: true, ...termKit_utility_1.default.mapWidgetOptionsToTermKitOptions(rest), ...frame, }); this.calculateSizeLockDeltas(); this.text.__widget = this; this.text.on('click', this.handleMouseDown.bind(this)); } async handleMouseDown(position) { const { x, y } = position; const line = this.text.content.split('\n')[y]; await this.emit('click', { text: line, row: y, column: x, }); } getTermKitElement() { return this.text; } setFrame(frame) { const oldFrame = this.getFrame(); const newFrame = widget_utilities_1.default.buildFrame(frame, this.parent); this.text.setSizeAndPosition({ x: newFrame.left ?? oldFrame.left, y: newFrame.top ?? oldFrame.top, width: newFrame.width ?? oldFrame.width, height: newFrame.height ?? oldFrame.height, }); this.text.draw(); } getText() { return this.text.content; } isLogScrolledAllTheWay() { const scrollDistance = this.getScrollY() * -1; const contentHeight = this.text.textBuffer.cy; const visibleHeight = this.text.textAreaHeight; const maxScrollDistance = Math.max(contentHeight, visibleHeight) - visibleHeight; const isScrolledAllTheWay = scrollDistance >= maxScrollDistance; return isScrolledAllTheWay; } getScrollY() { return this.text.scrollY; } getScrollX() { return this.text.scrollX; } setText(content) { if (this.getText() === content) { return; } const isScrolledAllTheWay = this.isLogScrolledAllTheWay(); const logSelection = this.text.textBuffer.selectionRegion; const markupType = this.markupType(content); this.text.setContent(content, markupType); if (logSelection) { this.text.textBuffer.setSelectionRegion(logSelection); } if (this.shouldAutoScrollWhenAppendingContent && isScrolledAllTheWay) { this.text.scrollToBottom(); } } markupType(content) { const match = /\x1b\[([0-9;]+)m|(.[^\x1b]+)/g.exec(content); const markupType = match && match[1] ? 'ansi' : true; return markupType; } } exports.default = TkTextWidget; //# sourceMappingURL=TkTextWidget.js.map