diff --git a/src/commands/list.ts b/src/commands/list.ts new file mode 100644 index 0000000..210c248 --- /dev/null +++ b/src/commands/list.ts @@ -0,0 +1,75 @@ +import { fileUtils } from '../utils/fileUtils'; +import { Layout } from '../models/types'; + +interface ListOptions { + format?: 'table' | 'json'; + verbose?: boolean; +} + +export async function listLayouts(options: ListOptions): Promise { + await fileUtils.initialize(); + + const layouts = await fileUtils.listLayouts(); + if (layouts.length === 0) { + console.log('No layouts found. Use "save" command to create your first layout.'); + return; + } + + const layoutDetails: Array<{ + name: string; + terminalType: string; + windows: number; + description: string; + updatedAt: string; + }> = []; + + for (const layoutFile of layouts) { + try { + const layout = await fileUtils.readLayout(layoutFile) as Layout; + layoutDetails.push({ + name: layoutFile.replace(/\.(json|yaml|yml)$/, ''), + terminalType: layout.terminalType, + windows: layout.session.windows.length, + description: layout.metadata?.description || '-', + updatedAt: layout.updatedAt, + }); + } catch { + layoutDetails.push({ + name: layoutFile, + terminalType: 'unknown', + windows: 0, + description: 'Error reading layout', + updatedAt: '-', + }); + } + } + + if (options.format === 'json') { + console.log(JSON.stringify(layoutDetails, null, 2)); + return; + } + + const terminalTypeWidth = Math.max(12, ...layoutDetails.map((l) => l.terminalType.length)); + const nameWidth = Math.max(20, ...layoutDetails.map((l) => l.name.length)); + + console.log('Saved Layouts'); + console.log('='.repeat(nameWidth + terminalTypeWidth + 60)); + console.log( + `Name${' '.repeat(nameWidth - 4)} | Type${' '.repeat(terminalTypeWidth - 4)} | Windows | Description` + ); + console.log('-'.repeat(nameWidth + terminalTypeWidth + 60)); + + for (const layout of layoutDetails) { + const name = layout.name.padEnd(nameWidth); + const type = layout.terminalType.padEnd(terminalTypeWidth); + const windows = String(layout.windows).padEnd(7); + const desc = layout.description.substring(0, 30); + console.log(`${name} | ${type} | ${windows} | ${desc}`); + } + + console.log(`\nTotal: ${layouts.length} layout(s)`); + + if (options.verbose) { + console.log('\nLayout Directory:', fileUtils.getLayoutsDir()); + } +}