Class: CLI::UI::Spinner::SpinGroup

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/cli/ui/spinner/spin_group.rb

Defined Under Namespace

Classes: Task

Constant Summary collapse

DEFAULT_FINAL_GLYPH =
->(success) { success ? CLI::UI::Glyph::CHECK : CLI::UI::Glyph::X }

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initialize(auto_debrief: true, max_concurrent: 0, work_queue: nil) ⇒ SpinGroup

Returns a new instance of SpinGroup.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cli/ui/spinner/spin_group.rb', line 74

def initialize(auto_debrief: true, max_concurrent: 0, work_queue: nil)
  @m = Mutex.new
  @tasks = []
  @auto_debrief = auto_debrief
  @start = Time.new
  @internal_work_queue = work_queue.nil?
  @work_queue = T.let(
    work_queue || WorkQueue.new(max_concurrent.zero? ? 1024 : max_concurrent),
    WorkQueue,
  )
  if block_given?
    yield self
    wait
  end
end

Class Attribute Details

.pause_mutexObject (readonly)

Returns the value of attribute pause_mutex.



16
17
18
# File 'lib/cli/ui/spinner/spin_group.rb', line 16

def pause_mutex
  @pause_mutex
end

Class Method Details

.pause_spinners(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cli/ui/spinner/spin_group.rb', line 28

def pause_spinners(&block)
  previous_paused = T.let(nil, T.nilable(T::Boolean))
  @pause_mutex.synchronize do
    previous_paused = @paused
    @paused = true
  end
  block.call
ensure
  @pause_mutex.synchronize do
    @paused = previous_paused
  end
end

.paused?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/cli/ui/spinner/spin_group.rb', line 19

def paused?
  @paused
end

Instance Method Details

#add(title, final_glyph: DEFAULT_FINAL_GLYPH, merged_output: false, duplicate_output_to: File.new(File::NULL, 'w'), &block) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/cli/ui/spinner/spin_group.rb', line 289

def add(
  title,
  final_glyph: DEFAULT_FINAL_GLYPH,
  merged_output: false,
  duplicate_output_to: File.new(File::NULL, 'w'),
  &block
)
  @m.synchronize do
    @tasks << Task.new(
      title,
      final_glyph: final_glyph,
      merged_output: merged_output,
      duplicate_output_to: duplicate_output_to,
      work_queue: @work_queue,
      &block
    )
  end
end

#all_succeeded?Boolean

Returns:

  • (Boolean)


401
402
403
404
405
# File 'lib/cli/ui/spinner/spin_group.rb', line 401

def all_succeeded?
  @m.synchronize do
    @tasks.all?(&:success)
  end
end

#debriefObject



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
# File 'lib/cli/ui/spinner/spin_group.rb', line 410

def debrief
  @m.synchronize do
    @tasks.each do |task|
      title = task.title
      out = task.stdout
      err = task.stderr

      if task.success
        next @success_debrief&.call(title, out, err)
      end

      e = task.exception
      next @failure_debrief.call(title, e, out, err) if @failure_debrief

      CLI::UI::Frame.open('Task Failed: ' + title, color: :red, timing: Time.new - @start) do
        if e
          puts "#{e.class}: #{e.message}"
          puts "\tfrom #{e.backtrace.join("\n\tfrom ")}"
        end

        CLI::UI::Frame.divider('STDOUT')
        out = '(empty)' if out.nil? || out.strip.empty?
        puts out

        CLI::UI::Frame.divider('STDERR')
        err = '(empty)' if err.nil? || err.strip.empty?
        puts err
      end
    end
    @tasks.all?(&:success)
  end
end

#failure_debrief(&block) ⇒ Object



386
387
388
# File 'lib/cli/ui/spinner/spin_group.rb', line 386

def failure_debrief(&block)
  @failure_debrief = block
end

#success_debrief(&block) ⇒ Object



396
397
398
# File 'lib/cli/ui/spinner/spin_group.rb', line 396

def success_debrief(&block)
  @success_debrief = block
end

#waitObject



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/cli/ui/spinner/spin_group.rb', line 316

def wait
  idx = 0

  consumed_lines = 0

  @work_queue.close if @internal_work_queue

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

  loop do
    done_count = 0

    width = CLI::UI::Terminal.width

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

      @m.synchronize do
        CLI::UI.raw do
          @tasks.each.with_index do |task, int_index|
            nat_index = int_index + 1
            task_done = task.check
            done_count += 1 if task_done

            if CLI::UI.enable_cursor?
              if nat_index > consumed_lines
                print(task.render(idx, true, width: width) + "\n")
                consumed_lines += 1
              else
                offset = consumed_lines - int_index
                move_to = CLI::UI::ANSI.cursor_up(offset) + "\r"
                move_from = "\r" + CLI::UI::ANSI.cursor_down(offset)

                print(move_to + task.render(idx, idx.zero?, width: width) + move_from)
              end
            elsif !tasks_seen[int_index] || (task_done && !tasks_seen_done[int_index])
              print(task.render(idx, true, width: width) + "\n")
            end

            tasks_seen[int_index] = true
            tasks_seen_done[int_index] ||= task_done
          end
        end
      end
    end

    break if done_count == @tasks.size

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

  if @auto_debrief
    debrief
  else
    all_succeeded?
  end
rescue Interrupt
  @work_queue.interrupt
  raise
end