|
| 1 | +import { Box, Text } from 'ink'; |
| 2 | +import type { HistoryEntry } from '../hooks/useShell.js'; |
| 3 | + |
| 4 | +type Props = { |
| 5 | + entry: HistoryEntry; |
| 6 | +}; |
| 7 | + |
| 8 | +type DiffItem = { |
| 9 | + filenames?: string[]; |
| 10 | + content?: string; |
| 11 | + type: 'created' | 'modified' | 'deleted' | 'stdout' | 'stderr' | 'exit'; |
| 12 | + exitCode?: number; |
| 13 | + durationMs?: number; |
| 14 | +}; |
| 15 | + |
| 16 | +const TYPE_STYLE: Record<DiffItem['type'], { label: string; color: string }> = { |
| 17 | + created: { label: 'Created', color: 'green' }, |
| 18 | + modified: { label: 'Modified', color: 'yellow' }, |
| 19 | + deleted: { label: 'Deleted', color: 'red' }, |
| 20 | + stdout: { label: 'Stdout', color: 'white' }, |
| 21 | + stderr: { label: 'Stderr', color: 'red' }, |
| 22 | + exit: { label: 'Exit', color: 'green' }, |
| 23 | +}; |
| 24 | + |
| 25 | +function formatDuration(ms: number): string { |
| 26 | + if (ms < 1000) return `${ms}ms`; |
| 27 | + return `${(ms / 1000).toFixed(1)}s`; |
| 28 | +} |
| 29 | + |
| 30 | +function groupDiffItems(items: DiffItem[]): DiffItem[] { |
| 31 | + return items.reduce<DiffItem[]>((acc, item) => { |
| 32 | + const isDiff = item.type === 'created' || item.type === 'modified' || item.type === 'deleted'; |
| 33 | + const prev = acc[acc.length - 1]; |
| 34 | + |
| 35 | + if (isDiff && prev?.type === item.type) { |
| 36 | + prev.filenames = [...(prev.filenames ?? []), ...(item.filenames ?? [])]; |
| 37 | + return acc; |
| 38 | + } |
| 39 | + |
| 40 | + acc.push({ ...item }); |
| 41 | + return acc; |
| 42 | + }, []); |
| 43 | +} |
| 44 | + |
| 45 | +function contentHeight(item: DiffItem): number { |
| 46 | + const isDiff = item.type === 'created' || item.type === 'modified' || item.type === 'deleted'; |
| 47 | + if (isDiff) return (item.filenames?.length ?? 0) + 1; |
| 48 | + if (item.type === 'exit') return 1; |
| 49 | + return (item.content?.split('\n').length ?? 0) + 1; |
| 50 | +} |
| 51 | + |
| 52 | +export function DiffTimeline({ entry }: Props) { |
| 53 | + const rawItems: DiffItem[] = [ |
| 54 | + ...(entry.diff?.created || []).map(f => ({ filenames: [f], type: 'created' as const })), |
| 55 | + ...(entry.diff?.modified || []).map(f => ({ filenames: [f], type: 'modified' as const })), |
| 56 | + ...(entry.diff?.deleted || []).map(f => ({ filenames: [f], type: 'deleted' as const })), |
| 57 | + ...(entry.stdout ? [{ content: entry.stdout.trimEnd(), type: 'stdout' as const }] : []), |
| 58 | + ...(entry.stderr ? [{ content: entry.stderr.trimEnd(), type: 'stderr' as const }] : []), |
| 59 | + { type: 'exit' as const, exitCode: entry.exitCode, durationMs: entry.durationMs }, |
| 60 | + ]; |
| 61 | + |
| 62 | + const items = groupDiffItems(rawItems); |
| 63 | + |
| 64 | + return ( |
| 65 | + <Box flexDirection="column" paddingLeft={2} marginBottom={1} marginTop={1}> |
| 66 | + {items.map((item, index) => { |
| 67 | + const isLast = index === items.length - 1; |
| 68 | + const style = TYPE_STYLE[item.type]; |
| 69 | + const isDiff = item.type === 'created' || item.type === 'modified' || item.type === 'deleted'; |
| 70 | + const isExit = item.type === 'exit'; |
| 71 | + const exitColor = isExit ? (item.exitCode === 0 ? 'green' : 'red') : style.color; |
| 72 | + const pipeCount = contentHeight(item) + (isLast ? -1 : 1); |
| 73 | + |
| 74 | + return ( |
| 75 | + <Box key={index} flexDirection="row"> |
| 76 | + <Box flexDirection="column" marginRight={2} alignItems="center"> |
| 77 | + <Text color={isExit ? exitColor : style.color}>●</Text> |
| 78 | + {pipeCount > 0 && ( |
| 79 | + <Box flexDirection="column"> |
| 80 | + {Array.from({ length: pipeCount }).map((_, i) => ( |
| 81 | + <Text key={i} dimColor>│</Text> |
| 82 | + ))} |
| 83 | + </Box> |
| 84 | + )} |
| 85 | + </Box> |
| 86 | + |
| 87 | + <Box flexDirection="column"> |
| 88 | + {isExit ? ( |
| 89 | + <Box gap={1}> |
| 90 | + <Text color={exitColor}>exit {item.exitCode}</Text> |
| 91 | + </Box> |
| 92 | + ) : isDiff ? ( |
| 93 | + <Box flexDirection="column" gap={1}> |
| 94 | + <Text bold>{style.label}</Text> |
| 95 | + <Box flexDirection="column"> |
| 96 | + {item.filenames?.map((filename, i) => ( |
| 97 | + <Text key={i}> |
| 98 | + {item.type === 'created' ? <Text color={style.color}>[+]</Text> : item.type === 'modified' ? <Text color={style.color}>[~]</Text> : <Text color={style.color}>[-]</Text>} |
| 99 | + {filename} |
| 100 | + </Text> |
| 101 | + ))} |
| 102 | + </Box> |
| 103 | + </Box> |
| 104 | + ) : ( |
| 105 | + <Box flexDirection="column"> |
| 106 | + <Text bold dimColor={item.type === 'stderr'}>{style.label}</Text> |
| 107 | + <Box marginTop={1}> |
| 108 | + <Text dimColor={item.type === 'stderr'}>{item.content}</Text> |
| 109 | + </Box> |
| 110 | + </Box> |
| 111 | + )} |
| 112 | + </Box> |
| 113 | + </Box> |
| 114 | + ); |
| 115 | + })} |
| 116 | + </Box> |
| 117 | + ); |
| 118 | +} |
0 commit comments