-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpowerbar-demo
executable file
·263 lines (243 loc) · 6.86 KB
/
powerbar-demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env ruby
require 'rubygems'
require 'powerbar'
require 'logger'
# DEMO 1
def demo_1
total = 100000
step = 1000
p = PowerBar.new
# This is how you alter the settings.
# (see the source-code for all available settings)
p.settings.tty.finite.show_eta = true
(0..total).step(step).each do |i|
p.show({:msg => 'DEMO 1 - Ten seconds of progress', :done => i, :total => total})
sleep 0.1
end
# close(true) will set the bar to 100% before closing.
# use this when you are sure your operation has finished but
# for some reason 'done' may still be smaller than 'total'.
#
# this is not necessary in this example, and usually a bad idea,
# but demonstrated here so you know it exists.
p.close(true)
end
# DEMO 2
def demo_2
total = 100000
step = 1000
text = " "*30 + "Need to change text in mid-progress? No Problemo!" +
" "*5 + "As seen in: DEMO 2 - Scrolling madness"
p = PowerBar.new
j = 0
(0..total).step(step).each do |i|
woah = sprintf('%-26s', text[j%text.length..j%text.length+25])
p.show({:msg => woah, :done => i, :total => total})
j += 1
sleep 0.1
end
p.close
end
# DEMO 3
def demo_3
total = 100000
step = 1000
text = "DEMO 3 - Don't sweat your message width too much, we can squeeze it some"
p = PowerBar.new
j = 0
(0..total).step(step).each do |i|
woah = text[0..j]
p.show({:msg => woah, :done => i, :total => total})
j += 1
sleep 0.1
end
p.close
end
# DEMO 4
def demo_4
total = 100000
step = 1000
text = "\e[0mDEMO 4 - Colors!"
interstitial = [
"Oh, btw, know what?\n",
"You may use PowerBar#print to print messages while the bar is running.\n",
"Awesome, right?\n"
]
p = PowerBar.new
p.settings.tty.finite.template.main = \
"${<msg>} ${<bar> }\e[0m${<rate>/s} \e[33;1m${<percent>%} " +
"\e[36;1m${<elapsed>}\e[31;1m${ ETA: <eta>}"
p.settings.tty.finite.template.padchar = "\e[30;1m#{p.settings.tty.finite.template.padchar}"
p.settings.tty.finite.template.barchar = "\e[34;1m#{p.settings.tty.finite.template.barchar}"
p.settings.tty.finite.template.exit = "\e[?25h\e[0m" # clean up after us
p.settings.tty.finite.template.close = "\e[?25h\e[0m\n" # clean up after us
p.settings.tty.finite.output = Proc.new{ |s|
# The default output-function truncates our
# string to enable the "squeezing" as seen in the
# previous demo. This doesn't mix so well with ANSI-colors,
# thus if you want to use colors you'll have to make the
# output-function a little more naive. Like this:
$stderr.print s
}
j = 0
(0..total).step(step).each do |i|
p.show({:msg => text, :done => i, :total => total})
j += 1
if 0 == j % 25
p.print interstitial.shift
end
sleep 0.1
end
p.close
end
# DEMO 5
def demo_5
total = 100000
step = 1000
text = "DEMO 5 - When total is :unknown then we only display what we know"
p = PowerBar.new
j = 0
(0..total).step(step).each do |i|
p.show({:msg => text, :done => i, :total => :unknown})
j += 1
sleep 0.1
end
p.close
end
# DEMO 6
def demo_6
total = 100000
step = 1000
text = "DEMO 6 - Still :unknown, now with a different template"
p = PowerBar.new
p.settings.tty.infinite.template.main = \
'${<msg>} > ${Rate: <rate>/s, }elapsed: ${<elapsed>}'
j = 0
(0..total).step(step).each do |i|
p.show({:msg => text, :done => i, :total => :unknown})
j += 1
sleep 0.1
end
p.close
end
# DEMO 7
def demo_7
total = 100000
step = 1000
text = "DEMO 7 - Forcing notty mode"
p = PowerBar.new
p.settings.force_mode = :notty
j = 0
(0..total).step(step).each do |i|
p.show({:msg => text, :done => i, :total => total})
j += 1
if j == 60
p.print "Hint: Of course PowerBar#print also works when in :notty-mode\n"
end
sleep 0.1
end
p.close
end
# DEMO 8
def demo_8
total = 100000
step = 1000
log = Logger.new(STDOUT)
text = "DEMO 8 - Forcing notty mode and using a logger"
p = PowerBar.new
p.settings.force_mode = :notty
p.settings.notty.finite.output = Proc.new { |s|
log.debug(s.chomp) unless s == ''
}
j = 0
(0..total).step(step).each do |i|
p.show({:msg => text, :done => i, :total => total})
j += 1
if j == 60
p.print "Hint: Yes, PowerBar#print also works here. ;)\n"
end
sleep 0.1
end
p.close
end
# DEMO 9
def demo_9
total = 100000
step = 1000
puts "DEMO 9 - No output by PowerBar."
puts "Here we use it to collect stats silently, and then we roll our own output:\n-"
p = PowerBar.new
j = 0
(0..total).step(step).each do |i|
# update() does not output anything, it only updates (who would've guessed!)
p.update({:done => i, :total => total})
j += 1
if 0 == j % 50
puts "elapsed is: #{p.elapsed}, humanized elapsed is: #{p.h_elapsed}"
puts "percent is: #{p.percent}, humanized percent is: #{p.h_percent}"
puts "eta is: #{p.eta}, humanized eta is: #{p.h_eta}"
puts "done is: #{p.done}, humanized done is: #{p.h_done}"
puts "total is: #{p.total}, humanized done is: #{p.h_total}"
puts "total is: #{p.total}, humanized done is: #{p.h_total}"
puts "bar is: #{p.bar}"
puts "-"
end
sleep 0.1
end
#p.close # no need to close this one
end
# DEMO 10
def demo_10
total = 100000
step = 1000
text = " " * 30 + "DEMO 10 - OMGWTFBBQ!"
p = PowerBar.new
p.settings.tty.finite.template.padchar = "\e[30;1m#{p.settings.tty.finite.template.padchar}"
p.settings.tty.finite.template.barchar = "\e[34;1m#{p.settings.tty.finite.template.barchar}"
p.settings.tty.finite.output = Proc.new{ |s| $stderr.print s }
j = 0
total = 300000000
step = 1000000
spin = ')|(|'
spun = ',.oO^`^Oo'
(0..total).step(step).each do |i|
p.send(:state).scope = nil # omghax, don't try this at home!
wtf_range = (j % text.length)..((j % text.length) + 25)
wtf = sprintf('%-26s', text[wtf_range])
wtf.gsub!(/./) { |char| rand(2).zero? ? char.downcase : char.upcase }
p.show(
:msg => wtf,
:done => i,
:total => total,
:settings => {
:tty => {
:finite => {
:template => {
:barchar => "\e[36;1m#{spin[j%spin.length..j%spin.length]}",
:padchar => "\e[0;34m#{spun[j%spun.length..j%spun.length]}",
:main => "\e[#{rand(1)};3#{rand(7)}m${<msg>}\e[0m "+
"\e[0m${<bar> }\e[0m${<rate>/s} "+
"\e[33;1m${<percent>%} " +
"\e[36;1m${<elapsed>}\e[31;1m${ ETA: <eta>}",
:exit => "\e[?25h\e[0m",
:close => "\e[?25h\e[0m\n"
}
}
}
}
)
j += 1
sleep 0.1
end
p.wipe
puts "Thanks for watching! ;-)"
end
begin
method("demo_#{ARGV[0]}".to_sym).call
rescue
(1..10).each do |i|
method("demo_#{i}".to_sym).call
puts "---"
sleep 1
end
end