Files
nupst/test/showcase.ts
Juergen Kunz f8269a1cb7
Some checks failed
CI / Type Check & Lint (push) Failing after 6s
CI / Build Test (Current Platform) (push) Successful in 6s
Release / build-and-release (push) Successful in 44s
CI / Build All Platforms (push) Successful in 50s
CI / Type Check & Lint (pull_request) Failing after 5s
CI / Build Test (Current Platform) (pull_request) Successful in 5s
CI / Build All Platforms (pull_request) Successful in 49s
feat(cli): add beautiful colored output and fix daemon exit bug
Major improvements:
- Created color theme system (ts/colors.ts) with ANSI colors
- Enhanced logger with colors, table formatting, and styled boxes
- Fixed daemon exit bug - now stays running when no UPS configured
- Added config hot-reload with file watcher for live updates
- Beautified CLI help output with color-coded commands
- Added showcase test demonstrating all output features
- Fixed ANSI code handling for perfect table/box alignment

Features:
- Color-coded messages (success=green, error=red, warning=yellow, info=cyan)
- Status symbols (●○◐◯ for running/stopped/starting/unknown)
- Battery level colors (green>60%, yellow 30-60%, red<30%)
- Table formatting with auto-sizing and column alignment
- Styled boxes (success, error, warning, info styles)
- Hot-reload: daemon watches config file and reloads automatically
- Idle mode: daemon stays alive when no devices, checks periodically

Daemon improvements:
- No longer exits when no UPS devices configured
- Enters idle monitoring loop waiting for config
- File watcher detects config changes in real-time
- Auto-reloads and starts monitoring when devices added
- Logs warnings instead of errors for missing devices

Technical fixes:
- Strip ANSI codes when calculating text width for alignment
- Use visible length for padding calculations in tables and boxes
- Properly handle colored text in table cells and box lines

Breaking changes: None (backward compatible)
2025-10-19 15:08:30 +00:00

