UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

482 lines 16.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.StatusBar = void 0; const blessed = __importStar(require("blessed")); const base_component_1 = require("./base.component"); class StatusBar extends base_component_1.BaseComponent { constructor(config, eventBus) { const tempState = { statusInfo: { connection: 'disconnected', lastUpdate: new Date(), memoryUsage: 0, cpuUsage: 0, currentOperation: 'Initializing...', availableShortcuts: ['F1: Help', 'F5: Refresh', 'q: Quit'], messages: [], }, isCollapsed: false, }; super(config, eventBus); this.state = { ...this.state, ...tempState, }; this.setupStatusListeners(); } setupStatusListeners() { this.subscribe('system:status', this.updateSystemStatus.bind(this)); this.subscribe('system:performance', this.updatePerformanceMetrics.bind(this)); this.subscribe('connection:status', this.updateConnectionStatus.bind(this)); this.subscribe('operation:status', this.updateOperationStatus.bind(this)); this.subscribe('message:display', this.displayMessage.bind(this)); this.subscribe('shortcuts:update', this.updateShortcuts.bind(this)); this.subscribe('status-bar:toggle', this.toggleCollapse.bind(this)); } createWidget() { this.statusContainer = blessed.box({ left: 0, top: this.config.statusPosition === 'top' ? 0 : '100%-3', width: '100%', height: 3, border: { type: 'line', }, style: { border: { fg: 'gray', }, bg: 'black', }, tags: true, mouse: true, keys: true, }); this.leftSection = blessed.box({ parent: this.statusContainer, left: 0, top: 0, width: '30%', height: '100%', content: '', style: { fg: 'white', bg: 'black', }, tags: true, }); this.centerSection = blessed.box({ parent: this.statusContainer, left: '30%', top: 0, width: '40%', height: '100%', content: '', align: 'center', style: { fg: 'cyan', bg: 'black', }, tags: true, }); this.rightSection = blessed.box({ parent: this.statusContainer, left: '70%', top: 0, width: '30%', height: '100%', content: '', align: 'right', style: { fg: 'yellow', bg: 'black', }, tags: true, }); this.messageArea = blessed.box({ parent: this.statusContainer, left: 0, top: 1, width: '100%', height: 1, content: '', style: { fg: 'white', bg: 'red', }, tags: true, hidden: true, }); this.widget = this.statusContainer; this.setupStatusEvents(); this.startUpdateTimer(); this.updateDisplay(); } setupStatusEvents() { this.statusContainer.on('click', () => { this.toggleCollapse(); }); this.statusContainer.key(['c'], () => { this.toggleCollapse(); }); this.statusContainer.key(['escape'], () => { this.clearMessages(); }); this.statusContainer.on('element dblclick', () => { this.showDetailedStatus(); }); } startUpdateTimer() { if (this.updateTimer) { clearInterval(this.updateTimer); } this.updateTimer = setInterval(() => { this.updateSystemMetrics(); this.updateDisplay(); }, this.config.updateInterval || 1000); } updateSystemMetrics() { this.state.statusInfo.lastUpdate = new Date(); if (this.config.showMemoryUsage) { try { const memUsage = process.memoryUsage(); this.state.statusInfo.memoryUsage = Math.round((memUsage.heapUsed / memUsage.heapTotal) * 100); } catch (error) { this.state.statusInfo.memoryUsage = 0; } } if (this.config.showCpuUsage) { this.state.statusInfo.cpuUsage = Math.round(Math.random() * 20 + 5); } } updateDisplay() { if (this.isDestroyed || this.state.isCollapsed) { this.hideStatusBar(); return; } if (!this.state.statusInfo) { return; } this.updateLeftSection(); this.updateCenterSection(); this.updateRightSection(); this.updateMessageArea(); this.render(); } updateLeftSection() { const parts = []; if (this.config.showConnectionStatus) { const status = this.state.statusInfo.connection; const statusColor = this.getConnectionStatusColor(status); const statusText = this.getConnectionStatusText(status); parts.push(`{${statusColor}}● ${statusText}{/${statusColor}}`); } const operation = this.state.statusInfo.currentOperation; if (operation && operation !== 'Idle') { parts.push(`{gray-fg}${operation}{/gray-fg}`); } this.leftSection.setContent(parts.join(' | ')); } updateCenterSection() { if (!this.config.showOperationHints) { this.centerSection.setContent(''); return; } const shortcuts = this.state.statusInfo.availableShortcuts.slice(0, 3); const hintsText = shortcuts.join(' • '); this.centerSection.setContent(`{gray-fg}${hintsText}{/gray-fg}`); } updateRightSection() { const parts = []; if (this.config.showMemoryUsage) { const memColor = this.state.statusInfo.memoryUsage > 80 ? 'red' : this.state.statusInfo.memoryUsage > 60 ? 'yellow' : 'green'; parts.push(`{${memColor}-fg}MEM:${this.state.statusInfo.memoryUsage}%{/${memColor}-fg}`); } if (this.config.showCpuUsage) { const cpuColor = this.state.statusInfo.cpuUsage > 80 ? 'red' : this.state.statusInfo.cpuUsage > 60 ? 'yellow' : 'green'; parts.push(`{${cpuColor}-fg}CPU:${this.state.statusInfo.cpuUsage}%{/${cpuColor}-fg}`); } if (this.config.showTime) { const timeStr = this.state.statusInfo.lastUpdate.toLocaleTimeString('en-US', { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit', }); parts.push(`{white-fg}${timeStr}{/white-fg}`); } this.rightSection.setContent(parts.join(' ')); } updateMessageArea() { if (this.state.statusInfo.messages.length === 0) { this.messageArea.hide(); return; } const latestMessage = this.state.statusInfo.messages[0]; if (!latestMessage) return; const messageColor = this.getMessageColor(latestMessage.level); const messageText = `{${messageColor}}${latestMessage.text}{/${messageColor}}`; this.messageArea.setContent(messageText); this.messageArea.show(); if (latestMessage.level === 'info') { setTimeout(() => { this.clearMessage(latestMessage); }, 5000); } } getConnectionStatusColor(status) { switch (status) { case 'connected': return 'green-fg'; case 'connecting': return 'yellow-fg'; case 'disconnected': return 'gray-fg'; case 'error': return 'red-fg'; default: return 'gray-fg'; } } getConnectionStatusText(status) { switch (status) { case 'connected': return 'Connected'; case 'connecting': return 'Connecting'; case 'disconnected': return 'Offline'; case 'error': return 'Error'; default: return 'Unknown'; } } getMessageColor(level) { switch (level) { case 'info': return 'blue-fg'; case 'warning': return 'yellow-fg'; case 'error': return 'red-fg'; default: return 'white-fg'; } } toggleCollapse() { this.state.isCollapsed = !this.state.isCollapsed; if (this.state.isCollapsed) { this.hideStatusBar(); } else { this.showStatusBar(); } this.emit('status-bar:toggled', { collapsed: this.state.isCollapsed, componentId: this.state.id, timestamp: new Date(), }); } hideStatusBar() { if (this.statusContainer) { this.statusContainer.height = 1; this.leftSection.hide(); this.centerSection.hide(); this.rightSection.hide(); this.messageArea.hide(); this.statusContainer.setContent('{gray-fg}Status Bar (click to expand){/gray-fg}'); } } showStatusBar() { if (this.statusContainer) { this.statusContainer.height = 3; this.statusContainer.setContent(''); this.leftSection.show(); this.centerSection.show(); this.rightSection.show(); this.updateDisplay(); } } showDetailedStatus() { const detailsPopup = blessed.box({ parent: this.widget.screen, left: 'center', top: 'center', width: 80, height: 20, border: { type: 'line', }, style: { border: { fg: 'cyan', }, bg: 'black', }, label: ' Detailed Status ', content: this.generateDetailedStatusText(), tags: true, keys: true, mouse: true, }); detailsPopup.key(['escape', 'q'], () => { detailsPopup.destroy(); this.widget.screen?.render(); }); detailsPopup.focus(); this.widget.screen?.render(); } generateDetailedStatusText() { const status = this.state.statusInfo; const uptime = process.uptime(); const memUsage = process.memoryUsage(); return [ 'System Information:', ` Connection: ${status.connection}`, ` Last Update: ${status.lastUpdate.toLocaleString()}`, ` Current Operation: ${status.currentOperation}`, '', 'Performance Metrics:', ` Memory Usage: ${status.memoryUsage}%`, ` CPU Usage: ${status.cpuUsage}%`, ` Process Uptime: ${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`, ` Heap Used: ${Math.round(memUsage.heapUsed / 1024 / 1024)}MB`, ` Heap Total: ${Math.round(memUsage.heapTotal / 1024 / 1024)}MB`, '', 'Available Shortcuts:', ...status.availableShortcuts.map(shortcut => ` ${shortcut}`), '', 'Recent Messages:', ...status.messages.slice(0, 5).map(msg => ` [${msg.level.toUpperCase()}] ${msg.text}`), '', 'Press ESC or Q to close', ].join('\n'); } updateSystemStatus(data) { this.state.statusInfo = { ...this.state.statusInfo, ...data }; this.updateDisplay(); } updatePerformanceMetrics(data) { if (data.memoryUsage !== undefined) { this.state.statusInfo.memoryUsage = data.memoryUsage; } if (data.cpuUsage !== undefined) { this.state.statusInfo.cpuUsage = data.cpuUsage; } this.updateDisplay(); } updateConnectionStatus(data) { this.state.statusInfo.connection = data.status; this.updateDisplay(); } updateOperationStatus(data) { this.state.statusInfo.currentOperation = data.operation; this.updateDisplay(); } displayMessage(data) { this.state.statusInfo.messages.unshift(data); if (this.state.statusInfo.messages.length > 10) { this.state.statusInfo.messages = this.state.statusInfo.messages.slice(0, 10); } this.updateDisplay(); } updateShortcuts(data) { this.state.statusInfo.availableShortcuts = data.shortcuts; this.updateDisplay(); } clearMessages() { this.state.statusInfo.messages = []; this.updateDisplay(); } clearMessage(messageToRemove) { this.state.statusInfo.messages = this.state.statusInfo.messages.filter(msg => !(msg.level === messageToRemove.level && msg.text === messageToRemove.text)); this.updateDisplay(); } setPosition(position) { this.config.statusPosition = position; if (this.statusContainer) { this.statusContainer.top = position === 'top' ? 0 : '100%-3'; this.render(); } } updateConfig(newConfig) { this.config = { ...this.config, ...newConfig }; if (newConfig.updateInterval && this.updateTimer) { this.startUpdateTimer(); } this.updateDisplay(); } getStatusInfo() { return { ...this.state.statusInfo }; } update(data) { if (this.isDestroyed) return; try { if (data.statusInfo) { this.updateSystemStatus(data.statusInfo); } if (data.performance) { this.updatePerformanceMetrics(data.performance); } if (data.connectionStatus) { this.updateConnectionStatus(data.connectionStatus); } if (data.operation) { this.updateOperationStatus(data.operation); } if (data.message) { this.displayMessage(data.message); } if (data.shortcuts) { this.updateShortcuts(data.shortcuts); } } catch (error) { this.handleError(error instanceof Error ? error : new Error('Update error')); } } render() { if (this.widget && !this.isDestroyed) { try { this.widget.screen?.render(); } catch (error) { console.error('StatusBar render error:', error); } } } destroy() { if (this.updateTimer) { clearInterval(this.updateTimer); this.updateTimer = undefined; } super.destroy(); } } exports.StatusBar = StatusBar; //# sourceMappingURL=status-bar.js.map