Method: CLI::UI::Spinner::SpinGroup#wait

Defined in:
lib/cli/ui/spinner/spin_group.rb

#wait(to: $stdout) ⇒ Object

Tells the group you’re done adding tasks and to wait for all of them to finish

Options

  • :to - Target stream, like $stdout or $stderr. Can be anything with print and puts methods, or under Sorbet, IO or StringIO. Defaults to $stdout

Example Usage:

spin_group = CLI::UI::SpinGroup.new
spin_group.add('Title') { |spinner| sleep 1.0 }
spin_group.wait

: (?to: io_like) -> bool



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/cli/ui/spinner/spin_group.rb', line 364

def wait(to: $stdout)
  result = false #: bool

  CLI::UI::ProgressReporter.with_progress(mode: :indeterminate, to: to, delay_start: true) do |reporter|
    idx = 0
    consumed_lines = 0

    @work_queue.close if @internal_work_queue

    tasks_seen = @tasks.map { false }
    tasks_seen_done = @tasks.map { false }

    current_mode = :indeterminate #: Symbol
    first_render = true #: bool

    loop do
      break if stopped?

      done_count = 0
      width = CLI::UI::Terminal.width

      # Update progress mode based on task states
      current_mode = update_progress_mode(reporter, current_mode, first_render)

      self.class.pause_mutex.synchronize do
        next if self.class.paused?

        @m.synchronize do
          CLI::UI.raw do
            # Render any messages above the spinner
            force_full_render = render_puts_above(to, consumed_lines)

            # Render all tasks
            done_count, consumed_lines = render_tasks(
              to: to,
              tasks_seen: tasks_seen,
              tasks_seen_done: tasks_seen_done,
              consumed_lines: consumed_lines,
              idx: idx,
              force_full_render: force_full_render,
              width: width,
            )
          end
        end
      end

      break if done_count == @tasks.size

      # After first render, start the progress reporter in indeterminate mode
      if first_render
        reporter.force_set_indeterminate
        first_render = false
      end

      idx = (idx + 1) % GLYPHS.size
      Spinner.index = idx
      sleep(PERIOD)
    end

    # Show error state briefly if tasks failed
    success = all_succeeded?
    unless success
      reporter.set_error
      sleep(0.5)
    end

    result = if @auto_debrief
      debrief(to: to)
    else
      all_succeeded?
    end
  end

  result
rescue Interrupt
  @work_queue.interrupt
  debrief(to: to) if @interrupt_debrief
  stopped? ? false : raise
end