234 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Showcase test for NUPST CLI outputs
* Demonstrates all the beautiful colored output features
*
* Run with: deno run --allow-all test/showcase.ts
*/
import { logger, type ITableColumn } from '../ts/logger.ts';
import { theme, symbols, getBatteryColor, formatPowerStatus } from '../ts/colors.ts';
console.log('');
console.log('═'.repeat(80));
logger.highlight('NUPST CLI OUTPUT SHOWCASE');
logger.dim('Demonstrating beautiful, colored terminal output');
console.log('═'.repeat(80));
console.log('');
// === 1. Basic Logging Methods ===
logger.logBoxTitle('Basic Logging Methods', 60, 'info');
logger.logBoxLine('');
logger.log('Normal log message (default color)');
logger.success('Success message with ✓ symbol');
logger.error('Error message with ✗ symbol');
logger.warn('Warning message with ⚠ symbol');
logger.info('Info message with symbol');
logger.dim('Dim/secondary text for less important info');
logger.highlight('Highlighted/bold text for emphasis');
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 2. Colored Boxes ===
logger.logBoxTitle('Colored Box Styles', 60);
logger.logBoxLine('');
logger.logBoxLine('Boxes can be styled with different colors:');
logger.logBoxEnd();
console.log('');
logger.logBox('Success Box (Green)', [
'Used for successful operations',
'Installation complete, service started, etc.',
], 60, 'success');
console.log('');
logger.logBox('Error Box (Red)', [
'Used for critical errors and failures',
'Configuration errors, connection failures, etc.',
], 60, 'error');
console.log('');
logger.logBox('Warning Box (Yellow)', [
'Used for warnings and deprecations',
'Old command format, missing config, etc.',
], 60, 'warning');
console.log('');
logger.logBox('Info Box (Cyan)', [
'Used for informational messages',
'Version info, update available, etc.',
], 60, 'info');
console.log('');
// === 3. Status Symbols ===
logger.logBoxTitle('Status Symbols', 60, 'info');
logger.logBoxLine('');
logger.logBoxLine(`${symbols.running} Service Running`);
logger.logBoxLine(`${symbols.stopped} Service Stopped`);
logger.logBoxLine(`${symbols.starting} Service Starting`);
logger.logBoxLine(`${symbols.unknown} Status Unknown`);
logger.logBoxLine('');
logger.logBoxLine(`${symbols.success} Operation Successful`);
logger.logBoxLine(`${symbols.error} Operation Failed`);
logger.logBoxLine(`${symbols.warning} Warning Condition`);
logger.logBoxLine(`${symbols.info} Information`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 4. Battery Level Colors ===
logger.logBoxTitle('Battery Level Color Coding', 60, 'info');
logger.logBoxLine('');
logger.logBoxLine('Battery levels are color-coded:');
logger.logBoxLine('');
logger.logBoxLine(` ${getBatteryColor(85)('85%')} - Good (green, ≥60%)`);
logger.logBoxLine(` ${getBatteryColor(45)('45%')} - Medium (yellow, 30-60%)`);
logger.logBoxLine(` ${getBatteryColor(15)('15%')} - Critical (red, <30%)`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 5. Power Status Formatting ===
logger.logBoxTitle('Power Status Formatting', 60, 'info');
logger.logBoxLine('');
logger.logBoxLine(`Status: ${formatPowerStatus('online')}`);
logger.logBoxLine(`Status: ${formatPowerStatus('onBattery')}`);
logger.logBoxLine(`Status: ${formatPowerStatus('unknown')}`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 6. Table Formatting ===
const upsColumns: ITableColumn[] = [
{ header: 'ID', key: 'id' },
{ header: 'Name', key: 'name' },
{ header: 'Host', key: 'host' },
{ header: 'Status', key: 'status', color: (v) => {
if (v.includes('Online')) return theme.success(v);
if (v.includes('Battery')) return theme.warning(v);
return theme.dim(v);
}},
{ header: 'Battery', key: 'battery', align: 'right', color: (v) => {
const pct = parseInt(v);
return getBatteryColor(pct)(v);
}},
{ header: 'Runtime', key: 'runtime', align: 'right' },
];
const upsData = [
{
id: 'ups-1',
name: 'Main UPS',
host: '192.168.1.10',
status: 'Online',
battery: '95%',
runtime: '45 min',
},
{
id: 'ups-2',
name: 'Backup UPS',
host: '192.168.1.11',
status: 'On Battery',
battery: '42%',
runtime: '12 min',
},
{
id: 'ups-3',
name: 'Critical UPS',
host: '192.168.1.12',
status: 'On Battery',
battery: '18%',
runtime: '5 min',
},
];
logger.logTable(upsColumns, upsData, 'UPS Devices');
console.log('');
// === 7. Group Table ===
const groupColumns: ITableColumn[] = [
{ header: 'ID', key: 'id' },
{ header: 'Name', key: 'name' },
{ header: 'Mode', key: 'mode' },
{ header: 'UPS Count', key: 'count', align: 'right' },
];
const groupData = [
{ id: 'dc-1', name: 'Data Center 1', mode: 'redundant', count: '3' },
{ id: 'office', name: 'Office Servers', mode: 'nonRedundant', count: '2' },
];
logger.logTable(groupColumns, groupData, 'UPS Groups');
console.log('');
// === 8. Service Status Example ===
logger.logBoxTitle('Service Status', 70, 'success');
logger.logBoxLine('');
logger.logBoxLine(`Status: ${symbols.running} ${theme.statusActive('Active (Running)')}`);
logger.logBoxLine(`Enabled: ${symbols.success} ${theme.success('Yes')}`);
logger.logBoxLine(`Uptime: 2 days, 5 hours, 23 minutes`);
logger.logBoxLine(`PID: ${theme.dim('12345')}`);
logger.logBoxLine(`Memory: ${theme.dim('45.2 MB')}`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 9. Configuration Example ===
logger.logBoxTitle('Configuration', 70);
logger.logBoxLine('');
logger.logBoxLine(`UPS Devices: ${theme.highlight('3')}`);
logger.logBoxLine(`Groups: ${theme.highlight('2')}`);
logger.logBoxLine(`Check Interval: ${theme.dim('30 seconds')}`);
logger.logBoxLine(`Config File: ${theme.path('/etc/nupst/config.json')}`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 10. Update Available Example ===
logger.logBoxTitle('Update Available', 70, 'warning');
logger.logBoxLine('');
logger.logBoxLine(`Current Version: ${theme.dim('4.0.1')}`);
logger.logBoxLine(`Latest Version: ${theme.highlight('4.0.2')}`);
logger.logBoxLine('');
logger.logBoxLine(`Run ${theme.command('sudo nupst update')} to update`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === 11. Error Example ===
logger.logBoxTitle('Error Example', 70, 'error');
logger.logBoxLine('');
logger.logBoxLine(`${symbols.error} Failed to connect to UPS at 192.168.1.10`);
logger.logBoxLine('');
logger.logBoxLine('Possible causes:');
logger.logBoxLine(` ${theme.dim('• UPS is offline or unreachable')}`);
logger.logBoxLine(` ${theme.dim('• Incorrect SNMP community string')}`);
logger.logBoxLine(` ${theme.dim('• Firewall blocking port 161')}`);
logger.logBoxLine('');
logger.logBoxLine(`Try: ${theme.command('nupst ups test --debug')}`);
logger.logBoxLine('');
logger.logBoxEnd();
console.log('');
// === Final Summary ===
console.log('═'.repeat(80));
logger.success('CLI Output Showcase Complete!');
logger.dim('All color and formatting features demonstrated');
console.log('═'.repeat(80));
console.log('');