Class: Philiprehberger::Progress::Bar
- Inherits:
-
Object
- Object
- Philiprehberger::Progress::Bar
- Defined in:
- lib/philiprehberger/progress/bar.rb
Constant Summary collapse
- FILL_CHAR =
"\u2588"- EMPTY_CHAR =
"\u2591"
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #advance(n = 1) ⇒ Object
- #elapsed ⇒ Object
- #eta ⇒ Object
- #finish ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(total:, width: 30, output: $stderr, fill: '=', empty: ' ', tip: '>') ⇒ Bar
constructor
A new instance of Bar.
-
#pause ⇒ self
Pause the progress bar, freezing elapsed time calculation.
- #paused? ⇒ Boolean
- #percentage ⇒ Object
-
#reset ⇒ self
Reset the bar to 0, preserving total and width.
-
#resume ⇒ self
Resume after pause.
-
#set(n) ⇒ self
Set absolute progress position.
- #throughput ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(total:, width: 30, output: $stderr, fill: '=', empty: ' ', tip: '>') ⇒ Bar
Returns a new instance of Bar.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/philiprehberger/progress/bar.rb', line 13 def initialize(total:, width: 30, output: $stderr, fill: '=', empty: ' ', tip: '>') @total = [total, 0].max @width = width @output = output @current = 0 @start_time = now @finished = false @paused = false @pause_elapsed = 0.0 @pause_start = nil @fill = fill @empty = empty @tip = tip end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
11 12 13 |
# File 'lib/philiprehberger/progress/bar.rb', line 11 def current @current end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
11 12 13 |
# File 'lib/philiprehberger/progress/bar.rb', line 11 def total @total end |
Instance Method Details
#advance(n = 1) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/philiprehberger/progress/bar.rb', line 28 def advance(n = 1) return self if @finished @current = [@current + n, @total].min render_to_output self end |
#elapsed ⇒ Object
106 107 108 109 110 |
# File 'lib/philiprehberger/progress/bar.rb', line 106 def elapsed raw = now - @start_time - @pause_elapsed raw -= (now - @pause_start) if @paused raw end |
#eta ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/philiprehberger/progress/bar.rb', line 112 def eta return 0.0 if @current >= @total return nil if @current.zero? elapsed_time = elapsed items_per_sec = @current.to_f / elapsed_time (@total - @current) / items_per_sec end |
#finish ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/philiprehberger/progress/bar.rb', line 88 def finish @current = @total @finished = true render_to_output @output.write("\n") if tty? self end |
#finished? ⇒ Boolean
96 97 98 |
# File 'lib/philiprehberger/progress/bar.rb', line 96 def finished? @finished end |
#pause ⇒ self
Pause the progress bar, freezing elapsed time calculation.
64 65 66 67 68 69 70 |
# File 'lib/philiprehberger/progress/bar.rb', line 64 def pause return self if @paused || @finished @paused = true @pause_start = now self end |
#paused? ⇒ Boolean
84 85 86 |
# File 'lib/philiprehberger/progress/bar.rb', line 84 def paused? @paused end |
#percentage ⇒ Object
100 101 102 103 104 |
# File 'lib/philiprehberger/progress/bar.rb', line 100 def percentage return 100.0 if @total.zero? (@current.to_f / @total * 100).round(1) end |
#reset ⇒ self
Reset the bar to 0, preserving total and width. Restarts the timer.
51 52 53 54 55 56 57 58 59 |
# File 'lib/philiprehberger/progress/bar.rb', line 51 def reset @current = 0 @finished = false @paused = false @pause_elapsed = 0.0 @pause_start = nil @start_time = now self end |
#resume ⇒ self
Resume after pause.
75 76 77 78 79 80 81 82 |
# File 'lib/philiprehberger/progress/bar.rb', line 75 def resume return self unless @paused @pause_elapsed += now - @pause_start @pause_start = nil @paused = false self end |
#set(n) ⇒ self
Set absolute progress position.
40 41 42 43 44 45 46 |
# File 'lib/philiprehberger/progress/bar.rb', line 40 def set(n) return self if @finished @current = [[n, 0].max, @total].min render_to_output self end |
#throughput ⇒ Object
121 122 123 124 125 126 |
# File 'lib/philiprehberger/progress/bar.rb', line 121 def throughput elapsed_time = elapsed return 0.0 if elapsed_time.zero? @current.to_f / elapsed_time end |
#to_h ⇒ Object
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/philiprehberger/progress/bar.rb', line 128 def to_h { percentage: percentage, elapsed: elapsed, eta: eta, throughput: throughput, current: @current, total: @total } end |
#to_s ⇒ Object
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/philiprehberger/progress/bar.rb', line 139 def to_s return to_h.to_json if Philiprehberger::Progress.json_mode? = pct = format('%<p>5.1f%%', p: percentage) eta_str = format_eta(eta) tput = format('%<t>.1f/s', t: throughput) "[#{}] #{pct} | #{@current}/#{@total} | ETA: #{eta_str} | #{tput}" end |