| 1 | import { Text } from "ink"; |
| 2 | import React, { useEffect, useState } from "react"; |
| 3 | |
| 4 | const BRAILLE_BY_DENSITY: { [key: number]: string[] } = { |
| 5 | 0: ["⠀"], // No dots |
| 6 | 1: ["⠁", "⠂", "⠄", "⠈", "⠐", "⠠", "⡀", "⢀"], // 1 dot |
| 7 | 2: ["⠃", "⠅", "⠉", "⠑", "⠡", "⠊", "⠒", "⠔"], // 2 dots |
| 8 | 3: ["⠇", "⠋", "⠍", "⠕", "⠣", "⠱", "⠪", "⠜"], // 3 dots |
| 9 | 4: ["⠏", "⠗", "⠛", "⠝", "⠧", "⠯", "⠳", "⠵"], // 4 dots |
| 10 | 5: ["⠟", "⠫", "⠭", "⠷", "⠽", "⠾", "⠻", "⠺"], // 5 dots |
| 11 | 6: ["⠿", "⡷", "⡾", "⡻", "⡯", "⡧", "⢿", "⣷"], // 6 dots |
| 12 | 7: ["⣿", "⣾", "⣽", "⣻", "⣯", "⣧", "⣏", "⡿"], // 7 dots |
| 13 | 8: ["⣿"], // All dots (8) |
| 14 | }; |
| 15 | |
| 16 | // Function to generate frame with specific dot density |
| 17 | const generateDensityFrame = (dotCount: number): string => { |
| 18 | const chars = BRAILLE_BY_DENSITY[Math.min(8, Math.max(0, dotCount))]; |
| 19 | return Array.from( |
| 20 | { length: 3 }, |
| 21 | () => chars[Math.floor(Math.random() * chars.length)], |
| 22 | ).join(""); |
| 23 | }; |
| 24 | |
| 25 | // Helper function to generate eased static fade animation with full timing control |
| 26 | const generateEasedStaticFade = ( |
| 27 | timingCurve: number[] = [3, 3, 2, 1, 1, 1, 2, 3, 3], |
| 28 | ): string[] => { |
| 29 | const frames: string[] = []; |
| 30 | |
| 31 | // timingCurve[0] = frames at density 8 |
| 32 | // timingCurve[1] = frames at density 7 |
| 33 | // timingCurve[2] = frames at density 6 |
| 34 | // ... and so on |
| 35 | // timingCurve[8] = frames at density 0 |
| 36 | |
| 37 | // Going down (8 -> 0) |
| 38 | for (let density = 8; density >= 0; density--) { |
| 39 | const frameCount = timingCurve[8 - density]; |
| 40 | for (let i = 0; i < frameCount; i++) { |
| 41 | frames.push(generateDensityFrame(density)); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // Going up (1 -> 8) - skip 0 to avoid double-holding at the bottom |
| 46 | for (let density = 1; density <= 8; density++) { |
| 47 | const frameCount = timingCurve[8 - density]; |
| 48 | for (let i = 0; i < frameCount; i++) { |
| 49 | frames.push(generateDensityFrame(density)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return frames; |
| 54 | }; |
| 55 | |
| 56 | // Usage examples: |
| 57 | |
| 58 | // Default ease-in-ease-out |
| 59 | const TIMING_CURVE = [3, 3, 2, 1, 1, 0, 1, 2, 3]; |
| 60 | const STATIC_FADE = [ |
| 61 | ...generateEasedStaticFade(TIMING_CURVE), |
| 62 | ...generateEasedStaticFade(TIMING_CURVE), |
| 63 | ...generateEasedStaticFade(TIMING_CURVE), |
| 64 | ...generateEasedStaticFade(TIMING_CURVE), |
| 65 | ]; |
| 66 | |
| 67 | const SPINNER: string | string[] = STATIC_FADE; |
| 68 | |
| 69 | interface LoadingAnimationProps { |
| 70 | visible?: boolean; |
| 71 | color?: string; |
| 72 | } |
| 73 | |
| 74 | const LoadingAnimation: React.FC<LoadingAnimationProps> = ({ |
| 75 | visible = true, |
| 76 | color = "green", |
| 77 | }) => { |
| 78 | const [currentIndex, setCurrentIndex] = useState(0); |
| 79 | const animationChars = |
| 80 | typeof SPINNER === "string" ? (SPINNER as string).split("") : SPINNER; |
| 81 | |
| 82 | useEffect(() => { |
| 83 | if (!visible) return; |
| 84 | |
| 85 | const interval = setInterval(() => { |
| 86 | setCurrentIndex((prev) => (prev + 1) % animationChars.length); |
| 87 | }, 150); |
| 88 | |
| 89 | return () => clearInterval(interval); |
| 90 | }, [visible, animationChars.length]); |
| 91 | |
| 92 | if (!visible) return null; |
| 93 | |
| 94 | return <Text color={color}>{animationChars[currentIndex]}</Text>; |
| 95 | }; |
| 96 | |
| 97 | export { LoadingAnimation }; |