forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStepProgress.tsx
More file actions
152 lines (131 loc) · 3.7 KB
/
Copy pathStepProgress.tsx
File metadata and controls
152 lines (131 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { STATUS_COLORS, TEXT_COLORS } from '../theme';
import { Box, Text } from 'ink';
import React, { useEffect, useState } from 'react';
export type StepStatus = 'pending' | 'running' | 'success' | 'error' | 'warn' | 'info';
export interface Step {
label: string;
status: StepStatus;
error?: string;
warn?: string;
info?: string;
}
// eslint-disable-next-line react-refresh/only-export-components
export function hasStepError(steps: Step[]): boolean {
return steps.some(s => s.status === 'error');
}
// eslint-disable-next-line react-refresh/only-export-components
export function areStepsComplete(steps: Step[]): boolean {
if (steps.length === 0) return false;
return steps.every(s => s.status === 'success' || s.status === 'error' || s.status === 'warn' || s.status === 'info');
}
const STEP_LABELS = {
pending: ' ',
running: ' ',
success: '[done] ',
error: '[error] ',
warn: '[warning] ',
info: ' ',
} as const;
const STEP_COLORS = {
pending: TEXT_COLORS.muted,
running: undefined,
success: STATUS_COLORS.success,
error: STATUS_COLORS.error,
warn: STATUS_COLORS.warning,
info: undefined,
} as const;
interface GradientTextProps {
text: string;
}
type CharBrightness = 'white' | 'gray' | 'dim';
function getCharBrightness(distance: number): CharBrightness {
if (distance <= 2) return 'white';
if (distance <= 4) return 'gray';
return 'dim';
}
export function GradientText({ text }: GradientTextProps) {
const [offset, setOffset] = useState(0);
useEffect(() => {
const interval = setInterval(() => {
setOffset(prev => (prev + 1) % text.length);
}, 125);
return () => clearInterval(interval);
}, [text.length]);
const chars = text.split('');
return (
<Text>
{chars.map((char, i) => {
const distance = Math.abs(i - offset);
const brightness = getCharBrightness(distance);
if (brightness === 'dim') {
return (
<Text key={i} dimColor>
{char}
</Text>
);
}
return (
<Text key={i} color={brightness === 'white' ? TEXT_COLORS.primary : TEXT_COLORS.muted}>
{char}
</Text>
);
})}
</Text>
);
}
interface StepIndicatorProps {
step: Step;
}
function StepIndicator({ step }: StepIndicatorProps) {
const label = STEP_LABELS[step.status];
const color = STEP_COLORS[step.status];
if (step.status === 'running') {
return (
<Box>
<Text>{label}</Text>
<GradientText text={step.label} />
</Box>
);
}
return (
<Box flexDirection="column">
<Text color={color} dimColor={step.status === 'pending'}>
{label}
{step.label}
</Text>
{step.status === 'error' && step.error && (
<Text color={STATUS_COLORS.error}>
{' → '}
{step.error}
</Text>
)}
{step.status === 'warn' && step.warn && (
<Text color={STATUS_COLORS.warning}>
{' → '}
{step.warn}
</Text>
)}
{step.status === 'info' && step.info && (
<Text color={STATUS_COLORS.info}>
{' → '}
{step.info}
</Text>
)}
</Box>
);
}
interface StepProgressProps {
steps: Step[];
}
export function StepProgress({ steps }: StepProgressProps) {
// Find if there's an error - if so, don't show pending steps after it
const errorIndex = steps.findIndex(s => s.status === 'error');
const visibleSteps = errorIndex >= 0 ? steps.slice(0, errorIndex + 1) : steps;
return (
<Box flexDirection="column">
{visibleSteps.map((step, i) => (
<StepIndicator key={i} step={step} />
))}
</Box>
);
}