(message: string, onComplete: () => void)
| 238 | let currentMessageIndex = 0 |
| 239 | |
| 240 | const streamWords = (message: string, onComplete: () => void) => { |
| 241 | const words = message.split(/\s+/) |
| 242 | let currentWordIndex = 0 |
| 243 | let currentText = '' |
| 244 | |
| 245 | const addNextWords = () => { |
| 246 | if (currentWordIndex >= words.length) { |
| 247 | onComplete() |
| 248 | return |
| 249 | } |
| 250 | |
| 251 | const wordsToAdd = Math.min( |
| 252 | Math.floor(Math.random() * 4) + 1, |
| 253 | words.length - currentWordIndex, |
| 254 | ) |
| 255 | |
| 256 | const nextSegment = words |
| 257 | .slice(currentWordIndex, currentWordIndex + wordsToAdd) |
| 258 | .join(' ') |
| 259 | currentText += (currentText ? ' ' : '') + nextSegment |
| 260 | currentWordIndex += wordsToAdd |
| 261 | |
| 262 | setTerminalLines((prev) => { |
| 263 | const newLines = [...prev] |
| 264 | if (newLines.length === currentMessageIndex) { |
| 265 | newLines.push(currentText) |
| 266 | } else { |
| 267 | newLines[currentMessageIndex] = currentText |
| 268 | } |
| 269 | return newLines |
| 270 | }) |
| 271 | |
| 272 | if (currentWordIndex < words.length) { |
| 273 | setTimeout(addNextWords, Math.random() * 100 + 50) |
| 274 | } else { |
| 275 | setTimeout(onComplete, 100) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | addNextWords() |
| 280 | } |
| 281 | |
| 282 | const processNextMessage = () => { |
| 283 | if (currentMessageIndex >= messages.length) { |
no test coverage detected