Class: ThreadSafeProgressBar

Inherits:
ProgressBar show all
Defined in:
lib/tapsoob/multi_progress_bar.rb

Overview

Thread-safe progress bar that reports to a MultiProgressBar

Constant Summary

Constants inherited from ProgressBar

ProgressBar::VERSION

Instance Attribute Summary collapse

Attributes inherited from ProgressBar

#current, #start_time, #total

Instance Method Summary collapse

Methods inherited from ProgressBar

#file_transfer_mode, #format=, #format_arguments=, #halt, #inspect, #set

Constructor Details

#initialize(title, total, multi_progress_bar) ⇒ ThreadSafeProgressBar

Returns a new instance of ThreadSafeProgressBar.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tapsoob/multi_progress_bar.rb', line 158

def initialize(title, total, multi_progress_bar)
  @multi_progress_bar = multi_progress_bar
  @out = STDOUT  # Need this for get_width to work
  # Don't call parent initialize, we'll manage output ourselves
  @title = title
  @total = total
  @terminal_width = 80
  @bar_mark = "="
  @current = 0
  @previous = 0
  @finished_p = false
  @start_time = ::Time.now
  @previous_time = @start_time
  @format_arguments = [:title, :percentage, :bar, :stat]
end

Instance Attribute Details

#titleObject (readonly)

Returns the value of attribute title.



156
157
158
# File 'lib/tapsoob/multi_progress_bar.rb', line 156

def title
  @title
end

Instance Method Details

#clearObject

Override clear to do nothing (managed by MultiProgressBar)



207
208
209
# File 'lib/tapsoob/multi_progress_bar.rb', line 207

def clear
  # no-op
end

#finishObject

Override finish to notify multi-progress



222
223
224
225
# File 'lib/tapsoob/multi_progress_bar.rb', line 222

def finish
  @current = @total
  @multi_progress_bar.finish_bar(self)
end

#finished?Boolean

Override to use the same @finished_p flag

Returns:

  • (Boolean)


217
218
219
# File 'lib/tapsoob/multi_progress_bar.rb', line 217

def finished?
  @finished_p
end

#inc(step = 1) ⇒ Object

Override inc to check if we need to update



228
229
230
231
232
233
# File 'lib/tapsoob/multi_progress_bar.rb', line 228

def inc(step = 1)
  @current += step
  @current = @total if @current > @total
  show_if_needed
  @previous = @current
end

#mark_finishedObject

Mark this bar as finished (for tracking)



212
213
214
# File 'lib/tapsoob/multi_progress_bar.rb', line 212

def mark_finished
  @finished_p = true
end

#render_to(out) ⇒ Object

Render this bar to the given output stream



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/tapsoob/multi_progress_bar.rb', line 181

def render_to(out)
  # Get dynamic title width from MultiProgressBar for consistent alignment
  # Store as instance variable so parent class fmt_* methods can use it
  @title_width = @multi_progress_bar.max_title_width

  # Recalculate terminal width to handle resizes and use full width
  width = get_width
  # Calculate bar width: total_width - fixed_elements - padding
  # Fixed: title(variable) + " "(1) + percentage(4) + " "(1) + "|"(1) + "|"(1) + " "(1) + timer(15) = title_width + 25
  # Padding: +3 for timer fluctuations and safety
  fixed_chars = @title_width + 28
  @terminal_width = [width - fixed_chars, 20].max

  # Build format string with dynamic title width
  format = "%-#{@title_width}s %3d%% %s %s"
  arguments = @format_arguments.map { |method| send("fmt_#{method}") }
  line = sprintf(format, *arguments)

  # Ensure line doesn't exceed terminal width to prevent wrapping
  # Leave 2 chars margin for safety
  line = line[0, width - 2] if line.length > width - 2

  out.print(line)
end

#showObject

Override show to notify multi-progress instead of direct output



175
176
177
178
# File 'lib/tapsoob/multi_progress_bar.rb', line 175

def show
  @previous_time = ::Time.now  # Update to prevent time-based refresh spam
  @multi_progress_bar.update
end