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,6 +1,7 @@
|
||||
import process from 'node: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 IGroupConfig } from '../daemon.ts';
|
||||
|
||||
@@ -28,11 +29,10 @@ export class GroupHandler {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -41,43 +41,53 @@ export class GroupHandler {
|
||||
|
||||
// Check if multi-UPS config
|
||||
if (!config.groups || !Array.isArray(config.groups)) {
|
||||
// Legacy or missing groups configuration
|
||||
const boxWidth = 45;
|
||||
logger.logBoxTitle('UPS Groups', boxWidth);
|
||||
logger.logBoxLine('No groups configured.');
|
||||
logger.logBoxLine('Use "nupst group add" to add a UPS group.');
|
||||
logger.logBoxEnd();
|
||||
logger.logBox('UPS Groups', [
|
||||
'No groups configured.',
|
||||
'',
|
||||
`${theme.dim('Run')} ${theme.command('nupst group add')} ${theme.dim('to add a group')}`,
|
||||
], 50, 'info');
|
||||
return;
|
||||
}
|
||||
|
||||
// Display group list
|
||||
const boxWidth = 60;
|
||||
logger.logBoxTitle('UPS Groups', boxWidth);
|
||||
|
||||
// Display group list with modern table
|
||||
if (config.groups.length === 0) {
|
||||
logger.logBoxLine('No UPS groups configured.');
|
||||
logger.logBoxLine('Use "nupst group add" to add a UPS group.');
|
||||
} else {
|
||||
logger.logBoxLine(`Found ${config.groups.length} group(s)`);
|
||||
logger.logBoxLine('');
|
||||
logger.logBoxLine('ID | Name | Mode | UPS Devices');
|
||||
logger.logBoxLine('-----------+----------------------+--------------+----------------');
|
||||
|
||||
for (const group of config.groups) {
|
||||
const id = group.id.padEnd(10, ' ').substring(0, 10);
|
||||
const name = (group.name || '').padEnd(20, ' ').substring(0, 20);
|
||||
const mode = (group.mode || 'unknown').padEnd(12, ' ').substring(0, 12);
|
||||
|
||||
// Count UPS devices in this group
|
||||
const upsInGroup = config.upsDevices.filter((ups) => ups.groups.includes(group.id));
|
||||
const upsCount = upsInGroup.length;
|
||||
const upsNames = upsInGroup.map((ups) => ups.name).join(', ');
|
||||
|
||||
logger.logBoxLine(`${id} | ${name} | ${mode} | ${upsCount > 0 ? upsNames : 'None'}`);
|
||||
}
|
||||
logger.logBox('UPS Groups', [
|
||||
'No UPS groups configured.',
|
||||
'',
|
||||
`${theme.dim('Run')} ${theme.command('nupst group add')} ${theme.dim('to add a group')}`,
|
||||
], 60, 'info');
|
||||
return;
|
||||
}
|
||||
|
||||
logger.logBoxEnd();
|
||||
// Prepare table data
|
||||
const rows = config.groups.map((group) => {
|
||||
// Count UPS devices in this group
|
||||
const upsInGroup = config.upsDevices.filter((ups) => ups.groups.includes(group.id));
|
||||
const upsCount = upsInGroup.length;
|
||||
const upsNames = upsInGroup.map((ups) => ups.name).join(', ');
|
||||
|
||||
return {
|
||||
id: group.id,
|
||||
name: group.name || '',
|
||||
mode: group.mode || 'unknown',
|
||||
count: String(upsCount),
|
||||
devices: upsCount > 0 ? upsNames : theme.dim('None'),
|
||||
};
|
||||
});
|
||||
|
||||
const columns: ITableColumn[] = [
|
||||
{ header: 'ID', key: 'id', align: 'left', color: theme.highlight },
|
||||
{ header: 'Name', key: 'name', align: 'left' },
|
||||
{ header: 'Mode', key: 'mode', align: 'left', color: theme.info },
|
||||
{ header: 'UPS Count', key: 'count', align: 'right' },
|
||||
{ header: 'UPS Devices', key: 'devices', align: 'left' },
|
||||
];
|
||||
|
||||
logger.log('');
|
||||
logger.info(`UPS Groups (${config.groups.length}):`);
|
||||
logger.log('');
|
||||
logger.logTable(columns, rows);
|
||||
logger.log('');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Failed to list UPS groups: ${error instanceof Error ? error.message : String(error)}`,
|
||||
|
Reference in New Issue
Block a user