UNPKG

dop-stick

Version:

Source control tooling for versionable-upgradeable smart contracts

328 lines 10.2 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Terminal = void 0; const readline_1 = __importDefault(require("readline")); class Terminal { /** * Output Methods */ static write(text, newLine = false, safe = true) { let processedText = text; if (safe) { if (this.capabilities.isCI) { processedText = this.stripAnsi(processedText); } if (this.capabilities.isWindows) { processedText = Buffer.from(processedText, 'utf8').toString('utf8'); } } try { process.stdout.write(processedText + (newLine ? '\n' : '')); } catch (error) { console.log(processedText); } } static clear() { this.write(this.CSI + '2J' + this.CSI + 'H'); } static eraseLine() { if (!process.stdout.isTTY) return; this.write(this.CSI + '2K'); this.cursorTo(0); } /** * Styling Methods */ static style(text, color) { if (!this.capabilities.hasColors) { return text; } if (Array.isArray(color)) { const combined = color.join(';'); return `${this.CSI}${combined}m${text}${this.CSI}0m`; } if (color.startsWith('#')) { return `${this.CSI}38;2;${this.hexToRgb(color)}m${text}${this.CSI}0m`; } // Handle ANSI color codes return `${color}${text}${this.colors.reset}`; } static stripAnsi(text) { return text.replace(this.ANSI_REGEX, ''); } /** * Cursor Control Methods */ static cursorTo(x, y) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${y !== null && y !== void 0 ? y : 0};${x}H`); } static cursorUp(count = 1) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${count}A`); } static cursorDown(count = 1) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${count}B`); } static cursorForward(count = 1) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${count}C`); } static cursorBackward(count = 1) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${count}D`); } static cursorNextLine(count = 1) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${count}E`); } static cursorPrevLine(count = 1) { if (!process.stdout.isTTY) return; this.write(this.CSI + `${count}F`); } static cursorHide() { if (!process.stdout.isTTY) return; this.write(this.CSI + '?25l'); } static cursorShow() { if (!process.stdout.isTTY) return; this.write(this.CSI + '?25h'); } static cursorSave() { if (!process.stdout.isTTY) return; this.write(this.CSI + 's'); } static cursorRestore() { if (!process.stdout.isTTY) return; this.write(this.CSI + 'u'); } /** * Utility Methods */ static getChars(type) { return this.capabilities.hasUnicode ? this.chars[type].unicode : this.chars[type].ascii; } static supports(feature) { const value = this.capabilities[feature]; return typeof value === 'boolean' ? value : Boolean(value); } static getWindowSize() { var _a; let columns = 80; let rows = 24; if (process.stdout.columns && process.stdout.rows) { columns = process.stdout.columns; rows = process.stdout.rows; } else if (process.stdout.isTTY) { try { const ttySize = (_a = process.stdout._handle) === null || _a === void 0 ? void 0 : _a.getWindowSize(); if (ttySize) { [rows, columns] = ttySize; } } catch (_b) { // Keep default values if tty method fails } } else { columns = parseInt(process.env.COLUMNS || '80', 10); rows = parseInt(process.env.ROWS || '24', 10); } return { columns: Math.max(40, Math.min(columns, 200)), rows: Math.max(10, Math.min(rows, 100)) }; } static getContentWidth(margin = 0) { const { columns } = this.getWindowSize(); return Math.max(40, columns - (margin * 2)); } static hexToRgb(hex) { const r = parseInt(hex.slice(1, 3), 16); const g = parseInt(hex.slice(3, 5), 16); const b = parseInt(hex.slice(5, 7), 16); return `${r};${g};${b}`; } static writeLine(text) { this.write(text, true, true); } /** * Move cursor to specific position */ static moveCursor(dx, dy) { process.stdout.moveCursor(dx, dy); } /** * Clear the current line */ static clearLine() { process.stdout.clearLine(0); } /** * Clear everything from cursor down */ static clearScreenDown() { process.stdout.clearScreenDown(); } /** * Save cursor position */ static saveCursor() { process.stdout.write('\u001B7'); } /** * Restore cursor position */ static restoreCursor() { process.stdout.write('\u001B8'); } /** * Move cursor to start of line */ static cursorToLineStart() { process.stdout.cursorTo(0); } /** * Prompts the user for input * @param question The prompt text to display * @returns Promise that resolves with user's input */ static async prompt(question) { const rl = readline_1.default.createInterface({ input: process.stdin, output: process.stdout }); try { return await new Promise((resolve) => { rl.question(question, (answer) => { resolve(answer); }); }); } finally { rl.close(); } } } exports.Terminal = Terminal; Terminal.ESC = '\x1b'; Terminal.CSI = Terminal.ESC + '['; Terminal.ANSI_REGEX = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; // ANSI Color codes Terminal.colors = { // Basic colors black: '\x1b[30m', red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m', magenta: '\x1b[35m', cyan: '\x1b[36m', white: '\x1b[37m', // Bright colors brightBlack: '\x1b[90m', brightRed: '\x1b[91m', brightGreen: '\x1b[92m', brightYellow: '\x1b[93m', brightBlue: '\x1b[94m', brightMagenta: '\x1b[95m', brightCyan: '\x1b[96m', brightWhite: '\x1b[97m', // Special reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m', italic: '\x1b[3m', underline: '\x1b[4m', // Semantic colors success: '\x1b[32m', error: '\x1b[31m', warning: '\x1b[33m', info: '\x1b[90m', muted: '\x1b[90m', default: '\x1b[0m', matrix: (text) => `\x1b[32m${text}\x1b[0m`, orange: '\x1b[38;5;208m', orangeBg: '\x1b[48;5;208m', // just dark orange background }; // Terminal capability detection Terminal.capabilities = { hasColors: Boolean(process.stdout.isTTY && process.env.TERM !== 'dumb'), isWindows: process.platform === 'win32', isCI: Boolean(process.env.CI), terminalType: process.env.TERM || '', hasUnicode: Boolean(process.platform !== 'win32' || process.env.TERM_PROGRAM === 'vscode'), hasHyperlinks: Boolean(process.env.TERM_PROGRAM === 'hyper' || process.env.TERM_PROGRAM === 'vscode'), colorDepth: (() => { var _a, _b; if (process.env.FORCE_COLOR) { return parseInt(process.env.FORCE_COLOR, 10) || 1; } if (process.stdout.isTTY) { return ((_b = (_a = process.stdout).getColorDepth) === null || _b === void 0 ? void 0 : _b.call(_a)) || 1; } return 1; })() }; // Fallback characters for different terminal types Terminal.chars = { box: { unicode: { topLeft: '╭', topRight: '╮', bottomLeft: '╰', bottomRight: '╯', horizontal: '─', vertical: '│' }, ascii: { topLeft: '+', topRight: '+', bottomLeft: '+', bottomRight: '+', horizontal: '-', vertical: '|' } }, spinner: { unicode: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'], ascii: ['-', '\\', '|', '/'] }, progress: { unicode: { complete: '█', incomplete: '░' }, ascii: { complete: '=', incomplete: '-' } } }; // Add new styled text functions specifically for intro animations Terminal.introStyles = { elegant: (text) => `${Terminal.colors.cyan}${text}${Terminal.colors.reset}`, matrix: (text) => `${Terminal.colors.brightGreen}${text}${Terminal.colors.reset}`, wave: (text) => `${Terminal.colors.brightMagenta}${Terminal.colors.bold}${text}${Terminal.colors.reset}`, sparkle: (text) => `${Terminal.colors.brightYellow}${text}${Terminal.colors.reset}`, minimal: (text) => `${Terminal.colors.brightWhite}${text}${Terminal.colors.reset}`, modern: (text) => `${Terminal.colors.brightBlue}${text}${Terminal.colors.reset}`, // Compound styles elegantBold: (text) => `${Terminal.colors.bold}${Terminal.colors.cyan}${text}${Terminal.colors.reset}`, matrixBright: (text) => `${Terminal.colors.brightGreen}${Terminal.colors.bold}${text}${Terminal.colors.reset}`, sparkleGlow: (text) => `${Terminal.colors.brightYellow}${Terminal.colors.bold}${text}${Terminal.colors.reset}`, baseWhite: (text) => `\x1b[37m${text}\x1b[0m`, }; //# sourceMappingURL=terminal.js.map