Class: Rich::Progress

Inherits:
Object
  • Object
show all
Defined in:
lib/rich/progress.rb

Overview

Progress display for multiple tasks

Constant Summary collapse

DEFAULT_REFRESH =

Windows has slower console, so refresh less frequently

Gem.win_platform? ? 0.2 : 0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console: nil, refresh_rate: DEFAULT_REFRESH, transient: true) ⇒ Progress

Returns a new instance of Progress.



325
326
327
328
329
330
331
332
333
# File 'lib/rich/progress.rb', line 325

def initialize(console: nil, refresh_rate: DEFAULT_REFRESH, transient: true)
  @console = console || Console.new
  @refresh_rate = refresh_rate
  @transient = transient
  @tasks = []
  @started = false
  @finished = false
  @last_render = nil
end

Instance Attribute Details

#consoleConsole (readonly)

Returns Console for output.

Returns:



320
321
322
# File 'lib/rich/progress.rb', line 320

def console
  @console
end

#refresh_rateFloat (readonly)

Returns Refresh interval.

Returns:

  • (Float)

    Refresh interval



323
324
325
# File 'lib/rich/progress.rb', line 323

def refresh_rate
  @refresh_rate
end

#tasksArray<ProgressTask> (readonly)

Returns Tasks.

Returns:



317
318
319
# File 'lib/rich/progress.rb', line 317

def tasks
  @tasks
end

Instance Method Details

#add_task(description, total: 100) ⇒ ProgressTask

Add a new task

Parameters:

  • description (String)

    Task description

  • total (Integer) (defaults to: 100)

    Total steps

Returns:



339
340
341
342
343
# File 'lib/rich/progress.rb', line 339

def add_task(description, total: 100)
  task = ProgressTask.new(description: description, total: total)
  @tasks << task
  task
end

#refreshvoid

This method returns an undefined value.

Refresh display if needed



374
375
376
377
378
379
380
381
382
# File 'lib/rich/progress.rb', line 374

def refresh
  return unless @started

  now = Time.now
  if @last_render.nil? || now - @last_render >= @refresh_rate
    render
    @last_render = now
  end
end

#start { ... } ⇒ void

This method returns an undefined value.

Start progress display

Yields:

  • Block to execute with progress tracking



348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/rich/progress.rb', line 348

def start
  @started = true
  @console.hide_cursor

  if block_given?
    begin
      yield self
    ensure
      stop
    end
  end
end

#stopvoid

This method returns an undefined value.

Stop progress display



363
364
365
366
367
368
369
370
# File 'lib/rich/progress.rb', line 363

def stop
  return unless @started

  @finished = true
  render_final
  @console.show_cursor
  @started = false
end