Class: Rich::ProgressTask

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

Overview

A task in progress tracking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description:, total: 100) ⇒ ProgressTask

Returns a new instance of ProgressTask.



271
272
273
274
275
276
277
278
# File 'lib/rich/progress.rb', line 271

def initialize(description:, total: 100)
  @description = description
  @total = total
  @completed = 0
  @finished = false
  @start_time = Time.now
  @end_time = nil
end

Instance Attribute Details

#completedInteger (readonly)

Returns Completed steps.

Returns:

  • (Integer)

    Completed steps



260
261
262
# File 'lib/rich/progress.rb', line 260

def completed
  @completed
end

#descriptionString (readonly)

Returns Task description.

Returns:

  • (String)

    Task description



254
255
256
# File 'lib/rich/progress.rb', line 254

def description
  @description
end

#end_timeTime? (readonly)

Returns End time.

Returns:

  • (Time, nil)

    End time



269
270
271
# File 'lib/rich/progress.rb', line 269

def end_time
  @end_time
end

#finishedBoolean (readonly)

Returns Task is finished.

Returns:

  • (Boolean)

    Task is finished



263
264
265
# File 'lib/rich/progress.rb', line 263

def finished
  @finished
end

#start_timeTime (readonly)

Returns Start time.

Returns:

  • (Time)

    Start time



266
267
268
# File 'lib/rich/progress.rb', line 266

def start_time
  @start_time
end

#totalInteger (readonly)

Returns Total steps.

Returns:

  • (Integer)

    Total steps



257
258
259
# File 'lib/rich/progress.rb', line 257

def total
  @total
end

Instance Method Details

#advance(steps = 1) ⇒ Object

Update progress

Parameters:

  • advance (Integer)

    Steps to advance



282
283
284
285
# File 'lib/rich/progress.rb', line 282

def advance(steps = 1)
  @completed = [@completed + steps, @total].min
  finish if @completed >= @total
end

#elapsedFloat?

Returns Elapsed time.

Returns:

  • (Float, nil)

    Elapsed time



306
307
308
# File 'lib/rich/progress.rb', line 306

def elapsed
  (@end_time || Time.now) - @start_time
end

#finishObject

Mark as finished



295
296
297
298
# File 'lib/rich/progress.rb', line 295

def finish
  @finished = true
  @end_time = Time.now
end

#progressFloat

Returns Progress fraction.

Returns:

  • (Float)

    Progress fraction



301
302
303
# File 'lib/rich/progress.rb', line 301

def progress
  @completed.to_f / @total
end

#update(value) ⇒ Object

Set completed directly

Parameters:

  • value (Integer)

    Completed value



289
290
291
292
# File 'lib/rich/progress.rb', line 289

def update(value)
  @completed = [[value, 0].max, @total].min
  finish if @completed >= @total
end