Class: Rich::Progress
- Inherits:
-
Object
- Object
- Rich::Progress
- 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
-
#console ⇒ Console
readonly
Console for output.
-
#refresh_rate ⇒ Float
readonly
Refresh interval.
-
#tasks ⇒ Array<ProgressTask>
readonly
Tasks.
Instance Method Summary collapse
-
#add_task(description, total: 100) ⇒ ProgressTask
Add a new task.
-
#initialize(console: nil, refresh_rate: DEFAULT_REFRESH, transient: true) ⇒ Progress
constructor
A new instance of Progress.
-
#refresh ⇒ void
Refresh display if needed.
-
#start { ... } ⇒ void
Start progress display.
-
#stop ⇒ void
Stop progress display.
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
#console ⇒ Console (readonly)
Returns Console for output.
320 321 322 |
# File 'lib/rich/progress.rb', line 320 def console @console end |
#refresh_rate ⇒ Float (readonly)
Returns Refresh interval.
323 324 325 |
# File 'lib/rich/progress.rb', line 323 def refresh_rate @refresh_rate end |
#tasks ⇒ Array<ProgressTask> (readonly)
Returns Tasks.
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
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 |
#refresh ⇒ void
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
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 |
#stop ⇒ void
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 |