Files
nupst/ts/colors.ts

90 lines
2.2 KiB
TypeScript
Raw Normal View History

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
/**
* Color theme and styling utilities for NUPST CLI
* Uses Deno standard library colors module
*/
import * as colors from '@std/fmt/colors';
/**
* Color theme for consistent CLI styling
*/
export const theme = {
// Message types
error: colors.red,
warning: colors.yellow,
success: colors.green,
info: colors.cyan,
dim: colors.dim,
highlight: colors.bold,
bright: colors.bright,
// Status indicators
statusActive: (text: string) => colors.green(colors.bold(text)),
statusInactive: (text: string) => colors.red(text),
statusWarning: (text: string) => colors.yellow(text),
statusUnknown: (text: string) => colors.dim(text),
// Battery level colors
batteryGood: colors.green, // > 60%
batteryMedium: colors.yellow, // 30-60%
batteryCritical: colors.red, // < 30%
// Box borders
borderSuccess: colors.green,
borderError: colors.red,
borderWarning: colors.yellow,
borderInfo: colors.cyan,
borderDefault: (text: string) => text, // No color
// Command/code highlighting
command: colors.cyan,
code: colors.dim,
path: colors.blue,
};
/**
* Status symbols with colors
*/
export const symbols = {
success: colors.green('✓'),
error: colors.red('✗'),
warning: colors.yellow('⚠'),
info: colors.cyan(''),
running: colors.green('●'),
stopped: colors.red('○'),
starting: colors.yellow('◐'),
unknown: colors.dim('◯'),
};
/**
* Get color for battery level
*/
export function getBatteryColor(percentage: number): (text: string) => string {
if (percentage >= 60) return theme.batteryGood;
if (percentage >= 30) return theme.batteryMedium;
return theme.batteryCritical;
}
/**
* Get color for runtime remaining
*/
export function getRuntimeColor(minutes: number): (text: string) => string {
if (minutes >= 20) return theme.batteryGood;
if (minutes >= 10) return theme.batteryMedium;
return theme.batteryCritical;
}
/**
* Format UPS power status with color
*/
export function formatPowerStatus(status: 'online' | 'onBattery' | 'unknown'): string {
switch (status) {
case 'online':
return theme.success('Online');
case 'onBattery':
return theme.warning('On Battery');
case 'unknown':
default:
return theme.dim('Unknown');
}
}