* Starts the spinner. * * @example Usage * ```ts ignore * import { Spinner } from "@std/cli/unstable-spinner"; * * const spinner = new Spinner({ message: "Loading..." }); * spinner.start(); * ```
()
| 215 | * ``` |
| 216 | */ |
| 217 | start() { |
| 218 | if (this.#intervalId !== null || this.#output.writable.locked) { |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | let i = 0; |
| 223 | const noColor = Deno.noColor; |
| 224 | |
| 225 | // Updates the spinner after the given interval. |
| 226 | const updateFrame = () => { |
| 227 | const color = this.#color ?? ""; |
| 228 | const frame = encoder.encode( |
| 229 | noColor |
| 230 | ? this.#spinner[i] + " " + this.message |
| 231 | : color + this.#spinner[i] + COLOR_RESET + " " + this.message, |
| 232 | ); |
| 233 | // call writeSync once to reduce flickering |
| 234 | const writeData = new Uint8Array(LINE_CLEAR.length + frame.length); |
| 235 | writeData.set(LINE_CLEAR); |
| 236 | writeData.set(frame, LINE_CLEAR.length); |
| 237 | this.#output.writeSync(writeData); |
| 238 | i = (i + 1) % this.#spinner.length; |
| 239 | }; |
| 240 | |
| 241 | this.#intervalId = setInterval(updateFrame, this.#interval); |
| 242 | updateFrame(); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Stops the spinner. |
no outgoing calls
no test coverage detected