feat(cli): modernize all CLI output to use logger tables
- Modernize ups list command with logger.logTable() - Modernize group list command with logger.logTable() - Completely rewrite config show with tables and proper box styling - Add professional column definitions with themed colors - Replace all manual table formatting (padEnd, pipe separators) - Improve visual hierarchy with appropriate box styles (info, warning, success) - Ensure consistent theming across all CLI commands
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import process from 'node:process';
|
||||
import { execSync } from 'node:child_process';
|
||||
import { Nupst } from '../nupst.ts';
|
||||
import { logger } from '../logger.ts';
|
||||
import { logger, type ITableColumn } from '../logger.ts';
|
||||
import { theme } from '../colors.ts';
|
||||
import * as helpers from '../helpers/index.ts';
|
||||
import type { TUpsModel } from '../snmp/types.ts';
|
||||
import type { INupstConfig } from '../daemon.ts';
|
||||
@@ -379,11 +380,10 @@ export class UpsHandler {
|
||||
try {
|
||||
await this.nupst.getDaemon().loadConfig();
|
||||
} catch (error) {
|
||||
const errorBoxWidth = 45;
|
||||
logger.logBoxTitle('Configuration Error', errorBoxWidth);
|
||||
logger.logBoxLine('No configuration found.');
|
||||
logger.logBoxLine("Please run 'nupst setup' first to create a configuration.");
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('Configuration Error', [
|
||||
'No configuration found.',
|
||||
"Please run 'nupst ups add' first to create a configuration.",
|
||||
], 50, 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -393,58 +393,57 @@ export class UpsHandler {
|
||||
// Check if multi-UPS config
|
||||
if (!config.upsDevices || !Array.isArray(config.upsDevices)) {
|
||||
// Legacy single UPS configuration
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('UPS Devices', boxWidth);
|
||||
logger.logBoxLine('Legacy single-UPS configuration detected.');
|
||||
if (!config.snmp || !config.thresholds) {
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Error: Configuration missing SNMP or threshold settings');
|
||||
logger.logBoxEnd();
|
||||
return;
|
||||
}
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Default UPS:');
|
||||
logger.logBoxLine(` Host: ${config.snmp.host}:${config.snmp.port}`);
|
||||
logger.logBoxLine(` Model: ${config.snmp.upsModel || 'cyberpower'}`);
|
||||
logger.logBoxLine(
|
||||
` Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime`,
|
||||
);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('Use "nupst add" to add more UPS devices and migrate');
|
||||
logger.logBoxLine('to the multi-UPS configuration format.');
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('UPS Devices', [
|
||||
'Legacy single-UPS configuration detected.',
|
||||
'',
|
||||
...((!config.snmp || !config.thresholds)
|
||||
? ['Error: Configuration missing SNMP or threshold settings']
|
||||
: [
|
||||
'Default UPS:',
|
||||
` Host: ${config.snmp.host}:${config.snmp.port}`,
|
||||
` Model: ${config.snmp.upsModel || 'cyberpower'}`,
|
||||
` Thresholds: ${config.thresholds.battery}% battery, ${config.thresholds.runtime} min runtime`,
|
||||
'',
|
||||
'Use "nupst ups add" to add more UPS devices and migrate',
|
||||
'to the multi-UPS configuration format.',
|
||||
]
|
||||
),
|
||||
], 60, 'warning');
|
||||
return;
|
||||
}
|
||||
|
||||
// Display UPS list
|
||||
const boxWidth = 60;
|
||||
logger.logBoxTitle('UPS Devices', boxWidth);
|
||||
|
||||
// Display UPS list with modern table
|
||||
if (config.upsDevices.length === 0) {
|
||||
logger.logBoxLine('No UPS devices configured.');
|
||||
logger.logBoxLine('Use "nupst add" to add a UPS device.');
|
||||
} else {
|
||||
logger.logBoxLine(`Found ${config.upsDevices.length} UPS device(s)`);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine(
|
||||
'ID | Name | Host | Mode | Groups',
|
||||
);
|
||||
logger.logBoxLine(
|
||||
'-----------+----------------------+-----------------+--------------+----------------',
|
||||
);
|
||||
|
||||
for (const ups of config.upsDevices) {
|
||||
const id = ups.id.padEnd(10, ' ').substring(0, 10);
|
||||
const name = (ups.name || '').padEnd(20, ' ').substring(0, 20);
|
||||
const host = `${ups.snmp.host}:${ups.snmp.port}`.padEnd(15, ' ').substring(0, 15);
|
||||
const model = (ups.snmp.upsModel || 'cyberpower').padEnd(12, ' ').substring(0, 12);
|
||||
const groups = ups.groups.length > 0 ? ups.groups.join(', ') : 'None';
|
||||
|
||||
logger.logBoxLine(`${id} | ${name} | ${host} | ${model} | ${groups}`);
|
||||
}
|
||||
logger.logBox('UPS Devices', [
|
||||
'No UPS devices configured.',
|
||||
'',
|
||||
`${theme.dim('Run')} ${theme.command('nupst ups add')} ${theme.dim('to add a device')}`,
|
||||
], 60, 'info');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.logBoxEnd();
|
||||
// Prepare table data
|
||||
const rows = config.upsDevices.map((ups) => ({
|
||||
id: ups.id,
|
||||
name: ups.name || '',
|
||||
host: `${ups.snmp.host}:${ups.snmp.port}`,
|
||||
model: ups.snmp.upsModel || 'cyberpower',
|
||||
groups: ups.groups.length > 0 ? ups.groups.join(', ') : theme.dim('None'),
|
||||
}));
|
||||
|
||||
const columns: ITableColumn[] = [
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.highlight },
|
||||
{ header: 'Name', key: 'name', align: 'left' },
|
||||
{ header: 'Host:Port', key: 'host', align: 'left', color: theme.info },
|
||||
{ header: 'Model', key: 'model', align: 'left' },
|
||||
{ header: 'Groups', key: 'groups', align: 'left' },
|
||||
];
|
||||
|
||||
logger.log('');
|
||||
logger.info(`UPS Devices (${config.upsDevices.length}):`);
|
||||
logger.log('');
|
||||
logger.logTable(columns, rows);
|
||||
logger.log('');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to list UPS devices: ${error instanceof Error ? error.message : String(error)}`,
|
||||
|
Reference in New Issue
Block a user