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.to_s : CLI::UI::Glyph::X.to_s }

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initialize(auto_debrief: true) ⇒ SpinGroup

Returns a new instance of SpinGroup.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cli/ui/spinner/spin_group.rb', line 63

def initialize(auto_debrief: true)
  @m = Mutex.new
  @consumed_lines = 0
  @tasks = []
  @auto_debrief = auto_debrief
  @start = Time.new
  if block_given?
    yield self
    wait
  end
end

Class Attribute Details

.pause_mutexObject (readonly)

Returns the value of attribute pause_mutex.



13
14
15
# File 'lib/cli/ui/spinner/spin_group.rb', line 13

def pause_mutex
  @pause_mutex
end

Class Method Details

.pause_spinners(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cli/ui/spinner/spin_group.rb', line 25

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)


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

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cli/ui/spinner/spin_group.rb', line 260

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,
      &block
    )
  end
end

#all_succeeded?Boolean

Returns:

  • (Boolean)


357
358
359
360
361
# File 'lib/cli/ui/spinner/spin_group.rb', line 357

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

#debriefObject



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

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



342
343
344
# File 'lib/cli/ui/spinner/spin_group.rb', line 342

def failure_debrief(&block)
  @failure_debrief = block
end

#success_debrief(&block) ⇒ Object



352
353
354
# File 'lib/cli/ui/spinner/spin_group.rb', line 352

def success_debrief(&block)
  @success_debrief = block
end

#waitObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/cli/ui/spinner/spin_group.rb', line 286

def wait
  idx = 0

  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 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
          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
  @tasks.each(&:interrupt)
  raise
end