Class: Rich::ProgressBar

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

Overview

A progress bar for tracking task completion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total: 100, completed: 0, width: 40, complete_style: "bar.complete", incomplete_style: "bar.back", finished_style: "bar.finished", show_percentage: true, pulse: false, bar_char: "━", unfilled_char: "━") ⇒ ProgressBar

Returns a new instance of ProgressBar.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rich/progress.rb', line 114

def initialize(
  total: 100,
  completed: 0,
  width: 40,
  complete_style: "bar.complete",
  incomplete_style: "bar.back",
  finished_style: "bar.finished",
  show_percentage: true,
  pulse: false,
  bar_char: "",
  unfilled_char: ""
)
  @total = [total, 1].max
  @completed = [completed, 0].max
  @width = width
  @complete_style = complete_style.is_a?(String) ? Style.parse(complete_style) : complete_style
  @incomplete_style = incomplete_style.is_a?(String) ? Style.parse(incomplete_style) : incomplete_style
  @finished_style = finished_style.is_a?(String) ? Style.parse(finished_style) : finished_style
  @show_percentage = show_percentage
  @pulse = pulse
  @bar_char = bar_char
  @unfilled_char = unfilled_char
  @start_time = nil
end

Instance Attribute Details

#bar_charString (readonly)

Returns Bar character (filled).

Returns:

  • (String)

    Bar character (filled)



109
110
111
# File 'lib/rich/progress.rb', line 109

def bar_char
  @bar_char
end

#complete_styleStyle? (readonly)

Returns Completed portion style.

Returns:

  • (Style, nil)

    Completed portion style



94
95
96
# File 'lib/rich/progress.rb', line 94

def complete_style
  @complete_style
end

#completedInteger (readonly)

Returns Completed steps.

Returns:

  • (Integer)

    Completed steps



88
89
90
# File 'lib/rich/progress.rb', line 88

def completed
  @completed
end

#finished_styleStyle? (readonly)

Returns Finished style.

Returns:

  • (Style, nil)

    Finished style



100
101
102
# File 'lib/rich/progress.rb', line 100

def finished_style
  @finished_style
end

#incomplete_styleStyle? (readonly)

Returns Remaining portion style.

Returns:

  • (Style, nil)

    Remaining portion style



97
98
99
# File 'lib/rich/progress.rb', line 97

def incomplete_style
  @incomplete_style
end

#pulseBoolean (readonly)

Returns Pulse animation.

Returns:

  • (Boolean)

    Pulse animation



106
107
108
# File 'lib/rich/progress.rb', line 106

def pulse
  @pulse
end

#show_percentageBoolean (readonly)

Returns Show percentage.

Returns:

  • (Boolean)

    Show percentage



103
104
105
# File 'lib/rich/progress.rb', line 103

def show_percentage
  @show_percentage
end

#totalInteger (readonly)

Returns Total steps.

Returns:

  • (Integer)

    Total steps



85
86
87
# File 'lib/rich/progress.rb', line 85

def total
  @total
end

#unfilled_charString (readonly)

Returns Bar character (unfilled).

Returns:

  • (String)

    Bar character (unfilled)



112
113
114
# File 'lib/rich/progress.rb', line 112

def unfilled_char
  @unfilled_char
end

#widthInteger (readonly)

Returns Width of the bar.

Returns:

  • (Integer)

    Width of the bar



91
92
93
# File 'lib/rich/progress.rb', line 91

def width
  @width
end

Instance Method Details

#advance(steps = 1) ⇒ self

Update progress

Parameters:

  • advance (Integer)

    Steps to advance

Returns:

  • (self)


157
158
159
160
161
# File 'lib/rich/progress.rb', line 157

def advance(steps = 1)
  @start_time ||= Time.now
  @completed = [@completed + steps, @total].min
  self
end

#elapsedFloat?

Elapsed time since start

Returns:

  • (Float, nil)

    Seconds elapsed



182
183
184
185
186
# File 'lib/rich/progress.rb', line 182

def elapsed
  return nil unless @start_time

  Time.now - @start_time
end

#etaFloat?

Estimated time remaining

Returns:

  • (Float, nil)

    Seconds remaining



190
191
192
193
194
195
196
# File 'lib/rich/progress.rb', line 190

def eta
  return nil unless @start_time && progress > 0

  elapsed_time = elapsed
  total_estimated = elapsed_time / progress
  total_estimated - elapsed_time
end

#finished?Boolean

Returns True if complete.

Returns:

  • (Boolean)

    True if complete



150
151
152
# File 'lib/rich/progress.rb', line 150

def finished?
  @completed >= @total
end

#format_time(seconds) ⇒ String

Format time as string

Parameters:

  • seconds (Float)

    Seconds

Returns:

  • (String)

    Formatted time



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rich/progress.rb', line 201

def format_time(seconds)
  return "--:--" unless seconds

  mins = (seconds / 60).floor
  secs = (seconds % 60).floor

  if mins >= 60
    hours = (mins / 60).floor
    mins = mins % 60
    format("%d:%02d:%02d", hours, mins, secs)
  else
    format("%d:%02d", mins, secs)
  end
end

#percentageInteger

Returns Progress as percentage (0 to 100).

Returns:

  • (Integer)

    Progress as percentage (0 to 100)



145
146
147
# File 'lib/rich/progress.rb', line 145

def percentage
  (progress * 100).round
end

#progressFloat

Returns Progress as fraction (0.0 to 1.0).

Returns:

  • (Float)

    Progress as fraction (0.0 to 1.0)



140
141
142
# File 'lib/rich/progress.rb', line 140

def progress
  @completed.to_f / @total
end

#render(color_system: ColorSystem::TRUECOLOR) ⇒ String

Render to string with ANSI codes

Parameters:

  • color_system (Symbol) (defaults to: ColorSystem::TRUECOLOR)

    Color system

Returns:

  • (String)


246
247
248
# File 'lib/rich/progress.rb', line 246

def render(color_system: ColorSystem::TRUECOLOR)
  Segment.render(to_segments, color_system: color_system)
end

#resetself

Reset progress

Returns:

  • (self)


174
175
176
177
178
# File 'lib/rich/progress.rb', line 174

def reset
  @completed = 0
  @start_time = nil
  self
end

#to_segmentsArray<Segment>

Render progress bar to segments

Returns:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rich/progress.rb', line 218

def to_segments
  segments = []

  filled_width = (progress * @width).round
  unfilled_width = @width - filled_width

  # Bar
  style = finished? ? @finished_style : @complete_style

  if filled_width > 0
    segments << Segment.new(@bar_char * filled_width, style: style)
  end

  if unfilled_width > 0
    segments << Segment.new(@unfilled_char * unfilled_width, style: @incomplete_style)
  end

  # Percentage
  if @show_percentage
    segments << Segment.new(" #{percentage}%")
  end

  segments
end

#update(value) ⇒ self

Set completed value directly

Parameters:

  • value (Integer)

    Completed steps

Returns:

  • (self)


166
167
168
169
170
# File 'lib/rich/progress.rb', line 166

def update(value)
  @start_time ||= Time.now
  @completed = [[value, 0].max, @total].min
  self
end