powerbar 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/powerbar-demo CHANGED
@@ -53,6 +53,11 @@ def demo_4
53
53
  total = 100000
54
54
  step = 1000
55
55
  text = "\e[0mDEMO 4 - Colors!"
56
+ interstitial = [
57
+ "Oh, btw, know what?\n",
58
+ "You may use PowerBar#print to print messages while the bar is running.\n",
59
+ "Awesome, right?\n"
60
+ ]
56
61
  p = PowerBar.new
57
62
  p.settings.tty.finite.template.main = \
58
63
  "${<msg>} ${<bar> }\e[0m${<rate>/s} \e[33;1m${<percent>%} " +
@@ -73,6 +78,9 @@ def demo_4
73
78
  (0..total).step(step).each do |i|
74
79
  p.show({:msg => text, :done => i, :total => total})
75
80
  j += 1
81
+ if 0 == j % 25
82
+ p.print interstitial.shift
83
+ end
76
84
  sleep 0.1
77
85
  end
78
86
  p.close
@@ -122,6 +130,9 @@ def demo_7
122
130
  (0..total).step(step).each do |i|
123
131
  p.show({:msg => text, :done => i, :total => total})
124
132
  j += 1
133
+ if j == 60
134
+ p.print "Hint: Of course PowerBar#print also works when in :notty-mode\n"
135
+ end
125
136
  sleep 0.1
126
137
  end
127
138
  p.close
@@ -136,12 +147,15 @@ def demo_8
136
147
  p = PowerBar.new
137
148
  p.settings.force_mode = :notty
138
149
  p.settings.notty.finite.output = Proc.new { |s|
139
- log.debug(s) unless s == ''
150
+ log.debug(s.chomp) unless s == ''
140
151
  }
141
152
  j = 0
142
153
  (0..total).step(step).each do |i|
143
154
  p.show({:msg => text, :done => i, :total => total})
144
155
  j += 1
156
+ if j == 60
157
+ p.print "Hint: Yes, PowerBar#print also works here. ;)\n"
158
+ end
145
159
  sleep 0.1
146
160
  end
147
161
  p.close
@@ -153,7 +167,7 @@ def demo_9
153
167
  step = 1000
154
168
 
155
169
  puts "DEMO 9 - No output by PowerBar."
156
- puts "We only use it to collect stats and then roll our own output...\n---"
170
+ puts "Here we use it to collect stats silently, and then we roll our own output:\n-"
157
171
  p = PowerBar.new
158
172
  j = 0
159
173
  (0..total).step(step).each do |i|
@@ -168,7 +182,7 @@ def demo_9
168
182
  puts "total is: #{p.total}, humanized done is: #{p.h_total}"
169
183
  puts "total is: #{p.total}, humanized done is: #{p.h_total}"
170
184
  puts "bar is: #{p.bar}"
171
- puts "---"
185
+ puts "-"
172
186
  end
173
187
  sleep 0.1
174
188
  end
data/lib/powerbar.rb CHANGED
@@ -24,11 +24,10 @@ require 'powerbar/version'
24
24
  require 'ansi'
25
25
  require 'hashie/mash'
26
26
 
27
+ #
28
+ # This is PowerBar - The last progressbar-library you'll ever need.
29
+ #
27
30
  class PowerBar
28
- #
29
- # This is PowerBar - The last progressbar-library you'll ever need.
30
- #
31
-
32
31
  STRIP_ANSI = Regexp.compile '\e\[(\d+)(;\d+)?(;\d+)?[m|K]', nil
33
32
 
34
33
  def initialize(opts={})
@@ -67,7 +66,7 @@ class PowerBar
67
66
  # evaluates to something other than nil.
68
67
  :main => '${<msg>}: ${[<bar>] }${<rate>/s }${<percent>% }${<elapsed>}${, ETA: <eta>}',
69
68
  :post => '', # printed after the progressbar
70
- :wipe => "\e[1000D\e[K", # printed when 'wipe' is called
69
+ :wipe => "\e[0m\e[1000D\e[K", # printed when 'wipe' is called
71
70
  :close => "\e[?25h\n", # printed when 'close' is called
72
71
  :exit => "\e[?25h", # printed if the process exits unexpectedly
73
72
  :barchar => "\u2588", # fill-char for the progress-bar
@@ -81,7 +80,7 @@ class PowerBar
81
80
  :pre => "\e[1000D\e[?25l",
82
81
  :main => "${<msg>}: ${<done> }${<rate>/s }${<elapsed>}",
83
82
  :post => "\e[K",
84
- :wipe => "\e[1000D\e[K",
83
+ :wipe => "\e[0m\e[1000D\e[K",
85
84
  :close => "\e[?25h\n",
86
85
  :exit => "\e[?25h",
