UNPKG

@sprucelabs/spruce-cli

Version:

Command line interface for building Spruce skills.

128 lines 4.86 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); const normalizedContent = markupType === 'ansi' ? this.padAnsiSegments(content) : content; this.text.setContent(normalizedContent, markupType); if (logSelection) { this.text.textBuffer.setSelectionRegion(logSelection); } if (this.shouldAutoScrollWhenAppendingContent && isScrolledAllTheWay) { this.text.scrollToBottom(); } } markupType(content) { const match = /\x1b\[[0-9;]+m/.exec(content); const markupType = match ? 'ansi' : true; return markupType; } padAnsiSegments(content) { const sgrPattern = /\u001b\[[0-9;]+m/g; let result = ''; let lastIndex = 0; let match; while ((match = sgrPattern.exec(content))) { const segment = content.slice(lastIndex, match.index); if (segment) { result += this.padSegment(segment); } result += match[0]; lastIndex = match.index + match[0].length; } if (lastIndex < content.length) { result += this.padSegment(content.slice(lastIndex)); } return result; } padSegment(segment) { if (segment.length !== 1) { return segment; } if (segment.trim().length > 0) { // Terminal-kit drops single-character chunks when parsing ANSI // sequences, so pad with a zero-width space to keep visible // single characters (digits, braces, etc.) from disappearing. return `${segment}​`; } return segment; } } exports.default = TkTextWidget; //# sourceMappingURL=TkTextWidget.js.map