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

Instance Method Summary collapse

Methods included from T::Sig

sig

Constructor Details

#initialize(auto_debrief: true) ⇒ SpinGroup

Returns a new instance of SpinGroup.


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

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

Instance Method Details

#add(title, &block) ⇒ Object


200
201
202
203
204
# File 'lib/cli/ui/spinner/spin_group.rb', line 200

def add(title, &block)
  @m.synchronize do
    @tasks << Task.new(title, &block)
  end
end

#debriefObject


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/cli/ui/spinner/spin_group.rb', line 262

def debrief
  @m.synchronize do
    @tasks.each do |task|
      next if task.success

      e = task.exception
      out = task.stdout
      err = task.stderr

      CLI::UI::Frame.open('Task Failed: ' + task.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

#waitObject


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/cli/ui/spinner/spin_group.rb', line 214

def wait
  idx = 0

  loop do
    all_done = T.let(true, T::Boolean)

    width = CLI::UI::Terminal.width

    @m.synchronize do
      CLI::UI.raw do
        @tasks.each.with_index do |task, int_index|
          nat_index = int_index + 1
          task_done = task.check
          all_done = false unless 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

    break if all_done

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

  if @auto_debrief
    debrief
  else
    @m.synchronize do
      @tasks.all?(&:success)
    end
  end
end