import { ActionCard } from './shared/ActionCard'; import { Terminal } from 'lucide-react'; interface ShellCommandProps { command: string; result?: string; isError?: boolean; className?: string; } export function ShellCommand({ command, result, isError = false, className }: ShellCommandProps) { // Parse command to get useful information const parseCommand = (cmd: string) => { const parts = cmd.trim().split(' '); const baseCommand = parts[0]; // Get command info const getCommandInfo = (baseCmd: string) => { switch (baseCmd) { case 'ls': case 'dir': return { icon: '📁', description: 'List directory contents' }; case 'cd': return { icon: '📂', description: 'Change directory' }; case 'mkdir': return { icon: '📁', description: 'Create directory' }; case 'rm': case 'rmdir': case 'del': return { icon: '🗑️', description: 'Remove files/directories' }; case 'cp': case 'copy': return { icon: '📋', description: 'Copy files' }; case 'mv': case 'move': return { icon: '📦', description: 'Move/rename files' }; case 'cat': case 'type': return { icon: '📄', description: 'Display file contents' }; case 'grep': case 'findstr': return { icon: '🔍', description: 'Search text patterns' }; case 'find': return { icon: '🔍', description: 'Find files and directories' }; case 'chmod': return { icon: '🔐', description: 'Change file permissions' }; case 'git': return { icon: '🌿', description: 'Git version control' }; case 'npm': case 'yarn': case 'pnpm': return { icon: '📦', description: 'Package manager' }; case 'node': return { icon: '🟢', description: 'Run Node.js' }; case 'python': case 'python3': return { icon: '🐍', description: 'Run Python' }; case 'curl': case 'wget': return { icon: '🌐', description: 'Download from web' }; case 'ps': return { icon: '⚙️', description: 'List running processes' }; case 'kill': return { icon: '⚡', description: 'Terminate processes' }; case 'echo': return { icon: '💬', description: 'Display text' }; case 'pwd': return { icon: '📍', description: 'Show current directory' }; case 'whoami': return { icon: '👤', description: 'Show current user' }; case 'which': case 'where': return { icon: '🔍', description: 'Locate command' }; default: return { icon: '⚡', description: 'Execute command' }; } }; return { baseCommand, fullCommand: cmd, ...getCommandInfo(baseCommand) }; }; const commandInfo = parseCommand(command); // Parse output for useful information const parseOutput = (output: string) => { if (!output) return { summary: '', details: output }; const lines = output.split('\n').filter(line => line.trim()); // Look for common patterns if (commandInfo.baseCommand === 'ls' || commandInfo.baseCommand === 'dir') { const fileCount = lines.length; return { summary: `Found ${fileCount} item${fileCount !== 1 ? 's' : ''}`, details: output }; } if (commandInfo.baseCommand === 'git') { const gitSubcommand = command.split(' ')[1]; if (gitSubcommand === 'status') { return { summary: lines.find(line => line.includes('nothing to commit') || line.includes('Changes') || line.includes('Untracked')) || 'Git status checked', details: output }; } if (gitSubcommand === 'log') { const commitCount = (output.match(/commit /g) || []).length; return { summary: `Showing ${commitCount} commit${commitCount !== 1 ? 's' : ''}`, details: output }; } } if (commandInfo.baseCommand === 'npm' || commandInfo.baseCommand === 'yarn') { if (output.includes('added') || output.includes('installed')) { return { summary: 'Package installation completed', details: output }; } } // Default: use first meaningful line or last line const meaningfulLines = lines.filter(line => !line.startsWith('#') && !line.startsWith('//') && line.length > 0 ); return { summary: meaningfulLines[meaningfulLines.length - 1] || 'Command executed', details: output }; }; const { summary, details } = parseOutput(result || ''); return ( {commandInfo.icon}} result={result} isError={isError} className={className} runningIcon={} runningLabel="Executing" > {result && ( <> {/* Summary */} {summary && (
{summary}
)} {/* Full output */} {details && details.trim() && (
▶ View command output
Terminal Output
                    {details}
                  
)} {/* Command stats */} {!isError && details && (
{details.split('\n').length} line{details.split('\n').length !== 1 ? 's' : ''} output {commandInfo.baseCommand === 'ls' && ( {details.split('\n').filter(line => line.trim()).length} items listed )} {(commandInfo.baseCommand === 'git' && command.includes('log')) && ( {(details.match(/commit /g) || []).length} commits shown )}
)} )}
); }