Class: Tapsoob::Progress::ThreadSafeBar

Inherits:
Bar
  • Object
show all
Defined in:
lib/tapsoob/progress/thread_safe_bar.rb

Overview

Thread-safe progress bar that reports to a MultiBar

Constant Summary

Constants inherited from Bar

Bar::VERSION

Instance Attribute Summary collapse

Attributes inherited from Bar

#current, #start_time, #total

Instance Method Summary collapse

Methods inherited from Bar

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

Constructor Details

#initialize(title, total, multi_progress_bar) ⇒ ThreadSafeBar

Returns a new instance of ThreadSafeBar.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 9

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.



7
8
9
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 7

def title
  @title
end

Instance Method Details

#clearObject

Override clear to do nothing (managed by MultiBar)



58
59
60
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 58

def clear
  # no-op
end

#finishObject

Override finish to notify multi-progress



73
74
75
76
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 73

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

#finished?Boolean

Override to use the same @finished_p flag

Returns:

  • (Boolean)


68
69
70
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 68

def finished?
  @finished_p
end

#inc(step = 1) ⇒ Object

Override inc to check if we need to update



79
80
81
82
83
84
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 79

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)



63
64
65
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 63

def mark_finished
  @finished_p = true
end

#render_to(out) ⇒ Object

Render this bar to the given output stream



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 32

def render_to(out)
  # Get dynamic title width from MultiBar 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



26
27
28
29
# File 'lib/tapsoob/progress/thread_safe_bar.rb', line 26

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