87
86
  :barchar => "\u2588",
@@ -126,12 +125,12 @@ class PowerBar
126
125
  }.merge(opts) )
127
126
  end
128
127
 
129
- # Access the settings-hash
128
+ # settings-hash
130
129
  def settings
131
130
  @state.settings
132
131
  end
133
132
 
134
- # Access settings under current scope (e.g. tty.infinite)
133
+ # settings under current scope (e.g. tty.infinite)
135
134
  def scope
136
135
  scope_hash = [settings.force_mode,state.total].hash
137
136
  return @state.scope unless @state.scope.nil? or scope_hash != @state.scope_hash
@@ -152,7 +151,7 @@ class PowerBar
152
151
  state.scope
153
152
  end
154
153
 
155
- # Hook at_exit to ensure cleanup when we get interrupted
154
+ # Hook at_exit to ensure cleanup if we get interrupted
156
155
  def hook_exit
157
156
  return if @@exit_hooked
158
157
  if scope.template.exit
@@ -163,13 +162,19 @@ class PowerBar
163
162
  @@exit_hooked = true
164
163
  end
165
164
 
166
- # This prints the close-template which normally prints a newline.
165
+ # Print the close-template and defuse the exit-hook.
167
166
  # Be a good citizen, always close your PowerBars!
168
167
  def close
169
168
  scope.output.call(scope.template.close) unless scope.template.close.nil?
170
169
  state.closed = true
171
170
  end
172
171
 
172
+ # Remove progress-bar, print a message
173
+ def print(s)
174
+ wipe
175
+ scope.output.call(s)
176
+ end
177
+
173
178
  # Update state (and settings) without printing anything.
174
179
  def update(opts={})
175
180
  state.merge!(opts)
@@ -182,18 +187,20 @@ class PowerBar
182
187
  @rate.append(state.time_now, state.done)
183
188
  end
184
189
 
185
- # Display the PowerBar.
190
+ # Output the PowerBar.
191
+ # Returns true if bar was shown, false otherwise.
186
192
  def show(opts={})
187
- if scope.interval <= Time.now - state.time_last_show
188
- update(opts)
189
- hook_exit
190
-
191
- state.time_last_show = Time.now
192
- state.closed = false
193
- scope.output.call(scope.template.pre)
194
- scope.output.call(render)
195
- scope.output.call(scope.template.post)
196
- end
193
+ return false if scope.interval > Time.now - state.time_last_show
194
+
195
+ update(opts)
196
+ hook_exit
197
+
198
+ state.time_last_show = Time.now
199
+ state.closed = false
200
+ scope.output.call(scope.template.pre)
201
+ scope.output.call(render)
202
+ scope.output.call(scope.template.post)
203
+ true
197
204
  end
198
205
 
199
206
  # Render the PowerBar and return as a string.
@@ -343,19 +350,19 @@ class PowerBar
343
350
 
344
351
  class Rate < Array
345
352
  attr_reader :last_sample_at
346
- def initialize(at, len, max_interval=10, interval_step=0.1)
353
+ def initialize(at, window, max_interval=10, interval_step_up=0.1)
347
354
  super([])
348
355
  @last_sample_at = at
349
356
  @sample_interval = 0
350
- @sample_interval_step = interval_step
357
+ @sample_interval_step_up = interval_step_up
351
358
  @sample_interval_max = max_interval
352
359
  @counter = 0
353
- @len = len
360
+ @window = window
354
361
  end
355
362
 
356
363
  def append(at, v)
357
364
  return if @sample_interval > at - @last_sample_at
358
- @sample_interval += @sample_interval_step if @sample_interval < @sample_interval_max
365
+ @sample_interval += @sample_interval_step_up if @sample_interval < @sample_interval_max
359
366
 
360
367
  rate = (v - @counter) / (at - @last_sample_at).to_f
361
368
  return if rate.nan?
@@ -364,9 +371,9 @@ class PowerBar
364
371
  @counter = v
365
372
 
366
373
  self << rate
367
- if length > @len
368
- shift
369
- end
374
+ shift while @window < length
375
+
376
+ self
370
377
  end
371
378
 
372
379
  def sum
@@ -1,3 +1,3 @@
1
- module Powerbar
2
- VERSION = "1.0.1"
1
+ class Powerbar
2
+ VERSION = "1.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: powerbar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ansi
16
- requirement: &15214280 !ruby/object:Gem::Requirement
16
+ requirement: &9714940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.4.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15214280
24
+ version_requirements: *9714940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hashie
27
- requirement: &15213600 !ruby/object:Gem::Requirement
27
+ requirement: &9713660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *15213600
35
+ version_requirements: *9713660
36
36
  description: The last progressbar-library you'll ever need
37
37
  email:
38
38
  - moe@busyloop.net