Class: Rich::ProgressBar
- Inherits:
-
Object
- Object
- Rich::ProgressBar
- Defined in:
- lib/rich/progress.rb
Overview
A progress bar for tracking task completion
Instance Attribute Summary collapse
-
#bar_char ⇒ String
readonly
Bar character (filled).
-
#complete_style ⇒ Style?
readonly
Completed portion style.
-
#completed ⇒ Integer
readonly
Completed steps.
-
#finished_style ⇒ Style?
readonly
Finished style.
-
#incomplete_style ⇒ Style?
readonly
Remaining portion style.
-
#pulse ⇒ Boolean
readonly
Pulse animation.
-
#show_percentage ⇒ Boolean
readonly
Show percentage.
-
#total ⇒ Integer
readonly
Total steps.
-
#unfilled_char ⇒ String
readonly
Bar character (unfilled).
-
#width ⇒ Integer
readonly
Width of the bar.
Instance Method Summary collapse
-
#advance(steps = 1) ⇒ self
Update progress.
-
#elapsed ⇒ Float?
Elapsed time since start.
-
#eta ⇒ Float?
Estimated time remaining.
-
#finished? ⇒ Boolean
True if complete.
-
#format_time(seconds) ⇒ String
Format time as string.
-
#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
constructor
A new instance of ProgressBar.
-
#percentage ⇒ Integer
Progress as percentage (0 to 100).
-
#progress ⇒ Float
Progress as fraction (0.0 to 1.0).
-
#render(color_system: ColorSystem::TRUECOLOR) ⇒ String
Render to string with ANSI codes.
-
#reset ⇒ self
Reset progress.
-
#to_segments ⇒ Array<Segment>
Render progress bar to segments.
-
#update(value) ⇒ self
Set completed value directly.
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 = @unfilled_char = unfilled_char @start_time = nil end |
Instance Attribute Details
#bar_char ⇒ String (readonly)
Returns Bar character (filled).
109 110 111 |
# File 'lib/rich/progress.rb', line 109 def @bar_char end |
#complete_style ⇒ Style? (readonly)
Returns Completed portion style.
94 95 96 |
# File 'lib/rich/progress.rb', line 94 def complete_style @complete_style end |
#completed ⇒ Integer (readonly)
Returns Completed steps.
88 89 90 |
# File 'lib/rich/progress.rb', line 88 def completed @completed end |
#finished_style ⇒ Style? (readonly)
Returns Finished style.
100 101 102 |
# File 'lib/rich/progress.rb', line 100 def finished_style @finished_style end |
#incomplete_style ⇒ Style? (readonly)
Returns Remaining portion style.
97 98 99 |
# File 'lib/rich/progress.rb', line 97 def incomplete_style @incomplete_style end |
#pulse ⇒ Boolean (readonly)
Returns Pulse animation.
106 107 108 |
# File 'lib/rich/progress.rb', line 106 def pulse @pulse end |
#show_percentage ⇒ Boolean (readonly)
Returns Show percentage.
103 104 105 |
# File 'lib/rich/progress.rb', line 103 def show_percentage @show_percentage end |
#total ⇒ Integer (readonly)
Returns Total steps.
85 86 87 |
# File 'lib/rich/progress.rb', line 85 def total @total end |
#unfilled_char ⇒ String (readonly)
Returns Bar character (unfilled).
112 113 114 |
# File 'lib/rich/progress.rb', line 112 def unfilled_char @unfilled_char end |
#width ⇒ Integer (readonly)
Returns 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
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 |
#elapsed ⇒ Float?
Elapsed time since start
182 183 184 185 186 |
# File 'lib/rich/progress.rb', line 182 def elapsed return nil unless @start_time Time.now - @start_time end |
#eta ⇒ Float?
Estimated time 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.
150 151 152 |
# File 'lib/rich/progress.rb', line 150 def finished? @completed >= @total end |
#format_time(seconds) ⇒ String
Format time as string
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 |
#percentage ⇒ Integer
Returns Progress as percentage (0 to 100).
145 146 147 |
# File 'lib/rich/progress.rb', line 145 def percentage (progress * 100).round end |
#progress ⇒ Float
Returns 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
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 |
#reset ⇒ self
Reset progress
174 175 176 177 178 |
# File 'lib/rich/progress.rb', line 174 def reset @completed = 0 @start_time = nil self end |
#to_segments ⇒ Array<Segment>
Render progress bar to segments
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
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 |