UNPKG

@oclif/multi-stage-output

Version:

Terminal output for oclif commands with multiple stages

22 lines (21 loc) 766 B
import { Text } from 'ink'; import React from 'react'; import { readableTime } from '../utils.js'; export function Timer({ color, isStopped, unit, }) { const [time, setTime] = React.useState(0); const startTime = React.useRef(0); React.useEffect(() => { startTime.current = Date.now(); }, []); React.useEffect(() => { if (isStopped) { setTime(Date.now() - startTime.current); return () => { }; } const intervalId = setInterval(() => { setTime(Date.now() - startTime.current); }, unit === 'ms' ? 1 : 1000); return () => { clearInterval(intervalId); }; }, [isStopped, unit]); return React.createElement(Text, { color: color }, readableTime(time, unit)); }